Panda3D
Classes | Public Member Functions | Static Public Member Functions | Protected Types | Protected Member Functions | Static Protected Member Functions | Protected Attributes | Static Protected Attributes | Friends

AsyncTaskChain Class Reference

The AsyncTaskChain is a subset of the AsyncTaskManager. More...

#include "asyncTaskChain.h"

Inheritance diagram for AsyncTaskChain:
TypedReferenceCount Namable TypedObject ReferenceCount MemoryBase MemoryBase MemoryBase

List of all members.

Classes

class  AsyncTaskChainThread
class  AsyncTaskSortPriority
class  AsyncTaskSortWakeTime

Public Member Functions

 AsyncTaskChain (AsyncTaskManager *manager, const string &name)
virtual TypeHandle force_init_type ()
AsyncTaskCollection get_active_tasks () const
 Returns the set of tasks that are active (and not sleeping) on the task chain, at the time of the call.
double get_frame_budget () const
 Returns the maximum amount of time per frame the tasks on this chain are granted for execution.
bool get_frame_sync () const
 Returns the frame_sync flag.
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_running_threads () const
 Returns the number of threads that have been created and are actively running.
int get_num_tasks () const
 Returns the number of tasks that are currently active or sleeping within the task chain.
int get_num_threads () const
 Returns the number of threads that will be servicing tasks for this chain.
AsyncTaskCollection get_sleeping_tasks () const
 Returns the set of tasks that are sleeping (and not active) on the task chain, at the time of the call.
AsyncTaskCollection get_tasks () const
 Returns the set of tasks that are active or sleeping on the task chain, at the time of the call.
ThreadPriority get_thread_priority () const
 Returns the priority associated with threads that serve this task chain.
bool get_tick_clock () const
 Returns the tick_clock flag.
bool get_timeslice_priority () const
 Returns the timeslice_priority flag.
virtual TypeHandle get_type () const
bool has_task (AsyncTask *task) const
 Returns true if the indicated task has been added to this AsyncTaskChain, false otherwise.
bool is_started () const
 Returns true if the thread(s) have been started and are ready to service requests, false otherwise.
virtual void output (ostream &out) const
 Outputs the Namable.
void poll ()
 Runs through all the tasks in the task list, once, if the task chain is running in single-threaded mode (no threads available).
void set_frame_budget (double frame_budget)
 Sets the maximum amount of time per frame the tasks on this chain are granted for execution.
void set_frame_sync (bool frame_sync)
 Sets the frame_sync flag.
void set_num_threads (int num_threads)
 Changes the number of threads for this task chain.
void set_thread_priority (ThreadPriority priority)
 Changes the priority associated with threads that serve this task chain.
void set_tick_clock (bool tick_clock)
 Sets the tick_clock flag.
void set_timeslice_priority (bool timeslice_priority)
 Sets the timeslice_priority flag.
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 void init_type ()

Protected Types

enum  State { S_initial, S_started, S_interrupted, S_shutdown }
typedef pvector< PT(AsyncTask) > TaskHeap
typedef pvector< PT(AsyncTaskChainThread) > Threads

Protected Member Functions

void cleanup_pickup_mode ()
 Clean up the damage from setting pickup mode.
void cleanup_task (AsyncTask *task, bool upon_death, bool clean_exit)
 Called internally when a task has completed (or been interrupted) and is about to be removed from the active queue.
void do_add (AsyncTask *task)
 Adds the indicated task to the active queue.
void do_cleanup ()
 Stops all threads and messily empties the task list.
AsyncTaskCollection do_get_active_tasks () const
 Returns the set of tasks that are active (and not sleeping) on the task chain, at the time of the call.
double do_get_next_wake_time () const
 Returns the time at which the next sleeping thread will awaken, or -1 if there are no sleeping threads.
AsyncTaskCollection do_get_sleeping_tasks () const
 Returns the set of tasks that are sleeping (and not active) on the task chain, at the time of the call.
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).
void do_output (ostream &out) const
 The private implementation of output(), this assumes the lock is already held.
void do_poll ()
 The private implementation of poll(), this assumes the lock is already held.
bool do_remove (AsyncTask *task)
 Removes the indicated task from this chain.
void do_start_threads ()
 The private implementation of start_threads; assumes the lock is already held.
void do_stop_threads ()
 The private implementation of stop_threads; assumes the lock is already held.
void do_wait_for_tasks ()
 Blocks until the task list is empty.
void do_write (ostream &out, int indent_level) const
 The private implementation of write(), this assumes the lock is already held.
void filter_timeslice_priority ()
 Called to filter the _active tasks list when we are in the special timeslice_priority mode.
int find_task_on_heap (const TaskHeap &heap, AsyncTask *task) const
 Returns the index number of the indicated task within the specified task list, or -1 if the task is not found in the list (this may mean that it is currently being serviced).
bool finish_sort_group ()
 Called internally when all tasks of a given sort value have been completed, and it is time to increment to the next sort value, or begin the next epoch.
void service_one_task (AsyncTaskChainThread *thread)
 Pops a single task off the active queue, services it, and restores it to the end of the queue.
void write_task_line (ostream &out, int indent_level, AsyncTask *task, double now) const
 Writes a single line for a task in the report generated by do_write().

Static Protected Member Functions

static double get_wake_time (AsyncTask *task)
 Returns the time at which the indicated thread will awaken.

Protected Attributes

TaskHeap _active
bool _block_till_next_frame
int _current_frame
int _current_sort
ConditionVarFull _cvar
double _frame_budget
bool _frame_sync
AsyncTaskManager_manager
bool _needs_cleanup
TaskHeap _next_active
int _num_busy_threads
int _num_tasks
int _num_threads
bool _pickup_mode
TaskHeap _sleeping
State _state
TaskHeap _this_active
ThreadPriority _thread_priority
Threads _threads
bool _tick_clock
double _time_in_frame
bool _timeslice_priority

Static Protected Attributes

static PStatCollector _task_pcollector
static PStatCollector _wait_pcollector

Friends

class AsyncTask
class AsyncTaskChainThread
class AsyncTaskManager
class AsyncTaskSortWakeTime

Detailed Description

The AsyncTaskChain is a subset of the AsyncTaskManager.

Each chain maintains a separate list of tasks, and will execute them with its own set of threads. Each chain may thereby operate independently of the other chains.

The AsyncTaskChain will spawn a specified number of threads (possibly 0) to serve the tasks. If there are no threads, you must call poll() from time to time to serve the tasks in the main thread. Normally this is done by calling AsyncTaskManager::poll().

Each task will run exactly once each epoch. Beyond that, the tasks' sort and priority values control the order in which they are run: tasks are run in increasing order by sort value, and within the same sort value, they are run roughly in decreasing order by priority value, with some exceptions for parallelism. Tasks with different sort values are never run in parallel together, but tasks with different priority values might be (if there is more than one thread).

Definition at line 57 of file asyncTaskChain.h.


Member Function Documentation

void AsyncTaskChain::cleanup_pickup_mode ( ) [protected]

Clean up the damage from setting pickup mode.

This means we restore the _active and _next_active lists as they should have been without pickup mode, for next frame. Assumes the lock is held.

Definition at line 1327 of file asyncTaskChain.cxx.

References finish_sort_group().

Referenced by do_poll(), and do_stop_threads().

void AsyncTaskChain::cleanup_task ( AsyncTask task,
bool  upon_death,
bool  clean_exit 
) [protected]

Called internally when a task has completed (or been interrupted) and is about to be removed from the active queue.

Assumes the lock is held.

If upon_death is true, then task->upon_death() will also be called, with the indicated clean_exit parameter.

Note that the lock may be temporarily released by this method.

Definition at line 868 of file asyncTaskChain.cxx.

References MutexDirect::acquire(), do_output(), MutexDirect::release(), AsyncTaskManager::remove_task_by_name(), and AsyncTask::upon_death().

Referenced by do_cleanup(), do_remove(), and service_one_task().

void AsyncTaskChain::do_add ( AsyncTask task) [protected]

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.

This is normally called only by the AsyncTaskManager. Assumes the lock is already held.

Definition at line 486 of file asyncTaskChain.cxx.

References AsyncTaskManager::add_task_by_name(), do_has_task(), do_start_threads(), AsyncTask::get_delay(), AsyncTask::get_sort(), AsyncTask::has_delay(), and ConditionVarFullDirect::notify_all().

Referenced by AsyncTaskManager::add(), AsyncTask::jump_to_task_chain(), AsyncTask::set_priority(), and AsyncTask::set_sort().

void AsyncTaskChain::do_cleanup ( ) [protected]

Stops all threads and messily empties the task list.

This is intended to be called on destruction only. Assumes the lock is already held.

Definition at line 641 of file asyncTaskChain.cxx.

References MutexDirect::acquire(), cleanup_task(), do_output(), do_stop_threads(), and MutexDirect::release().

AsyncTaskCollection AsyncTaskChain::do_get_active_tasks ( ) const [protected]

Returns the set of tasks that are active (and not sleeping) on the task chain, at the time of the call.

Assumes the lock is held.

Definition at line 1205 of file asyncTaskChain.cxx.

References AsyncTaskCollection::add_task().

Referenced by AsyncTaskManager::get_active_tasks(), get_active_tasks(), get_tasks(), and AsyncTaskManager::get_tasks().

double AsyncTaskChain::do_get_next_wake_time ( ) const [inline, protected]

Returns the time at which the next sleeping thread will awaken, or -1 if there are no sleeping threads.

Assumes the lock is already held.

Definition at line 37 of file asyncTaskChain.I.

Referenced by AsyncTaskManager::get_next_wake_time(), and get_next_wake_time().

AsyncTaskCollection AsyncTaskChain::do_get_sleeping_tasks ( ) const [protected]

Returns the set of tasks that are sleeping (and not active) on the task chain, at the time of the call.

Assumes the lock is held.

Definition at line 1240 of file asyncTaskChain.cxx.

References AsyncTaskCollection::add_task().

Referenced by AsyncTaskManager::get_sleeping_tasks(), get_sleeping_tasks(), get_tasks(), and AsyncTaskManager::get_tasks().

bool AsyncTaskChain::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 710 of file asyncTaskChain.cxx.

References find_task_on_heap().

Referenced by do_add(), AsyncTaskManager::do_has_task(), and has_task().

void AsyncTaskChain::do_output ( ostream &  out) const [protected]

The private implementation of output(), this assumes the lock is already held.

Definition at line 1349 of file asyncTaskChain.cxx.

Referenced by cleanup_task(), do_cleanup(), finish_sort_group(), output(), and set_num_threads().

void AsyncTaskChain::do_poll ( ) [protected]

The private implementation of poll(), this assumes the lock is already held.

Definition at line 1259 of file asyncTaskChain.cxx.

References cleanup_pickup_mode(), do_start_threads(), finish_sort_group(), ConditionVarFullDirect::notify_all(), and service_one_task().

Referenced by do_wait_for_tasks(), AsyncTaskManager::poll(), and poll().

bool AsyncTaskChain::do_remove ( AsyncTask task) [protected]

Removes the indicated task from this chain.

Returns true if removed, false otherwise. Assumes the lock is already held. The task->upon_death() method is not* called.

Definition at line 546 of file asyncTaskChain.cxx.

References cleanup_task(), and find_task_on_heap().

Referenced by AsyncTask::set_priority(), AsyncTask::set_sort(), and AsyncTask::set_task_chain().

void AsyncTaskChain::do_start_threads ( ) [protected]

The private implementation of start_threads; assumes the lock is already held.

Definition at line 1170 of file asyncTaskChain.cxx.

References do_stop_threads(), and Thread::is_threading_supported().

Referenced by do_add(), do_poll(), do_wait_for_tasks(), set_num_threads(), set_thread_priority(), AsyncTaskManager::start_threads(), and start_threads().

void AsyncTaskChain::do_stop_threads ( ) [protected]
void AsyncTaskChain::do_wait_for_tasks ( ) [protected]

Blocks until the task list is empty.

Assumes the lock is held.

Definition at line 608 of file asyncTaskChain.cxx.

References do_poll(), do_start_threads(), and ConditionVarFullDirect::wait().

Referenced by AsyncTaskManager::wait_for_tasks(), and wait_for_tasks().

void AsyncTaskChain::do_write ( ostream &  out,
int  indent_level 
) const [protected]

The private implementation of write(), this assumes the lock is already held.

Definition at line 1366 of file asyncTaskChain.cxx.

References write_task_line().

void AsyncTaskChain::filter_timeslice_priority ( ) [protected]

Called to filter the _active tasks list when we are in the special timeslice_priority mode.

In this mode, go through and postpone any tasks that have already exceeded their priority budget for this epoch.

Assumes the lock is already held.

Definition at line 1045 of file asyncTaskChain.cxx.

References AsyncTask::get_average_dt().

Referenced by finish_sort_group().

int AsyncTaskChain::find_task_on_heap ( const TaskHeap &  heap,
AsyncTask task 
) const [protected]

Returns the index number of the indicated task within the specified task list, or -1 if the task is not found in the list (this may mean that it is currently being serviced).

Assumes that the lock is currently held.

Definition at line 727 of file asyncTaskChain.cxx.

Referenced by do_has_task(), and do_remove().

bool AsyncTaskChain::finish_sort_group ( ) [protected]

Called internally when all tasks of a given sort value have been completed, and it is time to increment to the next sort value, or begin the next epoch.

Assumes the lock is held.

Returns true if there are more tasks on the queue after this operation, or false if the task list is empty and we need to wait.

Definition at line 907 of file asyncTaskChain.cxx.

References do_output(), filter_timeslice_priority(), and ConditionVarFullDirect::notify_all().

Referenced by cleanup_pickup_mode(), and do_poll().

AsyncTaskCollection AsyncTaskChain::get_active_tasks ( ) const

Returns the set of tasks that are active (and not sleeping) on the task chain, at the time of the call.

Definition at line 403 of file asyncTaskChain.cxx.

References do_get_active_tasks().

double AsyncTaskChain::get_frame_budget ( ) const

Returns the maximum amount of time per frame the tasks on this chain are granted for execution.

See set_frame_budget().

Definition at line 216 of file asyncTaskChain.cxx.

bool AsyncTaskChain::get_frame_sync ( ) const

Returns the frame_sync flag.

See set_frame_sync().

Definition at line 253 of file asyncTaskChain.cxx.

double AsyncTaskChain::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 448 of file asyncTaskChain.cxx.

References do_get_next_wake_time().

int AsyncTaskChain::get_num_running_threads ( ) const

Returns the number of threads that have been created and are actively running.

This will return 0 before the threads have been started; it will also return 0 if thread support is not available.

Definition at line 153 of file asyncTaskChain.cxx.

int AsyncTaskChain::get_num_tasks ( ) const

Returns the number of tasks that are currently active or sleeping within the task chain.

Definition at line 376 of file asyncTaskChain.cxx.

int AsyncTaskChain::get_num_threads ( ) const

Returns the number of threads that will be servicing tasks for this chain.

Also see get_num_running_threads().

Definition at line 139 of file asyncTaskChain.cxx.

AsyncTaskCollection AsyncTaskChain::get_sleeping_tasks ( ) const

Returns the set of tasks that are sleeping (and not active) on the task chain, at the time of the call.

Definition at line 416 of file asyncTaskChain.cxx.

References do_get_sleeping_tasks().

AsyncTaskCollection AsyncTaskChain::get_tasks ( ) const

Returns the set of tasks that are active or sleeping on the task chain, at the time of the call.

Definition at line 388 of file asyncTaskChain.cxx.

References AsyncTaskCollection::add_tasks_from(), do_get_active_tasks(), and do_get_sleeping_tasks().

ThreadPriority AsyncTaskChain::get_thread_priority ( ) const

Returns the priority associated with threads that serve this task chain.

Definition at line 185 of file asyncTaskChain.cxx.

bool AsyncTaskChain::get_tick_clock ( ) const

Returns the tick_clock flag.

See set_tick_clock().

Definition at line 94 of file asyncTaskChain.cxx.

bool AsyncTaskChain::get_timeslice_priority ( ) const

Returns the timeslice_priority flag.

This changes the interpretation of priority, and the number of times per epoch each task will run. See set_timeslice_priority().

Definition at line 297 of file asyncTaskChain.cxx.

double AsyncTaskChain::get_wake_time ( AsyncTask task) [inline, static, protected]

Returns the time at which the indicated thread will awaken.

Assumes the lock is already held.

Definition at line 51 of file asyncTaskChain.I.

bool AsyncTaskChain::has_task ( AsyncTask task) const

Returns true if the indicated task has been added to this AsyncTaskChain, false otherwise.

Definition at line 341 of file asyncTaskChain.cxx.

References do_has_task().

bool AsyncTaskChain::is_started ( ) const [inline]

Returns true if the thread(s) have been started and are ready to service requests, false otherwise.

If this is false, the next call to add() or add_and_do() will automatically start the threads.

Definition at line 25 of file asyncTaskChain.I.

void AsyncTaskChain::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 459 of file asyncTaskChain.cxx.

References do_output().

void AsyncTaskChain::poll ( )

Runs through all the tasks in the task list, once, if the task chain 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.

Normally, you would not call this function directly; instead, call AsyncTaskManager::poll(), which polls all of the task chains in sequence.

Definition at line 435 of file asyncTaskChain.cxx.

References do_poll().

void AsyncTaskChain::service_one_task ( AsyncTaskChain::AsyncTaskChainThread thread) [protected]

Pops a single task off the active queue, services it, and restores it to the end of the queue.

This is called internally only within one of the task threads. Assumes the lock is already held.

Note that the lock may be temporarily released by this method.

Definition at line 749 of file asyncTaskChain.cxx.

References cleanup_task(), Thread::get_current_thread(), and ConditionVarFullDirect::notify_all().

Referenced by do_poll().

void AsyncTaskChain::set_frame_budget ( double  frame_budget)

Sets the maximum amount of time per frame the tasks on this chain are granted for execution.

If this is less than zero, there is no limit; if it is >= 0, it represents a maximum amount of time (in seconds) that will be used to execute tasks. If this time is exceeded in any one frame, the task chain will stop executing tasks until the next frame, as defined by the TaskManager's clock.

Definition at line 203 of file asyncTaskChain.cxx.

void AsyncTaskChain::set_frame_sync ( bool  frame_sync)

Sets the frame_sync flag.

When this flag is true, this task chain will be forced to sync with the TaskManager's clock. It will run no faster than one epoch per clock frame.

When this flag is false, the default, the task chain will finish all of its tasks and then immediately start from the first task again, regardless of the clock frame. When it is true, the task chain will finish all of its tasks and then wait for the clock to tick to the next frame before resuming the first task.

This only makes sense for threaded task chains. Non-threaded task chains are automatically synchronous.

Definition at line 242 of file asyncTaskChain.cxx.

void AsyncTaskChain::set_num_threads ( int  num_threads)

Changes the number of threads for this task chain.

This may require stopping the threads if they are already running.

Definition at line 107 of file asyncTaskChain.cxx.

References do_output(), do_start_threads(), do_stop_threads(), and Thread::is_threading_supported().

void AsyncTaskChain::set_thread_priority ( ThreadPriority  priority)

Changes the priority associated with threads that serve this task chain.

This may require stopping the threads if they are already running.

Definition at line 166 of file asyncTaskChain.cxx.

References do_start_threads(), and do_stop_threads().

void AsyncTaskChain::set_tick_clock ( bool  tick_clock)

Sets the tick_clock flag.

When this is true, get_clock()->tick() will be called automatically at each task epoch. This is false by default.

Definition at line 83 of file asyncTaskChain.cxx.

void AsyncTaskChain::set_timeslice_priority ( bool  timeslice_priority)

Sets the timeslice_priority flag.

This changes the interpretation of priority, and the number of times per epoch each task will run.

When this flag is true, some tasks might not run in any given epoch. Instead, tasks with priority higher than 1 will be given precedence, in proportion to the amount of time they have already used. This gives higher-priority tasks more runtime than lower-priority tasks. Each task gets the amount of time proportional to its priority value, so a task with priority 100 will get five times as much processing time as a task with priority 20. For these purposes, priority values less than 1 are deemed to be equal to 1.

When this flag is false (the default), all tasks are run exactly once each epoch, round-robin style. Priority is only used to determine which task runs first within tasks of the same sort value.

Definition at line 283 of file asyncTaskChain.cxx.

void AsyncTaskChain::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 327 of file asyncTaskChain.cxx.

References do_start_threads().

void AsyncTaskChain::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 311 of file asyncTaskChain.cxx.

References do_stop_threads().

void AsyncTaskChain::wait_for_tasks ( )

Blocks until the task list is empty.

Definition at line 364 of file asyncTaskChain.cxx.

References do_wait_for_tasks().

void AsyncTaskChain::write_task_line ( ostream &  out,
int  indent_level,
AsyncTask task,
double  now 
) const [protected]

Writes a single line for a task in the report generated by do_write().

Assumes the lock is already held.

Definition at line 1454 of file asyncTaskChain.cxx.

References AsyncTask::get_average_dt().

Referenced by do_write().


The documentation for this class was generated from the following files:
 All Classes Functions Variables Enumerations