Panda3D
Loading...
Searching...
No Matches
thread.h
Go to the documentation of this file.
1/**
2 * PANDA 3D SOFTWARE
3 * Copyright (c) Carnegie Mellon University. All rights reserved.
4 *
5 * All use of this software is subject to the terms of the revised BSD
6 * license. You should have received a copy of this license along
7 * with this source code in a file named "LICENSE."
8 *
9 * @file thread.h
10 * @author cary
11 * @date 1998-09-16
12 */
13
14#ifndef THREAD_H
15#define THREAD_H
16
17#include "pandabase.h"
18#include "namable.h"
19#include "typedReferenceCount.h"
20#include "pointerTo.h"
21#include "threadPriority.h"
22#include "threadImpl.h"
23#include "pnotify.h"
24#include "config_pipeline.h"
25
26#ifdef ANDROID
27typedef struct _JNIEnv JNIEnv;
28#endif
29
30class Mutex;
31class ReMutex;
32class MutexDebug;
33class ConditionVarDebug;
34class ConditionVarFullDebug;
35class AsyncTask;
36
37/**
38 * A thread; that is, a lightweight process. This is an abstract base class;
39 * to use it, you must subclass from it and redefine thread_main().
40 *
41 * The thread itself will keep a reference count on the Thread object while it
42 * is running; when the thread returns from its root function, the Thread
43 * object will automatically be destructed if no other pointers are
44 * referencing it.
45 */
46class EXPCL_PANDA_PIPELINE Thread : public TypedReferenceCount, public Namable {
47protected:
48 Thread(const std::string &name, const std::string &sync_name);
49 Thread(const Thread &copy) = delete;
50
51PUBLISHED:
52 virtual ~Thread();
53
54protected:
55 Thread &operator = (const Thread &copy) = delete;
56
57 virtual void thread_main()=0;
58
59PUBLISHED:
60 static PT(Thread) bind_thread(const std::string &name, const std::string &sync_name);
61
62 INLINE const std::string &get_sync_name() const;
63
64 INLINE int get_pstats_index() const;
65 INLINE int get_python_index() const;
66 INLINE std::string get_unique_id() const;
67
68 INLINE int get_pipeline_stage() const;
69 void set_pipeline_stage(int pipeline_stage);
70 INLINE void set_min_pipeline_stage(int min_pipeline_stage);
71
72 INLINE static Thread *get_main_thread();
73 INLINE static Thread *get_external_thread();
74 INLINE static Thread *get_current_thread();
75 INLINE static int get_current_pipeline_stage();
76 INLINE static bool is_threading_supported();
77 INLINE static bool is_true_threads();
78 INLINE static bool is_simple_threads();
79 BLOCKING INLINE static void sleep(double seconds);
80
81 BLOCKING INLINE static void force_yield();
82 BLOCKING INLINE static void consider_yield();
83
84 virtual void output(std::ostream &out) const;
85 void output_blocker(std::ostream &out) const;
86 static void write_status(std::ostream &out);
87
88 INLINE bool is_started() const;
89 INLINE bool is_joinable() const;
90
91 bool start(ThreadPriority priority, bool joinable);
92 BLOCKING INLINE void join();
93 INLINE void preempt();
94
95 INLINE TypedReferenceCount *get_current_task() const;
96
97 INLINE void set_python_index(int index);
98
99 INLINE static void prepare_for_exit();
100
101 MAKE_PROPERTY(sync_name, get_sync_name);
102 MAKE_PROPERTY(pstats_index, get_pstats_index);
103 MAKE_PROPERTY(python_index, get_python_index);
104 MAKE_PROPERTY(unique_id, get_unique_id);
105 MAKE_PROPERTY(pipeline_stage, get_pipeline_stage, set_pipeline_stage);
106
107 MAKE_PROPERTY(main_thread, get_main_thread);
108 MAKE_PROPERTY(external_thread, get_external_thread);
109 MAKE_PROPERTY(current_thread, get_current_thread);
110 MAKE_PROPERTY(current_pipeline_stage, get_current_pipeline_stage);
111
112 MAKE_PROPERTY(threading_supported, is_threading_supported);
113 MAKE_PROPERTY(true_threads, is_true_threads);
114 MAKE_PROPERTY(simple_threads, is_simple_threads);
115
116 MAKE_PROPERTY(started, is_started);
117 MAKE_PROPERTY(joinable, is_joinable);
118 MAKE_PROPERTY(current_task, get_current_task);
119
120public:
121 // This class allows integration with PStats, particularly in the
122 // SIMPLE_THREADS case.
123 class EXPCL_PANDA_PIPELINE PStatsCallback {
124 public:
125 virtual ~PStatsCallback();
126 virtual void deactivate_hook(Thread *thread);
127 virtual void activate_hook(Thread *thread);
128 };
129
130 INLINE void set_pstats_index(int pstats_index);
131 INLINE void set_pstats_callback(PStatsCallback *pstats_callback);
132 INLINE PStatsCallback *get_pstats_callback() const;
133
134#ifdef ANDROID
135 INLINE JNIEnv *get_jni_env() const;
136#endif
137
138private:
139 static void init_main_thread();
140 static void init_external_thread();
141
142protected:
143 bool _started;
144
145private:
146 std::string _sync_name;
147 ThreadImpl _impl;
148 int _pstats_index;
149 int _pipeline_stage;
150 PStatsCallback *_pstats_callback;
151 bool _joinable;
152 AtomicAdjust::Pointer _current_task;
153
154 int _python_index;
155
156#ifdef DEBUG_THREADS
157 MutexDebug *_blocked_on_mutex;
158 ConditionVarDebug *_waiting_on_cvar;
159 ConditionVarFullDebug *_waiting_on_cvar_full;
160#endif // DEBUG_THREADS
161
162private:
163 static Thread *_main_thread;
164 static Thread *_external_thread;
165
166public:
167 static TypeHandle get_class_type() {
168 return _type_handle;
169 }
170 static void init_type() {
171 TypedReferenceCount::init_type();
172 Namable::init_type(),
173 register_type(_type_handle, "Thread",
174 TypedReferenceCount::get_class_type(),
175 Namable::get_class_type());
176 }
177 virtual TypeHandle get_type() const {
178 return get_class_type();
179 }
180 virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
181
182private:
183 static TypeHandle _type_handle;
184
185 friend class MutexDebug;
186 friend class ConditionVarDebug;
187 friend class ConditionVarFullDebug;
188
189 friend class ThreadDummyImpl;
190 friend class ThreadWin32Impl;
191 friend class ThreadPosixImpl;
192 friend class ThreadSimpleImpl;
193 friend class MainThread;
194 friend class AsyncTask;
195};
196
197INLINE std::ostream &operator << (std::ostream &out, const Thread &thread);
198
199#include "thread.I"
200
201#endif
This class represents a concrete task performed by an AsyncManager.
Definition asyncTask.h:32
A standard mutex, or mutual exclusion lock.
Definition pmutex.h:40
void output(std::ostream &out) const
Outputs the Namable.
Definition namable.I:61
A reentrant mutex.
Definition reMutex.h:34
virtual void deactivate_hook(Thread *thread)
Called when the thread is deactivated (swapped for another running thread).
Definition thread.cxx:247
virtual void activate_hook(Thread *thread)
Called when the thread is activated (resumes execution).
Definition thread.cxx:256
A thread; that is, a lightweight process.
Definition thread.h:46
void preempt()
Indicates that this thread should run as soon as possible, preemptying any other threads that may be ...
Definition thread.I:253
static void sleep(double seconds)
Suspends the current thread for at least the indicated amount of time.
Definition thread.I:192
get_python_index
Returns the Python index associated with this thread, or -1 if no index has yet been associated with ...
Definition thread.h:103
is_simple_threads
Returns true if Panda is currently compiled for "simple threads", which is to say,...
Definition thread.h:114
void output_blocker(std::ostream &out) const
Writes a description of the mutex or condition variable that this thread is blocked on.
Definition thread.cxx:142
get_pipeline_stage
Returns the Pipeline stage number associated with this thread.
Definition thread.h:105
is_joinable
Returns the value of joinable that was passed to the start() call.
Definition thread.h:117
void set_python_index(int index)
Stores a Python index to be associated with this thread.
Definition thread.I:274
is_threading_supported
Returns true if threading support has been compiled in and enabled, or false if no threading is avail...
Definition thread.h:112
get_current_task
Returns the task currently executing on this thread (via the AsyncTaskManager), if any,...
Definition thread.h:118
get_sync_name
Returns the sync name of the thread.
Definition thread.h:101
void join()
Blocks the calling process until the thread terminates.
Definition thread.I:239
void set_pstats_index(int pstats_index)
Stores a PStats index to be associated with this thread.
Definition thread.I:303
get_current_pipeline_stage
Returns the integer pipeline stage associated with the current thread.
Definition thread.h:110
get_main_thread
Returns a pointer to the "main" Thread object–this is the Thread that started the whole process.
Definition thread.h:107
static void consider_yield()
Possibly suspends the current thread for the rest of the current epoch, if it has run for enough this...
Definition thread.I:212
set_pipeline_stage
Specifies the Pipeline stage number associated with this thread.
Definition thread.h:105
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...
Definition thread.I:77
is_true_threads
Returns true if a real threading library is available that supports actual OS-implemented threads,...
Definition thread.h:113
get_external_thread
Returns a pointer to the "external" Thread object–this is a special Thread object that corresponds to...
Definition thread.h:108
bool start(ThreadPriority priority, bool joinable)
Starts the thread executing.
Definition thread.cxx:184
get_current_thread
Returns a pointer to the currently-executing Thread object.
Definition thread.h:109
void set_pstats_callback(PStatsCallback *pstats_callback)
Stores a PStats callback to be associated with this thread.
Definition thread.I:312
get_pstats_index
Returns the PStats index associated with this thread, or -1 if no index has yet been associated with ...
Definition thread.h:102
PStatsCallback * get_pstats_callback() const
Returns the PStats callback associated with this thread, or NULL if no callback has yet been associat...
Definition thread.I:322
static void prepare_for_exit()
Should be called by the main thread just before exiting the program, this blocks until any remaining ...
Definition thread.I:283
is_started
Returns true if the thread has been started, false if it has not, or if join() has already been calle...
Definition thread.h:116
get_unique_id
Returns a string that is guaranteed to be unique to this thread, across all processes on the machine,...
Definition thread.h:104
static void force_yield()
Suspends the current thread for the rest of the current epoch.
Definition thread.I:201
TypeHandle is the identifier used to differentiate C++ class types.
Definition typeHandle.h:81
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void register_type(TypeHandle &type_handle, const std::string &name)
This inline function is just a convenient way to call TypeRegistry::register_type(),...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.