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
The special "main thread" class.
Definition mainThread.h:24
A standard mutex, or mutual exclusion lock.
Definition pmutex.h:40
A base class for all things which can have a name.
Definition namable.h:26
void output(std::ostream &out) const
Outputs the Namable.
Definition namable.I:61
A reentrant mutex.
Definition reMutex.h:34
A thread; that is, a lightweight process.
Definition thread.h:46
TypeHandle is the identifier used to differentiate C++ class types.
Definition typeHandle.h:81
A base class for things which need to inherit from both TypedObject and from ReferenceCount.
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.