Panda3D
Classes | Public Member Functions | Static Public Member Functions | Public Attributes | Friends | List of all members
Thread Class Referenceabstract

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

#include "thread.h"

Inheritance diagram for Thread:
TypedReferenceCount Namable TypedObject ReferenceCount MemoryBase MemoryBase MemoryBase ExternalThread GenericThread MainThread

Classes

class  PStatsCallback
 

Public Member Functions

virtual TypeHandle force_init_type ()
 
TypedReferenceCountget_current_task () const
 
int get_pipeline_stage () const
 
PStatsCallbackget_pstats_callback () const
 Returns the PStats callback associated with this thread, or NULL if no callback has yet been associated with this thread. More...
 
int get_pstats_index () const
 
int get_python_index () const
 
virtual TypeHandle get_type () const
 
std::string get_unique_id () const
 
bool is_joinable () const
 
bool is_started () const
 
void join ()
 Blocks the calling process until the thread terminates. More...
 
virtual void output (std::ostream &out) const
 
void output_blocker (std::ostream &out) const
 Writes a description of the mutex or condition variable that this thread is blocked on. More...
 
void preempt ()
 Indicates that this thread should run as soon as possible, preemptying any other threads that may be scheduled to run. More...
 
void set_min_pipeline_stage (int min_pipeline_stage)
 Sets this thread's pipeline stage number to at least the indicated value, unless it is already larger. More...
 
void set_pipeline_stage (int pipeline_stage)
 
void set_pstats_callback (PStatsCallback *pstats_callback)
 Stores a PStats callback to be associated with this thread. More...
 
void set_pstats_index (int pstats_index)
 Stores a PStats index to be associated with this thread. More...
 
void set_python_index (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 TypedReferenceCount
 TypedReferenceCount (const TypedReferenceCount &copy)
 
void operator= (const TypedReferenceCount &copy)
 
- Public Member Functions inherited from TypedObject
 TypedObject (const TypedObject &copy)=default
 
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...
 
TypedObjectoperator= (const TypedObject &copy)=default
 
- Public Member Functions inherited from ReferenceCount
int get_ref_count () const
 
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 ref_if_nonzero () const
 Atomically increases the reference count of this object if it is not zero. 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...
 
WeakReferenceListweak_ref ()
 Adds the indicated PointerToVoid as a weak reference to this object. More...
 
void weak_unref ()
 Removes the indicated PointerToVoid as a weak reference to this object. More...
 
- Public Member Functions inherited from Namable
 Namable (const std::string &initial_name="")
 
void clear_name ()
 Resets the Namable's name to empty. More...
 
const std::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...
 
void output (std::ostream &out) const
 Outputs the Namable. More...
 
void set_name (const std::string &name)
 

Static Public Member Functions

static void consider_yield ()
 Possibly suspends the current thread for the rest of the current epoch, if it has run for enough this epoch. More...
 
static void force_yield ()
 Suspends the current thread for the rest of the current epoch. More...
 
static TypeHandle get_class_type ()
 
static int get_current_pipeline_stage ()
 
static Threadget_current_thread ()
 
static Threadget_external_thread ()
 
static Threadget_main_thread ()
 
static void init_type ()
 
static bool is_simple_threads ()
 
static bool is_threading_supported ()
 
static bool is_true_threads ()
 
static void prepare_for_exit ()
 Should be called by the main thread just before exiting the program, this blocks until any remaining thread cleanup has finished. More...
 
static void sleep (double seconds)
 Suspends the current thread for at least the indicated amount of time. More...
 
static void write_status (std::ostream &out)
 
- 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 ()
 

Public Attributes

 get_current_pipeline_stage
 Returns the integer pipeline stage associated with the current thread. More...
 
 get_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...
 
 get_current_thread
 Returns a pointer to the currently-executing Thread object. More...
 
 get_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...
 
 get_main_thread
 Returns a pointer to the "main" Thread object–this is the Thread that started the whole process. More...
 
 get_pipeline_stage
 Returns the Pipeline stage number associated with this thread. More...
 
 get_pstats_index
 Returns the PStats index associated with this thread, or -1 if no index has yet been associated with this thread. More...
 
 get_python_index
 Returns the Python index associated with this thread, or -1 if no index has yet been associated with this thread. More...
 
 get_sync_name
 Returns the sync name of the thread. More...
 
 get_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...
 
 is_joinable
 Returns the value of joinable that was passed to the start() call. More...
 
 is_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...
 
 is_started
 Returns true if the thread has been started, false if it has not, or if join() has already been called. More...
 
 is_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...
 
 is_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...
 
 set_pipeline_stage
 Specifies the Pipeline stage number associated with this thread. More...
 
- Public Attributes inherited from TypedObject
 get_type
 
- Public Attributes inherited from ReferenceCount
 get_ref_count
 Returns the current reference count. More...
 
- Public Attributes inherited from Namable
 get_name
 
 set_name
 

Friends

class AsyncTask
 
class ConditionVarDebug
 
class ConditionVarFullDebug
 
class MainThread
 
class MutexDebug
 
class ThreadDummyImpl
 
class ThreadPosixImpl
 
class ThreadSimpleImpl
 
class ThreadWin32Impl
 

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.

Definition at line 46 of file thread.h.

Member Function Documentation

◆ consider_yield()

void Thread::consider_yield ( )
inlinestatic

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.

Definition at line 212 of file thread.I.

Referenced by PartGroup::check_hierarchy(), PNMReader::read_data(), BamReader::read_pointer(), and PNMWriter::write_data().

◆ force_yield()

void Thread::force_yield ( )
inlinestatic

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

Definition at line 201 of file thread.I.

Referenced by AsyncFuture::set_result(), and AsyncFuture::wait().

◆ get_pstats_callback()

Thread::PStatsCallback * Thread::get_pstats_callback ( ) const
inline

Returns the PStats callback associated with this thread, or NULL if no callback has yet been associated with this thread.

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

Definition at line 322 of file thread.I.

◆ join()

void Thread::join ( )
inline

Blocks the calling process until the thread terminates.

If the thread has already terminated, this returns immediately.

Definition at line 239 of file thread.I.

◆ output_blocker()

void Thread::output_blocker ( std::ostream &  out) const

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.

Definition at line 142 of file thread.cxx.

◆ preempt()

void Thread::preempt ( )
inline

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.

Definition at line 253 of file thread.I.

◆ prepare_for_exit()

void Thread::prepare_for_exit ( )
inlinestatic

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

Definition at line 283 of file thread.I.

Referenced by PandaFramework::close_framework().

◆ set_min_pipeline_stage()

void Thread::set_min_pipeline_stage ( int  min_pipeline_stage)
inline

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

See set_pipeline_stage().

Definition at line 77 of file thread.I.

References set_pipeline_stage.

◆ set_pstats_callback()

void Thread::set_pstats_callback ( Thread::PStatsCallback pstats_callback)
inline

Stores a PStats callback to be associated with this thread.

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

Definition at line 312 of file thread.I.

◆ set_pstats_index()

void Thread::set_pstats_index ( int  pstats_index)
inline

Stores a PStats index to be associated with this thread.

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

Definition at line 303 of file thread.I.

◆ set_python_index()

void Thread::set_python_index ( int  python_index)
inline

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.

Definition at line 274 of file thread.I.

◆ sleep()

void Thread::sleep ( double  seconds)
inlinestatic

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

It might be suspended for longer.

Definition at line 192 of file thread.I.

Referenced by PStatServer::main_loop().

◆ start()

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

Definition at line 184 of file thread.cxx.

Member Data Documentation

◆ get_current_pipeline_stage

int Thread::get_current_pipeline_stage
inline

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.

Definition at line 110 of file thread.h.

◆ get_current_task

TypedReferenceCount * Thread::get_current_task
inline

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

Definition at line 118 of file thread.h.

◆ get_current_thread

Thread * Thread::get_current_thread
inline

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.

Definition at line 109 of file thread.h.

◆ get_external_thread

Thread * Thread::get_external_thread
inline

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.

Definition at line 108 of file thread.h.

◆ get_main_thread

Thread * Thread::get_main_thread
inline

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

Definition at line 107 of file thread.h.

◆ get_pipeline_stage

int Thread::get_pipeline_stage
inline

◆ get_pstats_index

int Thread::get_pstats_index
inline

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.

Definition at line 102 of file thread.h.

Referenced by PStatThread::PStatThread().

◆ get_python_index

int Thread::get_python_index
inline

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.

Definition at line 103 of file thread.h.

◆ get_sync_name

const std::string & Thread::get_sync_name
inline

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.

Definition at line 101 of file thread.h.

◆ get_unique_id

std::string Thread::get_unique_id
inline

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.

Definition at line 104 of file thread.h.

◆ is_joinable

bool Thread::is_joinable
inline

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

Definition at line 117 of file thread.h.

◆ is_simple_threads

bool Thread::is_simple_threads
inline

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.

Definition at line 114 of file thread.h.

◆ is_started

bool Thread::is_started
inline

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

Definition at line 116 of file thread.h.

◆ is_threading_supported

bool Thread::is_threading_supported
inline

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

Definition at line 112 of file thread.h.

◆ is_true_threads

bool Thread::is_true_threads
inline

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.

Definition at line 113 of file thread.h.

◆ set_pipeline_stage

void Thread::set_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).

Definition at line 105 of file thread.h.

Referenced by set_min_pipeline_stage().


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