Panda3D
 All Classes Functions Variables Enumerations
thread.h
1 // Filename: thread.h
2 // Created by: cary (16Sep98)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #ifndef THREAD_H
16 #define THREAD_H
17 
18 #include "pandabase.h"
19 #include "namable.h"
20 #include "typedReferenceCount.h"
21 #include "pointerTo.h"
22 #include "threadPriority.h"
23 #include "threadImpl.h"
24 #include "pnotify.h"
25 #include "config_pipeline.h"
26 
27 #ifdef HAVE_PYTHON
28 #undef _POSIX_C_SOURCE
29 #include <Python.h>
30 #endif // HAVE_PYTHON
31 
32 class Mutex;
33 class ReMutex;
34 class MutexDebug;
35 class ConditionVarDebug;
36 class ConditionVarFullDebug;
37 class AsyncTaskBase;
38 
39 ////////////////////////////////////////////////////////////////////
40 // Class : Thread
41 // Description : A thread; that is, a lightweight process. This is an
42 // abstract base class; to use it, you must subclass
43 // from it and redefine thread_main().
44 //
45 // The thread itself will keep a reference count on the
46 // Thread object while it is running; when the thread
47 // returns from its root function, the Thread object
48 // will automatically be destructed if no other pointers
49 // are referencing it.
50 ////////////////////////////////////////////////////////////////////
51 class EXPCL_PANDA_PIPELINE Thread : public TypedReferenceCount, public Namable {
52 protected:
53  Thread(const string &name, const string &sync_name);
54 
55 PUBLISHED:
56  virtual ~Thread();
57 
58 private:
59  INLINE Thread(const Thread &copy);
60  INLINE void operator = (const Thread &copy);
61 
62 protected:
63  virtual void thread_main()=0;
64 
65 PUBLISHED:
66  static PT(Thread) bind_thread(const string &name, const string &sync_name);
67 
68  INLINE const string &get_sync_name() const;
69 
70  INLINE int get_pstats_index() const;
71  INLINE string get_unique_id() const;
72 
73  INLINE int get_pipeline_stage() const;
74  void set_pipeline_stage(int pipeline_stage);
75  INLINE void set_min_pipeline_stage(int min_pipeline_stage);
76 
77  INLINE static Thread *get_main_thread();
78  INLINE static Thread *get_external_thread();
79  INLINE static Thread *get_current_thread();
80  INLINE static int get_current_pipeline_stage();
81  INLINE static bool is_threading_supported();
82  INLINE static bool is_true_threads();
83  INLINE static bool is_simple_threads();
84  BLOCKING INLINE static void sleep(double seconds);
85 
86  BLOCKING INLINE static void force_yield();
87  BLOCKING INLINE static void consider_yield();
88 
89  virtual void output(ostream &out) const;
90  void output_blocker(ostream &out) const;
91  static void write_status(ostream &out);
92 
93  INLINE bool is_started() const;
94  INLINE bool is_joinable() const;
95 
96  bool start(ThreadPriority priority, bool joinable);
97  BLOCKING INLINE void join();
98  INLINE void preempt();
99 
100 #ifdef HAVE_PYTHON
101  void set_python_data(PyObject *python_data);
102  PyObject *get_python_data() const;
103 #endif
104 
105  INLINE AsyncTaskBase *get_current_task() const;
106 
107  INLINE static void prepare_for_exit();
108 
109 public:
110  // This class allows integration with PStats, particularly in the
111  // SIMPLE_THREADS case.
112  class EXPCL_PANDA_PIPELINE PStatsCallback {
113  public:
114  virtual ~PStatsCallback();
115  virtual void deactivate_hook(Thread *thread);
116  virtual void activate_hook(Thread *thread);
117  };
118 
119  INLINE void set_pstats_index(int pstats_index);
120  INLINE void set_pstats_callback(PStatsCallback *pstats_callback);
121  INLINE PStatsCallback *get_pstats_callback() const;
122 
123 #ifdef HAVE_PYTHON
124  // Integration with Python.
125  PyObject *call_python_func(PyObject *function, PyObject *args);
126  void handle_python_exception();
127 #endif // HAVE_PYTHON
128 
129 private:
130  static void init_main_thread();
131  static void init_external_thread();
132 
133 protected:
134  bool _started;
135 
136 private:
137  string _sync_name;
138  ThreadImpl _impl;
139  int _pstats_index;
140  int _pipeline_stage;
141  PStatsCallback *_pstats_callback;
142  bool _joinable;
143  AsyncTaskBase *_current_task;
144 
145 #ifdef HAVE_PYTHON
146  PyObject *_python_data;
147 #endif
148 
149 #ifdef DEBUG_THREADS
150  MutexDebug *_blocked_on_mutex;
151  ConditionVarDebug *_waiting_on_cvar;
152  ConditionVarFullDebug *_waiting_on_cvar_full;
153 #endif // DEBUG_THREADS
154 
155 private:
156  static Thread *_main_thread;
157  static Thread *_external_thread;
158 
159 public:
160  static TypeHandle get_class_type() {
161  return _type_handle;
162  }
163  static void init_type() {
164  TypedReferenceCount::init_type();
165  Namable::init_type(),
166  register_type(_type_handle, "Thread",
167  TypedReferenceCount::get_class_type(),
168  Namable::get_class_type());
169  }
170  virtual TypeHandle get_type() const {
171  return get_class_type();
172  }
173  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
174 
175 private:
176  static TypeHandle _type_handle;
177 
178  friend class MutexDebug;
179  friend class ConditionVarDebug;
180  friend class ConditionVarFullDebug;
181 
182  friend class ThreadDummyImpl;
183  friend class ThreadWin32Impl;
184  friend class ThreadPosixImpl;
185  friend class ThreadSimpleImpl;
186  friend class MainThread;
187  friend class AsyncTaskBase;
188 };
189 
190 INLINE ostream &operator << (ostream &out, const Thread &thread);
191 
192 #include "thread.I"
193 
194 #endif
The abstract base class for AsyncTask.
Definition: asyncTaskBase.h:31
A base class for things which need to inherit from both TypedObject and from ReferenceCount.
A standard mutex, or mutual exclusion lock.
Definition: pmutex.h:44
A base class for all things which can have a name.
Definition: namable.h:29
The special &quot;main thread&quot; class.
Definition: mainThread.h:27
void output(ostream &out) const
Outputs the Namable.
Definition: namable.I:97
A thread; that is, a lightweight process.
Definition: thread.h:51
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
A reentrant mutex.
Definition: reMutex.h:36