00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #ifndef THREADSIMPLEIMPL_H
00016 #define THREADSIMPLEIMPL_H
00017
00018 #include "pandabase.h"
00019 #include "selectThreadImpl.h"
00020
00021 #ifdef THREAD_SIMPLE_IMPL
00022
00023 #include "pnotify.h"
00024 #include "threadPriority.h"
00025 #include "pvector.h"
00026 #include "contextSwitch.h"
00027
00028 #ifdef HAVE_PYTHON
00029
00030 #undef _POSIX_C_SOURCE
00031 #include <Python.h>
00032
00033 #endif // HAVE_PYTHON
00034
00035 class Thread;
00036 class ThreadSimpleManager;
00037 class MutexSimpleImpl;
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061 class EXPCL_PANDA_PIPELINE ThreadSimpleImpl {
00062 public:
00063 ThreadSimpleImpl(Thread *parent_obj);
00064 ~ThreadSimpleImpl();
00065
00066 void setup_main_thread();
00067 bool start(ThreadPriority priority, bool joinable);
00068 void join();
00069 void preempt();
00070
00071 string get_unique_id() const;
00072
00073 static void prepare_for_exit();
00074
00075 INLINE static Thread *get_current_thread();
00076 INLINE bool is_same_system_thread() const;
00077
00078 INLINE static void bind_thread(Thread *thread);
00079 INLINE static bool is_threading_supported();
00080 INLINE static bool is_true_threads();
00081 INLINE static bool is_simple_threads();
00082 INLINE static void sleep(double seconds);
00083 INLINE static void yield();
00084 INLINE static void consider_yield();
00085
00086 void sleep_this(double seconds);
00087 void yield_this(bool volunteer);
00088 INLINE void consider_yield_this();
00089
00090 INLINE double get_wake_time() const;
00091
00092 INLINE static void write_status(ostream &out);
00093
00094 private:
00095 static void st_begin_thread(void *data);
00096 void begin_thread();
00097
00098 private:
00099 enum ThreadStatus {
00100 TS_new,
00101 TS_running,
00102 TS_finished,
00103 TS_killed,
00104 };
00105
00106 static int _next_unique_id;
00107 int _unique_id;
00108 Thread *_parent_obj;
00109 bool _joinable;
00110 ThreadStatus _status;
00111 ThreadPriority _priority;
00112
00113
00114
00115 double _priority_weight;
00116
00117
00118 unsigned int _run_ticks;
00119
00120
00121
00122 double _start_time;
00123
00124
00125
00126 double _stop_time;
00127
00128
00129 double _wake_time;
00130
00131 ThreadContext *_context;
00132 unsigned char *_stack;
00133 size_t _stack_size;
00134
00135 #ifdef HAVE_PYTHON
00136
00137
00138 PyThreadState *_python_state;
00139 #endif // HAVE_PYTHON
00140
00141
00142 typedef pvector<ThreadSimpleImpl *> JoiningThreads;
00143 JoiningThreads _joining_threads;
00144
00145 ThreadSimpleManager *_manager;
00146 static ThreadSimpleImpl *volatile _st_this;
00147
00148
00149
00150
00151
00152
00153
00154
00155 #ifdef HAVE_POSIX_THREADS
00156 pthread_t _posix_system_thread_id;
00157 #endif
00158 #ifdef WIN32
00159 DWORD _win32_system_thread_id;
00160 #endif
00161
00162 friend class ThreadSimpleManager;
00163 };
00164
00165
00166
00167 #include "threadSimpleManager.h"
00168
00169 #include "threadSimpleImpl.I"
00170
00171 #endif // THREAD_SIMPLE_IMPL
00172
00173 #endif