Panda3D
Public Member Functions | Static Public Member Functions | Public Attributes | 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...

Inheritance diagram for AsyncTaskManager:
TypedReferenceCount Namable TypedObject ReferenceCount

Public Member Functions

 __init__ (str name)
 
 add (AsyncTask task)
 Adds the indicated task to the active queue. More...
 
 cleanup ()
 Stops all threads and messily empties the task list. More...
 
AsyncTask findTask (str name)
 Returns the first task found with the indicated name, or NULL if there is no task with the indicated name. More...
 
AsyncTaskChain findTaskChain (str name)
 Searches a new AsyncTaskChain of the indicated name and returns it if it exists, or NULL otherwise. More...
 
AsyncTaskCollection findTasks (str name)
 Returns the list of tasks found with the indicated name. More...
 
AsyncTaskCollection findTasksMatching (const GlobPattern pattern)
 Returns the list of tasks found whose name matches the indicated glob pattern, e.g. More...
 
AsyncTaskCollection getActiveTasks ()
 Returns the set of tasks that are active (and not sleeping) on the task manager, at the time of the call. More...
 
ClockObject getClock ()
 Returns the clock pointer used within the AsyncTaskManager. More...
 
double getNextWakeTime ()
 Returns the scheduled time (on the manager's clock) of the next sleeping task, on any task chain, to awaken. More...
 
int getNumTaskChains ()
 Returns the number of different task chains. More...
 
int getNumTasks ()
 Returns the number of tasks that are currently active or sleeping within the task manager. More...
 
AsyncTaskCollection getSleepingTasks ()
 Returns the set of tasks that are sleeping (and not active) on the task manager, at the time of the call. More...
 
AsyncTaskChain getTaskChain (int n)
 Returns the nth task chain. More...
 
list getTaskChains ()
 
AsyncTaskCollection getTasks ()
 Returns the set of tasks that are active or sleeping on the task manager, at the time of the call. More...
 
bool hasTask (AsyncTask task)
 Returns true if the indicated task has been added to this AsyncTaskManager, false otherwise. More...
 
AsyncTaskChain makeTaskChain (str name)
 Creates a new AsyncTaskChain of the indicated name and stores it within the AsyncTaskManager. More...
 
 output (Ostream out)
 
 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 removeTaskChain (str name)
 Removes the AsyncTaskChain of the indicated name. More...
 
 setClock (ClockObject clock)
 Replaces the clock pointer used within the AsyncTaskManager. More...
 
 startThreads ()
 Starts any requested threads to service the tasks on the queue. More...
 
 stopThreads ()
 Stops any threads that are currently running. More...
 
 waitForTasks ()
 Blocks until the task list is empty. More...
 
 write (Ostream out, int indent_level)
 
- Public Member Functions inherited from TypedObject
TypeHandle getType ()
 
int getTypeIndex ()
 Returns the internal index number associated with this object's TypeHandle, a unique number for each different type. More...
 
bool isExactType (TypeHandle handle)
 Returns true if the current object is the indicated type exactly. More...
 
bool isOfType (TypeHandle handle)
 Returns true if the current object is or derives from the indicated type. More...
 
- Public Member Functions inherited from ReferenceCount
int getRefCount ()
 Returns the current reference count. More...
 
 ref ()
 Explicitly increments the reference count. More...
 
bool testRefCountIntegrity ()
 Does some easy checks to make sure that the reference count isn't completely bogus. More...
 
bool testRefCountNonzero ()
 Does some easy checks to make sure that the reference count isn't zero, or completely bogus. More...
 
bool unref ()
 Explicitly decrements the reference count. More...
 
- Public Member Functions inherited from Namable
 __init__ (const Namable)
 
 __init__ (str initial_name)
 
 clearName ()
 Resets the Namable's name to empty. More...
 
str getName ()
 
bool hasName ()
 Returns true if the Namable has a nonempty name set, false if the name is empty. More...
 
 output (Ostream out)
 Outputs the Namable. More...
 
 setName (str name)
 

Static Public Member Functions

static TypeHandle getClassType ()
 
static AsyncTaskManager getGlobalPtr ()
 Returns a pointer to the global AsyncTaskManager. More...
 
- Static Public Member Functions inherited from TypedReferenceCount
static TypeHandle getClassType ()
 
- Static Public Member Functions inherited from TypedObject
static TypeHandle getClassType ()
 
- Static Public Member Functions inherited from ReferenceCount
static TypeHandle getClassType ()
 
- Static Public Member Functions inherited from Namable
static TypeHandle getClassType ()
 

Public Attributes

AsyncTaskCollection active_tasks
 Returns the set of tasks that are active (and not sleeping) on the task manager, at the time of the call. More...
 
ClockObject clock
 Returns the clock pointer used within the AsyncTaskManager. More...
 
double next_wake_time
 Returns the scheduled time (on the manager's clock) of the next sleeping task, on any task chain, to awaken. More...
 
AsyncTaskCollection sleeping_tasks
 Returns the set of tasks that are sleeping (and not active) on the task manager, at the time of the call. More...
 
AsyncTaskCollection tasks
 Returns the set of tasks that are active or sleeping on the task manager, at the time of the call. More...
 
- Public Attributes inherited from TypedObject
TypeHandle type
 Returns the TypeHandle representing this object's type. More...
 
- Public Attributes inherited from ReferenceCount
int ref_count
 The current reference count. More...
 
- Public Attributes inherited from Namable
const String name
 

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.

Member Function Documentation

◆ __init__()

__init__ ( str  name)

◆ add()

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.

◆ cleanup()

cleanup ( )

Stops all threads and messily empties the task list.

This is intended to be called on destruction only.

◆ findTask()

AsyncTask findTask ( str  name)

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.

◆ findTaskChain()

AsyncTaskChain findTaskChain ( str  name)

Searches a new AsyncTaskChain of the indicated name and returns it if it exists, or NULL otherwise.

◆ findTasks()

AsyncTaskCollection findTasks ( str  name)

Returns the list of tasks found with the indicated name.

◆ findTasksMatching()

AsyncTaskCollection findTasksMatching ( const GlobPattern  pattern)

Returns the list of tasks found whose name matches the indicated glob pattern, e.g.

"my_task_*".

◆ getActiveTasks()

AsyncTaskCollection getActiveTasks ( )

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

◆ getClassType()

static TypeHandle getClassType ( )
static

◆ getClock()

ClockObject getClock ( )

Returns the clock pointer used within the AsyncTaskManager.

See set_clock().

◆ getGlobalPtr()

static AsyncTaskManager getGlobalPtr ( )
static

Returns a pointer to the global AsyncTaskManager.

This is the AsyncTaskManager that most code should use for queueing tasks and suchlike.

◆ getNextWakeTime()

double getNextWakeTime ( )

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.

◆ getNumTaskChains()

int getNumTaskChains ( )

Returns the number of different task chains.

◆ getNumTasks()

int getNumTasks ( )

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

◆ getSleepingTasks()

AsyncTaskCollection getSleepingTasks ( )

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

◆ getTaskChain()

AsyncTaskChain getTaskChain ( int  n)

Returns the nth task chain.

◆ getTaskChains()

list getTaskChains ( )

◆ getTasks()

AsyncTaskCollection getTasks ( )

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

◆ hasTask()

bool hasTask ( AsyncTask  task)

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

◆ makeTaskChain()

AsyncTaskChain makeTaskChain ( str  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.

◆ output()

output ( Ostream  out)

◆ poll()

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.

◆ remove() [1/2]

bool 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.

◆ remove() [2/2]

int remove ( const AsyncTaskCollection  tasks)

Removes all of the tasks in the AsyncTaskCollection.

Returns the number of tasks removed.

◆ removeTaskChain()

bool removeTaskChain ( str  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.

◆ setClock()

setClock ( ClockObject  clock)

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.

◆ startThreads()

startThreads ( )

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.

◆ stopThreads()

stopThreads ( )

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.

◆ waitForTasks()

waitForTasks ( )

Blocks until the task list is empty.

◆ write()

write ( Ostream  out,
int  indent_level 
)

Member Data Documentation

◆ active_tasks

AsyncTaskCollection active_tasks

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

◆ clock

ClockObject clock

Returns the clock pointer used within the AsyncTaskManager.

See set_clock().

◆ next_wake_time

double 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.

◆ sleeping_tasks

AsyncTaskCollection sleeping_tasks

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

◆ tasks

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