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
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075 #ifndef Foundation_Thread_POSIX_INCLUDED
00076 #define Foundation_Thread_POSIX_INCLUDED
00077
00078
00079 #include "VMS/Foundation.h"
00080 #include "VMS/Runnable.h"
00081 #include "VMS/Event.h"
00082 #include "VMS/RefCountedObject.h"
00083 #include "VMS/AutoPtr.h"
00084 #include <pthread.h>
00085 #include <errno.h>
00086
00087
00088 namespace Poco {
00089
00090
00091 class Foundation_API ThreadImpl
00092 {
00093 public:
00094 enum Priority
00095 {
00096 PRIO_LOWEST_IMPL,
00097 PRIO_LOW_IMPL,
00098 PRIO_NORMAL_IMPL,
00099 PRIO_HIGH_IMPL,
00100 PRIO_HIGHEST_IMPL
00101 };
00102
00103 ThreadImpl();
00104 ~ThreadImpl();
00105
00106 Runnable& targetImpl() const;
00107 void setPriorityImpl(int prio);
00108 int getPriorityImpl() const;
00109 void startImpl(Runnable& target);
00110
00111 void joinImpl();
00112 bool joinImpl(long milliseconds);
00113 bool isRunningImpl() const;
00114 static void sleepImpl(long milliseconds);
00115 static void yieldImpl();
00116 static ThreadImpl* currentImpl();
00117
00118 protected:
00119 static void* entry(void* pThread);
00120 static int mapPrio(int prio);
00121
00122 private:
00123 struct ThreadData: public RefCountedObject
00124 {
00125 ThreadData():
00126 pTarget(0),
00127 thread(0),
00128 prio(PRIO_NORMAL_IMPL),
00129 done(false)
00130 {
00131 }
00132
00133 Runnable* pTarget;
00134 pthread_t thread;
00135 int prio;
00136 Event done;
00137 };
00138
00139 AutoPtr<ThreadData> _pData;
00140
00141 static pthread_key_t _currentKey;
00142 static bool _haveCurrentKey;
00143
00144 #if defined(POCO_OS_FAMILY_UNIX)
00145 SignalHandler::JumpBufferVec _jumpBufferVec;
00146 friend class SignalHandler;
00147 #endif
00148 };
00149
00150
00151
00152
00153
00154 inline int ThreadImpl::getPriorityImpl() const
00155 {
00156 return _pData->prio;
00157 }
00158
00159
00160 inline void ThreadImpl::sleepImpl(long milliseconds)
00161 {
00162 #if defined(__VMS) || defined(__digital__)
00163
00164 struct timespec interval;
00165 interval.tv_sec = milliseconds / 1000;
00166 interval.tv_nsec = (milliseconds % 1000)*1000000;
00167 pthread_delay_np(&interval);
00168 #else
00169 struct timeval tv;
00170 tv.tv_sec = milliseconds / 1000;
00171 tv.tv_usec = (milliseconds % 1000) * 1000;
00172 select(0, NULL, NULL, NULL, &tv);
00173 #endif
00174 }
00175
00176
00177 inline void ThreadImpl::yieldImpl()
00178 {
00179 sched_yield();
00180 }
00181
00182
00183 }
00184
00185
00186 #endif // Foundation_Thread_POSIX_INCLUDED