00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef _terimber_thread_h_
00029 #define _terimber_thread_h_
00030
00031 #include "base/template.h"
00032 #include "threadpool/threadpoolfactory.h"
00033
00034 BEGIN_TERIMBER_NAMESPACE
00035 #pragma pack(4)
00036
00039 class job_task
00040 {
00041 public:
00043 job_task() :
00044 _employer(0),
00045 _ident(0),
00046 _timeout(INFINITE),
00047 _user_data(0)
00048 {
00049 }
00051 job_task( terimber_thread_employer* employer,
00052 size_t ident,
00053 size_t timeout,
00054 void* user_data
00055 ) :
00056 _employer(employer),
00057 _ident(ident),
00058 _timeout(timeout),
00059 _user_data(user_data)
00060 {
00061 }
00063 job_task(const job_task& x)
00064 {
00065 *this = x;
00066 }
00068 job_task& operator=(const job_task& x)
00069 {
00070 if (this != &x)
00071 {
00072 _employer = x._employer;
00073 _ident = x._ident;
00074 _timeout = x._timeout;
00075 _user_data = x._user_data;
00076 }
00077 return *this;
00078 }
00080 void clear()
00081 {
00082 _employer = 0;
00083 _ident = 0;
00084 _timeout = INFINITE;
00085 _user_data = 0;
00086 }
00087
00088 terimber_thread_employer* _employer;
00089 size_t _ident;
00090 size_t _timeout;
00091 void* _user_data;
00092 };
00093
00096 enum thread_state
00097 {
00098 THREAD_CLOSE,
00099 THREAD_STARTING,
00100 THREAD_SLEEPING,
00101 THREAD_RUNNING,
00102 THREAD_STOPPING
00103 };
00104
00107 class thread
00108 {
00110 static
00111 #if OS_TYPE == OS_WIN32
00112 unsigned int __stdcall
00113 #else
00114 void*
00115 #endif
00116 start_thread( void* data
00117 );
00118
00121 bool
00122 inside_thread() const;
00125 thread(const thread& x);
00127 thread& operator=(const thread& x);
00128 public:
00130 thread();
00132 ~thread();
00134 thread_state get_state() const;
00136 bool
00137 start();
00141 bool
00142 assign_job( const job_task& job_task
00143 );
00145 bool
00146 cancel_job();
00148 bool
00149 stop();
00151 void
00152 wakeup() const;
00153 private:
00155 bool
00156 change_state( thread_state new_state
00157 );
00159 bool
00160 execute();
00162 bool
00163 sleep();
00164 private:
00165 job_task _job_task;
00166 mutex _mtx;
00167 event _ev_start;
00168 event _ev_wakeup;
00169 event _ev_end;
00170 mutex _mtx_job;
00171 thread_state _state;
00172 #if OS_TYPE == OS_WIN32
00173 HANDLE
00174 #else
00175 pthread_t
00176 #endif
00177 _handle;
00178 };
00179
00182 class thread_creator : public proto_creator< thread_creator, thread, job_task >
00183 {
00184 public:
00186 static
00187 thread*
00188 create( const job_task& task
00189 );
00191 static
00192 void
00193 activate( thread* obj,
00194 const job_task& task
00195 );
00197 static
00198 void
00199 back( thread* obj,
00200 const job_task& task
00201 );
00203 static
00204 void
00205 destroy( thread* obj,
00206 const job_task& task
00207 );
00209 static
00210 void
00211 deactivate( thread* obj,
00212 const job_task& task
00213 );
00214 };
00215
00218 typedef pool< thread_creator > thread_pool_t;
00219
00220 #pragma pack()
00221 END_TERIMBER_NAMESPACE
00222
00223 #endif // _terimber_thread_h_