Panda3D
|
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 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 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 string &name) |
Searches a new AsyncTaskChain of the indicated name and returns it if it exists, or NULL otherwise. More... | |
AsyncTaskCollection | find_tasks (const 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 |
Returns the set of tasks that are active (and not sleeping) on the task manager, at the time of the call. More... | |
ClockObject * | get_clock () |
Returns the clock pointer used within the AsyncTaskManager. More... | |
double | get_next_wake_time () const |
Returns the scheduled time (on the manager's clock) of the next sleeping task, on any task chain, to awaken. More... | |
int | get_num_task_chains () const |
Returns the number of different task chains. More... | |
int | 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 |
Returns the set of tasks that are sleeping (and not active) on the task manager, at the time of the call. More... | |
AsyncTaskChain * | get_task_chain (int n) const |
Returns the nth task chain. More... | |
AsyncTaskCollection | get_tasks () const |
Returns the set of tasks that are active or sleeping on the task manager, at the time of the call. More... | |
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... | |
MAKE_SEQ (get_task_chains, get_num_task_chains, get_task_chain) | |
AsyncTaskChain * | make_task_chain (const string &name) |
Creates a new AsyncTaskChain of the indicated name and stores it within the AsyncTaskManager. More... | |
virtual void | output (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... | |
int | remove (const AsyncTaskCollection &tasks) |
Removes all of the tasks in the AsyncTaskCollection. More... | |
bool | remove_task_chain (const string &name) |
Removes the AsyncTaskChain of the indicated name. More... | |
void | set_clock (ClockObject *clock) |
Replaces the clock pointer used within the AsyncTaskManager. More... | |
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 (ostream &out, int indent_level=0) const |
![]() | |
TypedReferenceCount (const TypedReferenceCount ©) | |
void | operator= (const TypedReferenceCount ©) |
![]() | |
TypedObject (const TypedObject ©) | |
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... | |
void | operator= (const TypedObject ©) |
![]() | |
int | get_ref_count () const |
Returns the current reference count. More... | |
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 | 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... | |
void | weak_ref (WeakPointerToVoid *ptv) |
Adds the indicated PointerToVoid as a weak reference to this object. More... | |
void | weak_unref (WeakPointerToVoid *ptv) |
Removes the indicated PointerToVoid as a weak reference to this object. More... | |
![]() | |
Namable (const string &initial_name="") | |
Namable (const Namable ©) | |
void | clear_name () |
Resets the Namable's name to empty. More... | |
const 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... | |
Namable & | operator= (const Namable &other) |
void | output (ostream &out) const |
Outputs the Namable. More... | |
void | set_name (const 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 TypeHandle | get_class_type () |
static void | init_type () |
![]() | |
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 TypeHandle | get_class_type () |
static void | init_type () |
![]() | |
static TypeHandle | get_class_type () |
static void | init_type () |
Friends | |
class | AsyncTask |
class | AsyncTaskChain |
class | AsyncTaskChain::AsyncTaskChainThread |
class | AsyncTaskSequence |
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 54 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 203 of file asyncTaskManager.cxx.
References has_task().
Referenced by NonlinearImager::add_viewer(), PandaFramework::get_graphics_engine(), and remove_task_chain().
void AsyncTaskManager::cleanup | ( | ) |
Stops all threads and messily empties the task list.
This is intended to be called on destruction only.
Definition at line 64 of file asyncTaskManager.cxx.
References get_num_task_chains().
AsyncTask * AsyncTaskManager::find_task | ( | const 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 279 of file asyncTaskManager.cxx.
References find_tasks(), and ReferenceCount::local_object().
Referenced by has_task().
AsyncTaskChain * AsyncTaskManager::find_task_chain | ( | const string & | name | ) |
Searches a new AsyncTaskChain of the indicated name and returns it if it exists, or NULL otherwise.
Definition at line 154 of file asyncTaskManager.cxx.
References remove_task_chain().
Referenced by make_task_chain().
AsyncTaskCollection AsyncTaskManager::find_tasks | ( | const string & | name | ) | const |
Returns the list of tasks found with the indicated name.
Definition at line 298 of file asyncTaskManager.cxx.
References AsyncTaskCollection::add_task(), find_tasks_matching(), and ReferenceCount::local_object().
Referenced by find_task().
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 319 of file asyncTaskManager.cxx.
References AsyncTaskCollection::add_task(), GlobPattern::get_const_prefix(), ReferenceCount::local_object(), GlobPattern::matches(), and remove().
Referenced by find_tasks().
AsyncTaskCollection AsyncTaskManager::get_active_tasks | ( | ) | const |
Returns the set of tasks that are active (and not sleeping) on the task manager, at the time of the call.
Definition at line 490 of file asyncTaskManager.cxx.
References AsyncTaskCollection::add_tasks_from(), and get_sleeping_tasks().
Referenced by get_tasks().
|
inline |
Returns the clock pointer used within the AsyncTaskManager.
See set_clock().
Definition at line 39 of file asyncTaskManager.I.
References get_num_tasks().
Referenced by set_clock().
|
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 63 of file asyncTaskManager.I.
Referenced by NonlinearImager::add_viewer(), get_num_tasks(), and GraphicsEngine::remove_all_windows().
double AsyncTaskManager::get_next_wake_time | ( | ) | const |
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 562 of file asyncTaskManager.cxx.
Referenced by poll().
int AsyncTaskManager::get_num_task_chains | ( | ) | const |
Returns the number of different task chains.
Definition at line 116 of file asyncTaskManager.cxx.
References get_task_chain().
Referenced by cleanup().
|
inline |
Returns the number of tasks that are currently active or sleeping within the task manager.
Definition at line 50 of file asyncTaskManager.I.
References get_global_ptr().
Referenced by get_clock().
AsyncTaskCollection AsyncTaskManager::get_sleeping_tasks | ( | ) | const |
Returns the set of tasks that are sleeping (and not active) on the task manager, at the time of the call.
Definition at line 513 of file asyncTaskManager.cxx.
References AsyncTaskCollection::add_tasks_from(), and poll().
Referenced by get_active_tasks().
AsyncTaskChain * AsyncTaskManager::get_task_chain | ( | int | n | ) | const |
Returns the nth task chain.
Definition at line 127 of file asyncTaskManager.cxx.
References make_task_chain().
Referenced by get_num_task_chains().
AsyncTaskCollection AsyncTaskManager::get_tasks | ( | ) | const |
Returns the set of tasks that are active or sleeping on the task manager, at the time of the call.
Definition at line 466 of file asyncTaskManager.cxx.
References AsyncTaskCollection::add_tasks_from(), and get_active_tasks().
Referenced by start_threads().
bool AsyncTaskManager::has_task | ( | AsyncTask * | task | ) | const |
Returns true if the indicated task has been added to this AsyncTaskManager, false otherwise.
Definition at line 252 of file asyncTaskManager.cxx.
References find_task().
Referenced by add().
AsyncTaskChain * AsyncTaskManager::make_task_chain | ( | const 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 142 of file asyncTaskManager.cxx.
References find_task_chain().
Referenced by get_task_chain().
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 538 of file asyncTaskManager.cxx.
References get_next_wake_time().
Referenced by get_sleeping_tasks().
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 345 of file asyncTaskManager.cxx.
References AsyncTaskCollection::add_task().
Referenced by find_tasks_matching().
int AsyncTaskManager::remove | ( | const AsyncTaskCollection & | tasks | ) |
Removes all of the tasks in the AsyncTaskCollection.
Returns the number of tasks removed.
Definition at line 361 of file asyncTaskManager.cxx.
References AsyncTaskCollection::get_num_tasks(), AsyncTaskCollection::get_task(), and wait_for_tasks().
bool AsyncTaskManager::remove_task_chain | ( | const 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 170 of file asyncTaskManager.cxx.
References add().
Referenced by find_task_chain().
|
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 28 of file asyncTaskManager.I.
References get_clock().
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 446 of file asyncTaskManager.cxx.
References get_tasks().
Referenced by stop_threads().
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 426 of file asyncTaskManager.cxx.
References start_threads().
Referenced by GraphicsEngine::remove_all_windows(), and wait_for_tasks().
void AsyncTaskManager::wait_for_tasks | ( | ) |
Blocks until the task list is empty.
Definition at line 402 of file asyncTaskManager.cxx.
References stop_threads().
Referenced by remove().