Panda3D
Classes | Public Member Functions | Static Public Member Functions | Friends | List of all members
AsyncTaskManager Class Reference

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"

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

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...
 
AsyncTaskfind_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...
 
AsyncTaskChainfind_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...
 
ClockObjectget_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...
 
AsyncTaskChainget_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)
 
AsyncTaskChainmake_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
 
- Public Member Functions inherited from TypedReferenceCount
 TypedReferenceCount (const TypedReferenceCount &copy)
 
void operator= (const TypedReferenceCount &copy)
 
- Public Member Functions inherited from TypedObject
 TypedObject (const TypedObject &copy)
 
TypedObjectas_typed_object ()
 Returns the object, upcast (if necessary) to a TypedObject pointer. More...
 
const TypedObjectas_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 &copy)
 
- Public Member Functions inherited from ReferenceCount
int get_ref_count () const
 Returns the current reference count. More...
 
WeakReferenceListget_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...
 
- Public Member Functions inherited from Namable
 Namable (const string &initial_name="")
 
 Namable (const Namable &copy)
 
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...
 
Namableoperator= (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 AsyncTaskManagerget_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 ()
 

Friends

class AsyncTask
 
class AsyncTaskChain
 
class AsyncTaskChain::AsyncTaskChainThread
 
class AsyncTaskSequence
 

Detailed Description

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.

Member Function Documentation

◆ add()

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().

◆ cleanup()

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().

◆ find_task()

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().

◆ find_task_chain()

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().

◆ find_tasks()

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().

◆ find_tasks_matching()

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().

◆ get_active_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().

◆ get_clock()

ClockObject * AsyncTaskManager::get_clock ( )
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().

◆ get_global_ptr()

AsyncTaskManager * AsyncTaskManager::get_global_ptr ( )
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().

◆ get_next_wake_time()

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().

◆ get_num_task_chains()

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().

◆ get_num_tasks()

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.

References get_global_ptr().

Referenced by get_clock().

◆ get_sleeping_tasks()

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().

◆ get_task_chain()

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().

◆ get_tasks()

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().

◆ has_task()

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().

◆ make_task_chain()

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().

◆ poll()

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().

◆ remove() [1/2]

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().

◆ remove() [2/2]

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().

◆ remove_task_chain()

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().

◆ set_clock()

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.

References get_clock().

◆ start_threads()

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().

◆ 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().

◆ 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().


The documentation for this class was generated from the following files: