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"
Classes | |
class | AsyncTaskSortName |
Public Member Functions | |
AsyncTaskManager (const string &name) | |
void | add (AsyncTask *task) |
Adds the indicated task to the active queue. | |
void | cleanup () |
Stops all threads and messily empties the task list. | |
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. | |
AsyncTaskChain * | find_task_chain (const string &name) |
Searches a new AsyncTaskChain of the indicated name and returns it if it exists, or NULL otherwise. | |
AsyncTaskCollection | find_tasks (const string &name) const |
Returns the list of tasks found with the indicated name. | |
AsyncTaskCollection | find_tasks_matching (const GlobPattern &pattern) const |
Returns the list of tasks found whose name matches the indicated glob pattern, e.g. | |
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. | |
ClockObject * | get_clock () |
Returns the clock pointer used within the AsyncTaskManager. | |
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. | |
int | get_num_task_chains () const |
Returns the number of different task chains. | |
int | get_num_tasks () const |
Returns the number of tasks that are currently active or sleeping within the task manager. | |
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. | |
AsyncTaskChain * | get_task_chain (int n) const |
Returns the nth task chain. | |
AsyncTaskCollection | get_tasks () const |
Returns the set of tasks that are active or sleeping on the task manager, at the time of the call. | |
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. | |
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. | |
virtual void | output (ostream &out) const |
Outputs the Namable. | |
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). | |
bool | remove (AsyncTask *task) |
Removes the indicated task from the active queue. | |
int | remove (const AsyncTaskCollection &tasks) |
Removes all of the tasks in the AsyncTaskCollection. | |
bool | remove_task_chain (const string &name) |
Removes the AsyncTaskChain of the indicated name. | |
void | set_clock (ClockObject *clock) |
Replaces the clock pointer used within the AsyncTaskManager. | |
void | start_threads () |
Starts any requested threads to service the tasks on the queue. | |
void | stop_threads () |
Stops any threads that are currently running. | |
void | wait_for_tasks () |
Blocks until the task list is empty. | |
virtual void | write (ostream &out, int indent_level=0) const |
Static Public Member Functions | |
static TypeHandle | get_class_type () |
static AsyncTaskManager * | get_global_ptr () |
Returns a pointer to the global AsyncTaskManager. | |
static void | init_type () |
Protected Types | |
typedef ov_set< PT(AsyncTaskChain), IndirectCompareNames < AsyncTaskChain > > | TaskChains |
typedef pmultiset< AsyncTask *, AsyncTaskSortName > | TasksByName |
Protected Member Functions | |
void | add_task_by_name (AsyncTask *task) |
Adds the task to the _tasks_by_name index, if it has a nonempty name. | |
AsyncTaskChain * | do_find_task_chain (const string &name) |
Searches a new AsyncTaskChain of the indicated name and returns it if it exists, or NULL otherwise. | |
bool | do_has_task (AsyncTask *task) const |
Returns true if the task is on one of the task lists, false if it is not (false may mean that the task is currently being serviced). | |
AsyncTaskChain * | do_make_task_chain (const string &name) |
Creates a new AsyncTaskChain of the indicated name and stores it within the AsyncTaskManager. | |
virtual void | do_output (ostream &out) const |
PT (ClockObject) _clock | |
void | remove_task_by_name (AsyncTask *task) |
Removes the task from the _tasks_by_name index, if it has a nonempty name. | |
Static Protected Member Functions | |
static | PT (AsyncTaskManager) _global_ptr |
Protected Attributes | |
ConditionVarFull | _frame_cvar |
Mutex | _lock |
int | _num_tasks |
TaskChains | _task_chains |
TasksByName | _tasks_by_name |
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 MutexDirect::acquire(), AsyncTaskChain::do_add(), do_find_task_chain(), do_has_task(), do_make_task_chain(), AsyncTask::is_runnable(), MutexDirect::release(), and AsyncTask::upon_birth().
Referenced by NonlinearImager::add_viewer(), PandaFramework::get_graphics_engine(), and PandaFramework::open_framework().
void AsyncTaskManager::add_task_by_name | ( | AsyncTask * | task | ) | [inline, protected] |
Adds the task to the _tasks_by_name index, if it has a nonempty name.
Definition at line 77 of file asyncTaskManager.I.
Referenced by AsyncTaskChain::do_add().
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 ordered_vector< Key, Compare >::empty(), ordered_vector< Key, Compare >::pop_back(), and ordered_vector< Key, Compare >::size().
AsyncTaskChain * AsyncTaskManager::do_find_task_chain | ( | const string & | name | ) | [protected] |
Searches a new AsyncTaskChain of the indicated name and returns it if it exists, or NULL otherwise.
Assumes the lock is held.
Definition at line 648 of file asyncTaskManager.cxx.
References ordered_vector< Key, Compare >::end().
Referenced by add(), find_task_chain(), AsyncTask::jump_to_task_chain(), AsyncTask::set_priority(), and AsyncTask::set_sort().
bool AsyncTaskManager::do_has_task | ( | AsyncTask * | task | ) | const [protected] |
Returns true if the task is on one of the task lists, false if it is not (false may mean that the task is currently being serviced).
Assumes the lock is currently held.
Definition at line 698 of file asyncTaskManager.cxx.
References ordered_vector< Key, Compare >::begin(), AsyncTaskChain::do_has_task(), and ordered_vector< Key, Compare >::end().
Referenced by add(), has_task(), and remove().
AsyncTaskChain * AsyncTaskManager::do_make_task_chain | ( | const string & | name | ) | [protected] |
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.
Assumes the lock is held.
Definition at line 632 of file asyncTaskManager.cxx.
Referenced by add(), AsyncTask::jump_to_task_chain(), and make_task_chain().
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 ReferenceCount::local_object().
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 do_find_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(), 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 319 of file asyncTaskManager.cxx.
References AsyncTaskCollection::add_task(), GlobPattern::get_const_prefix(), ReferenceCount::local_object(), and GlobPattern::matches().
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(), ordered_vector< Key, Compare >::begin(), AsyncTaskChain::do_get_active_tasks(), and ordered_vector< Key, Compare >::end().
ClockObject * AsyncTaskManager::get_clock | ( | ) | [inline] |
Returns the clock pointer used within the AsyncTaskManager.
See set_clock().
Definition at line 39 of file asyncTaskManager.I.
Referenced by AsyncTask::unlock_and_do_task().
AsyncTaskManager * AsyncTaskManager::get_global_ptr | ( | ) | [inline, static] |
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(), 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.
References ordered_vector< Key, Compare >::begin(), AsyncTaskChain::do_get_next_wake_time(), and ordered_vector< Key, Compare >::end().
int AsyncTaskManager::get_num_task_chains | ( | ) | const |
Returns the number of different task chains.
Definition at line 116 of file asyncTaskManager.cxx.
References ordered_vector< Key, Compare >::size().
int AsyncTaskManager::get_num_tasks | ( | ) | const [inline] |
Returns the number of tasks that are currently active or sleeping within the task manager.
Definition at line 50 of file asyncTaskManager.I.
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(), ordered_vector< Key, Compare >::begin(), AsyncTaskChain::do_get_sleeping_tasks(), and ordered_vector< Key, Compare >::end().
AsyncTaskChain * AsyncTaskManager::get_task_chain | ( | int | n | ) | const |
Returns the nth task chain.
Definition at line 127 of file asyncTaskManager.cxx.
References ordered_vector< Key, Compare >::size().
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(), ordered_vector< Key, Compare >::begin(), AsyncTaskChain::do_get_active_tasks(), AsyncTaskChain::do_get_sleeping_tasks(), and ordered_vector< Key, Compare >::end().
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 do_has_task().
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 do_make_task_chain().
void AsyncTaskManager::output | ( | ostream & | out | ) | const [virtual] |
Outputs the Namable.
This function simply writes the name to the output stream; most Namable derivatives will probably redefine this.
Reimplemented from Namable.
Definition at line 593 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 538 of file asyncTaskManager.cxx.
References AsyncTaskChain::do_poll(), ConditionVarFullDirect::notify_all(), and ordered_vector< Key, Compare >::size().
Referenced by PandaFramework::do_frame().
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 MutexDirect::acquire(), do_has_task(), AsyncTaskCollection::get_num_tasks(), AsyncTaskCollection::get_task(), and MutexDirect::release().
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 AsyncTask::remove().
void AsyncTaskManager::remove_task_by_name | ( | AsyncTask * | task | ) | [protected] |
Removes the task from the _tasks_by_name index, if it has a nonempty name.
Definition at line 666 of file asyncTaskManager.cxx.
Referenced by AsyncTaskChain::cleanup_task().
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 ordered_vector< Key, Compare >::end().
void AsyncTaskManager::set_clock | ( | ClockObject * | clock | ) | [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.
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 AsyncTaskChain::do_start_threads(), and ordered_vector< Key, Compare >::size().
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 AsyncTaskChain::do_stop_threads(), and ordered_vector< Key, Compare >::size().
Referenced by GraphicsEngine::remove_all_windows().
void AsyncTaskManager::wait_for_tasks | ( | ) |
Blocks until the task list is empty.
Definition at line 402 of file asyncTaskManager.cxx.
References AsyncTaskChain::do_wait_for_tasks(), and ordered_vector< Key, Compare >::size().