Panda3D
Public Member Functions | Static Public Member Functions | Public Attributes | List of all members
Thread Class Reference

A thread; that is, a lightweight process. More...

Inheritance diagram for Thread:
TypedReferenceCount Namable TypedObject ReferenceCount ExternalThread MainThread PythonThread

Public Member Functions

TypedReferenceCount getCurrentTask ()
 Returns the task currently executing on this thread (via the AsyncTaskManager), if any, or NULL if the thread is not currently servicing a task. More...
 
int getPipelineStage ()
 Returns the Pipeline stage number associated with this thread. More...
 
int getPstatsIndex ()
 Returns the PStats index associated with this thread, or -1 if no index has yet been associated with this thread. More...
 
int getPythonIndex ()
 Returns the Python index associated with this thread, or -1 if no index has yet been associated with this thread. More...
 
str getSyncName ()
 Returns the sync name of the thread. More...
 
str getUniqueId ()
 Returns a string that is guaranteed to be unique to this thread, across all processes on the machine, during at least the lifetime of this process. More...
 
bool isJoinable ()
 Returns the value of joinable that was passed to the start() call. More...
 
bool isStarted ()
 Returns true if the thread has been started, false if it has not, or if join() has already been called. More...
 
 join ()
 Blocks the calling process until the thread terminates. More...
 
 output (Ostream out)
 
 outputBlocker (Ostream out)
 Writes a description of the mutex or condition variable that this thread is blocked on. More...
 
 preempt ()
 Indicates that this thread should run as soon as possible, preemptying any other threads that may be scheduled to run. More...
 
 setMinPipelineStage (int min_pipeline_stage)
 Sets this thread's pipeline stage number to at least the indicated value, unless it is already larger. More...
 
 setPipelineStage (int pipeline_stage)
 Specifies the Pipeline stage number associated with this thread. More...
 
 setPythonIndex (int index)
 Stores a Python index to be associated with this thread. More...
 
bool start (ThreadPriority priority, bool joinable)
 Starts the thread executing. More...
 
- 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 Thread bindThread (str name, str sync_name)
 Returns a new Panda Thread object associated with the current thread (which has been created externally). More...
 
static considerYield ()
 Possibly suspends the current thread for the rest of the current epoch, if it has run for enough this epoch. More...
 
static forceYield ()
 Suspends the current thread for the rest of the current epoch. More...
 
static TypeHandle getClassType ()
 
static int getCurrentPipelineStage ()
 Returns the integer pipeline stage associated with the current thread. More...
 
static Thread getCurrentThread ()
 Returns a pointer to the currently-executing Thread object. More...
 
static Thread getExternalThread ()
 Returns a pointer to the "external" Thread object–this is a special Thread object that corresponds to any thread spawned outside of Panda's threading interface. More...
 
static Thread getMainThread ()
 Returns a pointer to the "main" Thread object–this is the Thread that started the whole process. More...
 
static bool isSimpleThreads ()
 Returns true if Panda is currently compiled for "simple threads", which is to say, cooperative context switching only, reducing the need for quite so many critical section protections. More...
 
static bool isThreadingSupported ()
 Returns true if threading support has been compiled in and enabled, or false if no threading is available (and Thread::start() will always fail). More...
 
static bool isTrueThreads ()
 Returns true if a real threading library is available that supports actual OS-implemented threads, or false if the only threading we can provide is simulated user-space threading. More...
 
static prepareForExit ()
 Should be called by the main thread just before exiting the program, this blocks until any remaining thread cleanup has finished. More...
 
static sleep (double seconds)
 Suspends the current thread for at least the indicated amount of time. More...
 
static writeStatus (Ostream out)
 
- 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

int current_pipeline_stage
 Returns the integer pipeline stage associated with the current thread. More...
 
TypedReferenceCount current_task
 Returns the task currently executing on this thread (via the AsyncTaskManager), if any, or NULL if the thread is not currently servicing a task. More...
 
Thread current_thread
 Returns a pointer to the currently-executing Thread object. More...
 
Thread external_thread
 Returns a pointer to the "external" Thread object–this is a special Thread object that corresponds to any thread spawned outside of Panda's threading interface. More...
 
bool joinable
 Returns the value of joinable that was passed to the start() call. More...
 
Thread main_thread
 Returns a pointer to the "main" Thread object–this is the Thread that started the whole process. More...
 
int pipeline_stage
 Returns the Pipeline stage number associated with this thread. More...
 
int pstats_index
 Returns the PStats index associated with this thread, or -1 if no index has yet been associated with this thread. More...
 
int python_index
 Returns the Python index associated with this thread, or -1 if no index has yet been associated with this thread. More...
 
bool simple_threads
 Returns true if Panda is currently compiled for "simple threads", which is to say, cooperative context switching only, reducing the need for quite so many critical section protections. More...
 
bool started
 Returns true if the thread has been started, false if it has not, or if join() has already been called. More...
 
const String sync_name
 Returns the sync name of the thread. More...
 
bool threading_supported
 Returns true if threading support has been compiled in and enabled, or false if no threading is available (and Thread::start() will always fail). More...
 
bool true_threads
 Returns true if a real threading library is available that supports actual OS-implemented threads, or false if the only threading we can provide is simulated user-space threading. More...
 
String unique_id
 Returns a string that is guaranteed to be unique to this thread, across all processes on the machine, during at least the lifetime of this process. 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 thread; that is, a lightweight process.

This is an abstract base class; to use it, you must subclass from it and redefine thread_main().

The thread itself will keep a reference count on the Thread object while it is running; when the thread returns from its root function, the Thread object will automatically be destructed if no other pointers are referencing it.

Member Function Documentation

◆ bindThread()

static Thread bindThread ( str  name,
str  sync_name 
)
static

Returns a new Panda Thread object associated with the current thread (which has been created externally).

This can be used to bind a unique Panda Thread object with an external thread, such as a new Python thread.

It is particularly useful to bind a Panda Thread object to an external thread for the purposes of PStats monitoring. Without this call, each external thread will be assigned the same global ExternalThread object, which means they will all appear in the same PStats graph.

It is the caller's responsibility to save the returned Thread pointer for the lifetime of the external thread. It is an error for the Thread pointer to destruct while the external thread is still in the system.

It is also an error to call this method from the main thread, or twice within a given thread, unless it is given the same name each time (in which case the same pointer will be returned each time).

◆ considerYield()

static considerYield ( )
static

Possibly suspends the current thread for the rest of the current epoch, if it has run for enough this epoch.

This is especially important for the simple thread implementation, which relies on cooperative yields like this.

◆ forceYield()

static forceYield ( )
static

Suspends the current thread for the rest of the current epoch.

◆ getClassType()

static TypeHandle getClassType ( )
static

◆ getCurrentPipelineStage()

static int getCurrentPipelineStage ( )
static

Returns the integer pipeline stage associated with the current thread.

This is the same thing as get_current_thread()->get_pipeline_stage(), but it may be faster to retrieve in some contexts.

◆ getCurrentTask()

TypedReferenceCount getCurrentTask ( )

Returns the task currently executing on this thread (via the AsyncTaskManager), if any, or NULL if the thread is not currently servicing a task.

◆ getCurrentThread()

static Thread getCurrentThread ( )
static

Returns a pointer to the currently-executing Thread object.

If this is called from the main thread, this will return the same value as get_main_thread().

This will always return some valid Thread pointer. It will never return NULL, even if the current thread was spawned outside of Panda's threading system, although all non-Panda threads will return the exact same Thread pointer.

◆ getExternalThread()

static Thread getExternalThread ( )
static

Returns a pointer to the "external" Thread object–this is a special Thread object that corresponds to any thread spawned outside of Panda's threading interface.

Note that multiple different threads may share this same pointer.

◆ getMainThread()

static Thread getMainThread ( )
static

Returns a pointer to the "main" Thread object–this is the Thread that started the whole process.

◆ getPipelineStage()

int getPipelineStage ( )

Returns the Pipeline stage number associated with this thread.

The default stage is 0 if no stage is specified otherwise. See set_pipeline_stage().

◆ getPstatsIndex()

int getPstatsIndex ( )

Returns the PStats index associated with this thread, or -1 if no index has yet been associated with this thread.

This is used internally by the PStatClient; you should not need to call this directly.

◆ getPythonIndex()

int getPythonIndex ( )

Returns the Python index associated with this thread, or -1 if no index has yet been associated with this thread.

This is used internally by the direct.stdpy.thread module; you should not need to call this directly.

◆ getSyncName()

str getSyncName ( )

Returns the sync name of the thread.

This name collects threads into "sync groups", which are expected to run synchronously. This is mainly used for the benefit of PStats; threads with the same sync name can be ticked all at once via the thread_tick() call.

◆ getUniqueId()

str getUniqueId ( )

Returns a string that is guaranteed to be unique to this thread, across all processes on the machine, during at least the lifetime of this process.

◆ isJoinable()

bool isJoinable ( )

Returns the value of joinable that was passed to the start() call.

◆ isSimpleThreads()

static bool isSimpleThreads ( )
static

Returns true if Panda is currently compiled for "simple threads", which is to say, cooperative context switching only, reducing the need for quite so many critical section protections.

This is not necessarily the opposite of "true threads", since one possible implementation of simple threads is via true threads with mutex protection to ensure only one runs at a time.

◆ isStarted()

bool isStarted ( )

Returns true if the thread has been started, false if it has not, or if join() has already been called.

◆ isThreadingSupported()

static bool isThreadingSupported ( )
static

Returns true if threading support has been compiled in and enabled, or false if no threading is available (and Thread::start() will always fail).

◆ isTrueThreads()

static bool isTrueThreads ( )
static

Returns true if a real threading library is available that supports actual OS-implemented threads, or false if the only threading we can provide is simulated user-space threading.

◆ join()

join ( )

Blocks the calling process until the thread terminates.

If the thread has already terminated, this returns immediately.

◆ output()

output ( Ostream  out)

◆ outputBlocker()

outputBlocker ( Ostream  out)

Writes a description of the mutex or condition variable that this thread is blocked on.

Writes nothing if there is no blocker, or if we are not in DEBUG_THREADS mode.

◆ preempt()

preempt ( )

Indicates that this thread should run as soon as possible, preemptying any other threads that may be scheduled to run.

This may not be implemented on every platform.

◆ prepareForExit()

static prepareForExit ( )
static

Should be called by the main thread just before exiting the program, this blocks until any remaining thread cleanup has finished.

◆ setMinPipelineStage()

setMinPipelineStage ( int  min_pipeline_stage)

Sets this thread's pipeline stage number to at least the indicated value, unless it is already larger.

See set_pipeline_stage().

◆ setPipelineStage()

setPipelineStage ( int  pipeline_stage)

Specifies the Pipeline stage number associated with this thread.

The default stage is 0 if no stage is specified otherwise.

This must be a value in the range [0 .. pipeline->get_num_stages() - 1]. It specifies the values that this thread observes for all pipelined data. Typically, an application thread will leave this at 0, but a render thread may set it to 1 or 2 (to operate on the previous frame's data, or the second previous frame's data).

◆ setPythonIndex()

setPythonIndex ( int  index)

Stores a Python index to be associated with this thread.

This is used internally by the thread module; you should not need to call this directly.

◆ sleep()

static sleep ( double  seconds)
static

Suspends the current thread for at least the indicated amount of time.

It might be suspended for longer.

◆ start()

bool start ( ThreadPriority  priority,
bool  joinable 
)

Starts the thread executing.

It is only valid to call this once.

The thread will begin executing its thread_main() function, and will terminate when thread_main() returns.

priority is intended as a hint to the relative importance of this thread. This may be ignored by the thread implementation.

joinable should be set true if you intend to call join() to wait for the thread to terminate, or false if you don't care and you will never call join(). Note that the reference count on the Thread object is incremented while the thread itself is running, so if you just want to fire and forget a thread, you may pass joinable = false, and never store the Thread object. It will automatically destruct itself when it finishes.

The return value is true if the thread is successfully started, false otherwise.

◆ writeStatus()

static writeStatus ( Ostream  out)
static

Member Data Documentation

◆ current_pipeline_stage

int current_pipeline_stage

Returns the integer pipeline stage associated with the current thread.

This is the same thing as get_current_thread()->get_pipeline_stage(), but it may be faster to retrieve in some contexts.

◆ current_task

TypedReferenceCount current_task

Returns the task currently executing on this thread (via the AsyncTaskManager), if any, or NULL if the thread is not currently servicing a task.

◆ current_thread

Thread current_thread

Returns a pointer to the currently-executing Thread object.

If this is called from the main thread, this will return the same value as get_main_thread().

This will always return some valid Thread pointer. It will never return NULL, even if the current thread was spawned outside of Panda's threading system, although all non-Panda threads will return the exact same Thread pointer.

◆ external_thread

Thread external_thread

Returns a pointer to the "external" Thread object–this is a special Thread object that corresponds to any thread spawned outside of Panda's threading interface.

Note that multiple different threads may share this same pointer.

◆ joinable

bool joinable

Returns the value of joinable that was passed to the start() call.

◆ main_thread

Thread main_thread

Returns a pointer to the "main" Thread object–this is the Thread that started the whole process.

◆ pipeline_stage

int pipeline_stage

Returns the Pipeline stage number associated with this thread.

The default stage is 0 if no stage is specified otherwise. See set_pipeline_stage().

◆ pstats_index

int pstats_index

Returns the PStats index associated with this thread, or -1 if no index has yet been associated with this thread.

This is used internally by the PStatClient; you should not need to call this directly.

◆ python_index

int python_index

Returns the Python index associated with this thread, or -1 if no index has yet been associated with this thread.

This is used internally by the direct.stdpy.thread module; you should not need to call this directly.

◆ simple_threads

bool simple_threads

Returns true if Panda is currently compiled for "simple threads", which is to say, cooperative context switching only, reducing the need for quite so many critical section protections.

This is not necessarily the opposite of "true threads", since one possible implementation of simple threads is via true threads with mutex protection to ensure only one runs at a time.

◆ started

bool started

Returns true if the thread has been started, false if it has not, or if join() has already been called.

◆ sync_name

const String sync_name

Returns the sync name of the thread.

This name collects threads into "sync groups", which are expected to run synchronously. This is mainly used for the benefit of PStats; threads with the same sync name can be ticked all at once via the thread_tick() call.

◆ threading_supported

bool threading_supported

Returns true if threading support has been compiled in and enabled, or false if no threading is available (and Thread::start() will always fail).

◆ true_threads

bool true_threads

Returns true if a real threading library is available that supports actual OS-implemented threads, or false if the only threading we can provide is simulated user-space threading.

◆ unique_id

String unique_id

Returns a string that is guaranteed to be unique to this thread, across all processes on the machine, during at least the lifetime of this process.