A class to manage a loose queue of isolated tasks, which can be performed either synchronously (in the foreground thread) or asynchronously (by a background thread). More...
#include "asyncTaskManager.h"
Public Member Functions | |
AsyncTaskManager (const std::string &name) | |
void | add (AsyncTask *task) |
Adds the indicated task to the active queue. More... | |
void | cleanup () |
Stops all threads and messily empties the task list. More... | |
AsyncTask * | find_task (const std::string &name) const |
Returns the first task found with the indicated name, or NULL if there is no task with the indicated name. More... | |
AsyncTaskChain * | find_task_chain (const std::string &name) |
Searches a new AsyncTaskChain of the indicated name and returns it if it exists, or NULL otherwise. More... | |
AsyncTaskCollection | find_tasks (const std::string &name) const |
Returns the list of tasks found with the indicated name. More... | |
AsyncTaskCollection | find_tasks_matching (const GlobPattern &pattern) const |
Returns the list of tasks found whose name matches the indicated glob pattern, e.g. More... | |
virtual TypeHandle | force_init_type () |
AsyncTaskCollection | get_active_tasks () const |
ClockObject * | get_clock () |
double | get_next_wake_time () const |
int | get_num_task_chains () const |
size_t | get_num_tasks () const |
Returns the number of tasks that are currently active or sleeping within the task manager. More... | |
AsyncTaskCollection | get_sleeping_tasks () const |
AsyncTaskChain * | get_task_chain (int n) const |
AsyncTaskCollection | get_tasks () const |
virtual TypeHandle | get_type () const |
bool | has_task (AsyncTask *task) const |
Returns true if the indicated task has been added to this AsyncTaskManager, false otherwise. More... | |
AsyncTaskChain * | make_task_chain (const std::string &name) |
Creates a new AsyncTaskChain of the indicated name and stores it within the AsyncTaskManager. More... | |
virtual void | output (std::ostream &out) const |
void | poll () |
Runs through all the tasks in the task list, once, if the task manager is running in single-threaded mode (no threads available). More... | |
bool | remove (AsyncTask *task) |
Removes the indicated task from the active queue. More... | |
size_t | remove (const AsyncTaskCollection &tasks) |
Removes all of the tasks in the AsyncTaskCollection. More... | |
bool | remove_task_chain (const std::string &name) |
Removes the AsyncTaskChain of the indicated name. More... | |
void | set_clock (ClockObject *clock) |
void | start_threads () |
Starts any requested threads to service the tasks on the queue. More... | |
void | stop_threads () |
Stops any threads that are currently running. More... | |
void | wait_for_tasks () |
Blocks until the task list is empty. More... | |
virtual void | write (std::ostream &out, int indent_level=0) const |
Public Member Functions inherited from TypedReferenceCount | |
TypedReferenceCount (const TypedReferenceCount ©) | |
void | operator= (const TypedReferenceCount ©) |
Public Member Functions inherited from TypedObject | |
TypedObject (const TypedObject ©)=default | |
TypedObject * | as_typed_object () |
Returns the object, upcast (if necessary) to a TypedObject pointer. More... | |
const TypedObject * | as_typed_object () const |
Returns the object, upcast (if necessary) to a TypedObject pointer. More... | |
int | get_best_parent_from_Set (const std::set< int > &) const |
int | get_type_index () const |
Returns the internal index number associated with this object's TypeHandle, a unique number for each different type. More... | |
bool | is_exact_type (TypeHandle handle) const |
Returns true if the current object is the indicated type exactly. More... | |
bool | is_of_type (TypeHandle handle) const |
Returns true if the current object is or derives from the indicated type. More... | |
TypedObject & | operator= (const TypedObject ©)=default |
Public Member Functions inherited from ReferenceCount | |
int | get_ref_count () const |
WeakReferenceList * | get_weak_list () const |
Returns the WeakReferenceList associated with this ReferenceCount object. More... | |
bool | has_weak_list () const |
Returns true if this particular ReferenceCount object has a WeakReferenceList created, false otherwise. More... | |
void | local_object () |
This function should be called, once, immediately after creating a new instance of some ReferenceCount-derived object on the stack. More... | |
void | ref () const |
Explicitly increments the reference count. More... | |
bool | ref_if_nonzero () const |
Atomically increases the reference count of this object if it is not zero. More... | |
bool | test_ref_count_integrity () const |
Does some easy checks to make sure that the reference count isn't completely bogus. More... | |
bool | test_ref_count_nonzero () const |
Does some easy checks to make sure that the reference count isn't zero, or completely bogus. More... | |
virtual bool | unref () const |
Explicitly decrements the reference count. More... | |
WeakReferenceList * | weak_ref () |
Adds the indicated PointerToVoid as a weak reference to this object. More... | |
void | weak_unref () |
Removes the indicated PointerToVoid as a weak reference to this object. More... | |
Public Member Functions inherited from Namable | |
Namable (const std::string &initial_name="") | |
void | clear_name () |
Resets the Namable's name to empty. More... | |
const std::string & | get_name () const |
bool | has_name () const |
Returns true if the Namable has a nonempty name set, false if the name is empty. More... | |
void | output (std::ostream &out) const |
Outputs the Namable. More... | |
void | set_name (const std::string &name) |
Static Public Member Functions | |
static TypeHandle | get_class_type () |
static AsyncTaskManager * | get_global_ptr () |
Returns a pointer to the global AsyncTaskManager. More... | |
static void | init_type () |
Static Public Member Functions inherited from TypedReferenceCount | |
static TypeHandle | get_class_type () |
static void | init_type () |
Static Public Member Functions inherited from TypedObject | |
static TypeHandle | get_class_type () |
static void | init_type () |
This function is declared non-inline to work around a compiler bug in g++ 2.96. More... | |
Static Public Member Functions inherited from ReferenceCount | |
static TypeHandle | get_class_type () |
static void | init_type () |
Static Public Member Functions inherited from Namable | |
static TypeHandle | get_class_type () |
static void | init_type () |
Public Attributes | |
get_active_tasks | |
Returns the set of tasks that are active (and not sleeping) on the task manager, at the time of the call. More... | |
get_clock | |
Returns the clock pointer used within the AsyncTaskManager. More... | |
get_next_wake_time | |
Returns the scheduled time (on the manager's clock) of the next sleeping task, on any task chain, to awaken. More... | |
get_num_task_chains | |
Returns the number of different task chains. More... | |
get_sleeping_tasks | |
Returns the set of tasks that are sleeping (and not active) on the task manager, at the time of the call. More... | |
get_task_chain | |
Returns the nth task chain. More... | |
get_tasks | |
Returns the set of tasks that are active or sleeping on the task manager, at the time of the call. More... | |
set_clock | |
Replaces the clock pointer used within the AsyncTaskManager. More... | |
Public Attributes inherited from TypedObject | |
get_type | |
Public Attributes inherited from ReferenceCount | |
get_ref_count | |
Returns the current reference count. More... | |
Public Attributes inherited from Namable | |
get_name | |
set_name | |
Friends | |
class | AsyncFuture |
class | AsyncTask |
class | AsyncTaskChain |
class | AsyncTaskChain::AsyncTaskChainThread |
class | AsyncTaskSequence |
class | PythonTask |
A class to manage a loose queue of isolated tasks, which can be performed either synchronously (in the foreground thread) or asynchronously (by a background thread).
The AsyncTaskManager is actually a collection of AsyncTaskChains, each of which maintains a list of tasks. Each chain can be either foreground or background (it may run only in the main thread, or it may be serviced by one or more background threads). See AsyncTaskChain for more information.
If you do not require background processing, it is perfectly acceptable to create only one AsyncTaskChain, which runs in the main thread. This is a common configuration.
Definition at line 48 of file asyncTaskManager.h.
void AsyncTaskManager::add | ( | AsyncTask * | task | ) |
Adds the indicated task to the active queue.
It is an error if the task is already added to this or any other active queue.
Definition at line 181 of file asyncTaskManager.cxx.
Referenced by PandaFramework::get_graphics_engine().
void AsyncTaskManager::cleanup | ( | ) |
Stops all threads and messily empties the task list.
This is intended to be called on destruction only.
Definition at line 59 of file asyncTaskManager.cxx.
References ordered_vector< Key, Compare, Vector >::empty(), ordered_vector< Key, Compare, Vector >::pop_back(), and ordered_vector< Key, Compare, Vector >::size().
AsyncTask * AsyncTaskManager::find_task | ( | const std::string & | name | ) | const |
Returns the first task found with the indicated name, or NULL if there is no task with the indicated name.
If there are multiple tasks with the same name, returns one of them arbitrarily.
Definition at line 253 of file asyncTaskManager.cxx.
References ReferenceCount::local_object().
AsyncTaskChain * AsyncTaskManager::find_task_chain | ( | const std::string & | name | ) |
Searches a new AsyncTaskChain of the indicated name and returns it if it exists, or NULL otherwise.
Definition at line 139 of file asyncTaskManager.cxx.
AsyncTaskCollection AsyncTaskManager::find_tasks | ( | const std::string & | name | ) | const |
Returns the list of tasks found with the indicated name.
Definition at line 269 of file asyncTaskManager.cxx.
References AsyncTaskCollection::add_task(), and ReferenceCount::local_object().
AsyncTaskCollection AsyncTaskManager::find_tasks_matching | ( | const GlobPattern & | pattern | ) | const |
Returns the list of tasks found whose name matches the indicated glob pattern, e.g.
"my_task_*".
Definition at line 288 of file asyncTaskManager.cxx.
References AsyncTaskCollection::add_task(), GlobPattern::get_const_prefix(), ReferenceCount::local_object(), and GlobPattern::matches().
|
inlinestatic |
Returns a pointer to the global AsyncTaskManager.
This is the AsyncTaskManager that most code should use for queueing tasks and suchlike.
Definition at line 50 of file asyncTaskManager.I.
|
inline |
Returns the number of tasks that are currently active or sleeping within the task manager.
Definition at line 40 of file asyncTaskManager.I.
bool AsyncTaskManager::has_task | ( | AsyncTask * | task | ) | const |
Returns true if the indicated task has been added to this AsyncTaskManager, false otherwise.
Definition at line 228 of file asyncTaskManager.cxx.
AsyncTaskChain * AsyncTaskManager::make_task_chain | ( | const std::string & | name | ) |
Creates a new AsyncTaskChain of the indicated name and stores it within the AsyncTaskManager.
If a task chain with this name already exists, returns it instead.
Definition at line 129 of file asyncTaskManager.cxx.
void AsyncTaskManager::poll | ( | ) |
Runs through all the tasks in the task list, once, if the task manager is running in single-threaded mode (no threads available).
This method does nothing in threaded mode, so it may safely be called in either case.
Definition at line 474 of file asyncTaskManager.cxx.
bool AsyncTaskManager::remove | ( | AsyncTask * | task | ) |
Removes the indicated task from the active queue.
Returns true if the task is successfully removed, or false if it wasn't there.
Definition at line 311 of file asyncTaskManager.cxx.
References AsyncTask::remove().
size_t AsyncTaskManager::remove | ( | const AsyncTaskCollection & | tasks | ) |
Removes all of the tasks in the AsyncTaskCollection.
Returns the number of tasks removed.
Definition at line 320 of file asyncTaskManager.cxx.
References AsyncTaskCollection::get_num_tasks.
bool AsyncTaskManager::remove_task_chain | ( | const std::string & | name | ) |
Removes the AsyncTaskChain of the indicated name.
If the chain still has tasks, this will block until all tasks are finished.
Returns true if successful, or false if the chain did not exist.
Definition at line 151 of file asyncTaskManager.cxx.
References ordered_vector< Key, Compare, Vector >::end().
void AsyncTaskManager::start_threads | ( | ) |
Starts any requested threads to service the tasks on the queue.
This is normally not necessary, since adding a task will start the threads automatically.
Definition at line 394 of file asyncTaskManager.cxx.
void AsyncTaskManager::stop_threads | ( | ) |
Stops any threads that are currently running.
If any tasks are still pending and have not yet been picked up by a thread, they will not be serviced unless poll() or start_threads() is later called.
Definition at line 376 of file asyncTaskManager.cxx.
void AsyncTaskManager::wait_for_tasks | ( | ) |
Blocks until the task list is empty.
Definition at line 355 of file asyncTaskManager.cxx.
AsyncTaskCollection AsyncTaskManager::get_active_tasks |
Returns the set of tasks that are active (and not sleeping) on the task manager, at the time of the call.
Definition at line 86 of file asyncTaskManager.h.
|
inline |
Returns the clock pointer used within the AsyncTaskManager.
See set_clock().
Definition at line 57 of file asyncTaskManager.h.
double AsyncTaskManager::get_next_wake_time |
Returns the scheduled time (on the manager's clock) of the next sleeping task, on any task chain, to awaken.
Returns -1 if there are no sleeping tasks.
Definition at line 91 of file asyncTaskManager.h.
int AsyncTaskManager::get_num_task_chains |
Returns the number of different task chains.
Definition at line 61 of file asyncTaskManager.h.
AsyncTaskCollection AsyncTaskManager::get_sleeping_tasks |
Returns the set of tasks that are sleeping (and not active) on the task manager, at the time of the call.
Definition at line 87 of file asyncTaskManager.h.
AsyncTaskChain * AsyncTaskManager::get_task_chain |
Returns the nth task chain.
Definition at line 61 of file asyncTaskManager.h.
AsyncTaskCollection AsyncTaskManager::get_tasks |
Returns the set of tasks that are active or sleeping on the task manager, at the time of the call.
Definition at line 85 of file asyncTaskManager.h.
|
inline |
Replaces the clock pointer used within the AsyncTaskManager.
This is used to control when tasks with a set_delay() specified will be scheduled. It can also be ticked automatically each epoch, if set_tick_clock() is true.
The default is the global clock pointer.
Definition at line 57 of file asyncTaskManager.h.