DCL 3.7.6
Loading...
Searching...
No Matches
Thread.h
Go to the documentation of this file.
1#ifndef __DCL_THREAD_H__
2#define __DCL_THREAD_H__ 20110109
3
4#ifndef __DCL_CONFIG_H__
5#include <dcl/Config.h>
6#endif
7
8#ifdef __WINNT__
9 #if __DCL_PTHREAD
10 #include <pthread.h>
11 #define INFINITE (-1)
12 #define THREAD_ONCE_INIT PTHREAD_ONCE_INIT
13 typedef pthread_once_t thread_once_t;
14 typedef pthread_key_t thread_key_t;
15 #else
16 #ifndef _WINDOWS_
17 #error "Required windows.h, See dcl/_windows.h"
18 #endif
19 #if 0
20 #include <synchapi.h>
21 #endif
22 #define THREAD_ONCE_INIT INIT_ONCE_STATIC_INIT
23 typedef INIT_ONCE thread_once_t;
24 typedef DWORD thread_key_t;
25 #endif
26#else
27 #if defined(__ANDROID__)
28 #include <bits/pthread_types.h>
29 #elif !defined(__DEFINED_pthread_t) && defined(__linux__)
30 #include <bits/pthreadtypes.h>
31 #elif defined(__APPLE__)
32 #include <sys/_pthread/_pthread_types.h>
33 #else
34 #include <pthread.h>
35 #endif
36 #define INFINITE (-1)
37 #define THREAD_ONCE_INIT PTHREAD_ONCE_INIT
38 typedef pthread_once_t thread_once_t;
39 typedef pthread_key_t thread_key_t;
40#endif
41
42#ifndef __DCL_OBJECT_H__
43#include <dcl/Object.h>
44#endif
45#ifndef __DCL_STRING_H__
46#include <dcl/String.h>
47#endif
48
49__DCL_BEGIN_NAMESPACE
50
54class SysError;
55
56class DCLCAPI Thread : public Object
57{
58 DECLARE_CLASSINFO(Thread)
59public:
63 virtual String toString() const;
64
70 Thread(const char_t* _name = NULL);
71
76 void start() __DCL_THROWS1(SysError*);
77
83 int join();
84
90 bool started() const { return __threadId != 0; }
91
95 thread_t thread() const { return __threadId; }
96
100 const String& name() const { return __name; }
101
102protected:
114 virtual bool init();
115
119 virtual int run() = 0;
120
121private:
122 thread_t __threadId;
123 String __name;
124
125#if __DCL_PTHREAD
126 static void* startRoutine(void* _pThread);
127#elif defined(__WINNT__)
128 HANDLE __hThread;
129 static DWORD WINAPI startRoutine(void* _pThread);
130#endif
131
132public:
136 static void sleep(unsigned int _mills);
137
141 static void yield();
142
146 static thread_t self();
147
153 static Thread* getSelfThread();
154
158 typedef void (* initRoutine)();
171 static void once(thread_once_t& _onceControl, initRoutine _initRoutine);
172
183 static thread_key_t keyCreate() __DCL_THROWS1(SysError*);
184
193 static void keyDelete(thread_key_t _key) __DCL_THROWS1(SysError*);
194
203 static void keySetValue(thread_key_t _key, void* _value) __DCL_THROWS1(SysError*);
204
215 static void* keyGetValue(thread_key_t _key) __DCL_THROWS1(SysError*);
216
230 static void crtLock(const void* _p);
231 static void crtUnlock(const void* _p);
232
233 static long incrementAndGet(volatile long& _n);
234 static long long incrementAndGet(volatile long long& _n);
235 static long decrementAndGet(volatile long& _n);
236 static long long decrementAndGet(volatile long long& _n);
237
238public:
240 {
241 private:
242#if __DCL_PTHREAD
243 enum { READ_FD_INDEX = 0 };
244 int __fds[2];
245#elif defined(__WINNT__)
246 HANDLE __hEvent;
247#endif
248
250#if __DCL_PTHREAD
251 int handle() { return __fds[READ_FD_INDEX]; }
252#elif defined(__WINNT__)
253 HANDLE handle() { return __hEvent; }
254#endif
255
256 public:
257 Event();
258 ~Event();
259 void set();
260 void reset();
261
267 bool wait(unsigned int _milliseconds = INFINITE);
268 bool isWaiting() { return __bWaiting; }
269 private:
270 volatile bool __bWaiting;
271 };
272
274 {
275 private:
276#if __DCL_PTHREAD
277 pthread_mutex_t __mutex;
278#elif defined(__WINNT__)
279 CRITICAL_SECTION __cs;
280#endif
281 volatile thread_t __locker;
282
283#ifdef __WINNT__
284 protected:
285 Mutex(DWORD _dwSpinCount);
286#endif
287 public:
288 Mutex();
289 ~Mutex();
290 void lock();
291 bool tryLock();
292 void unlock();
293 thread_t locker() const {
294 return __locker;
295 }
296 };
297
298#if __DCL_PTHREAD && !defined(__APPLE__)
299 class DCLCAPI SpinLock
300 {
301 private:
302 pthread_spinlock_t __spinLock;
303 volatile pthread_t __locker;
304
305 public:
306 SpinLock(bool _pshared = false);
307 ~SpinLock();
308 void lock();
309 bool tryLock();
310 void unlock();
311 pthread_t locker() const {
312 return __locker;
313 }
314 };
315#elif defined(__WINNT__)
316 class DCLCAPI SpinLock : public Mutex
317 {
318 public:
319 SpinLock() : Mutex(INFINITE) { }
320 };
321#endif
322
323#if __DCL_PTHREAD || (defined(__WINNT__) && _WIN32_WINNT >= 0x0600)
324 class DCLCAPI ReadWriteLock
325 {
326 private:
327#if __DCL_PTHREAD
328 pthread_rwlock_t __rwlock;
329#else
330 SRWLOCK __srwlock;
331#endif
332
333 public:
334 ReadWriteLock();
335 ~ReadWriteLock();
336 void readLock(); // WINNT AcquireSRWLockShared
337 void writeLock(); // WINNT AcquireSRWLockExclusive
338#if __DCL_PTHREAD
339 bool tryReadLock();
340 bool tryWriteLock();
341 void unlock();
342#else
343 void readUnlock(); // WINNT ReleaseSRWLockShared
344 void writeUnlock(); // WINNT ReleaseSRWLockExclusive
345#endif
346 };
347#endif
348
355 {
356 private:
357#if __DCL_PTHREAD
358 pthread_cond_t __cond;
359#elif defined(__WINNT__)
360 #if _WIN32_WINNT >= 0x0600
361 CONDITION_VARIABLE __cond;
362 #else
363 Event __cond;
364 #endif
365#endif
366
367 public:
368 CondVar();
369 ~CondVar();
370 void signal();
371#if __DCL_PTHREAD || (defined(__WINNT__) && _WIN32_WINNT >= 0x0600)
372 void broadcast();
373#endif
374
380 bool wait(Mutex& _mutex, unsigned int _milliseconds = INFINITE);
381#if defined(__WINNT__) && _WIN32_WINNT >= 0x0600
382 bool wait(ReadWriteLock& _lock, bool shared, unsigned int _milliseconds = INFINITE);
383#endif
384 bool isWaiting() { return __bWaiting; }
385 private:
386 volatile bool __bWaiting;
387 };
388
389#if __DCL_PTHREAD && !defined(__APPLE__)
390 class DCLCAPI Barrier
391 {
392 private:
393 pthread_barrier_t __barrier;
394
395 public:
396 Barrier(unsigned int _count);
397 ~Barrier();
398 void wait();
399 };
400#endif
401
402 template<typename TYPE>
404 {
405 private:
406 TYPE* __lock; // Mutex or SpinLock
407
408 public:
409 SingleLock(TYPE& _lock);
411 };
412
414 #ifndef __APPLE__
416 #endif
417};
418
419template<typename TYPE>
421{
422 if (_lock.locker() != Thread::self()) {
423 __lock = &_lock;
424 __lock->lock();
425 }
426 else {
427 __lock = NULL;
428 }
429}
430
431template<typename TYPE>
433{
434 if (__lock) {
435 __lock->unlock();
436 }
437}
438
439__DCL_END_NAMESPACE
440
441#endif // __DCL_THREAD_H__
#define NULL
Definition Config.h:316
#define DCLCAPI
Definition Config.h:95
wchar_t char_t
Definition Config.h:251
#define _PROTECTED
Definition Config.h:331
#define __DCL_THROWS1(e)
Definition Config.h:152
#define DECLARE_CLASSINFO(class_name)
Definition Object.h:229
pthread_t thread_t
Definition Object.h:29
pthread_key_t thread_key_t
Definition Thread.h:39
#define INFINITE
Definition Thread.h:36
pthread_once_t thread_once_t
Definition Thread.h:38
Object()
Definition Object.cpp:183
virtual String toString() const
Definition Object.cpp:187
bool isWaiting()
Definition Thread.h:384
bool wait(Mutex &_mutex, unsigned int _milliseconds=INFINITE)
Definition Thread.cpp:662
void unlock()
Definition Thread.cpp:499
void lock()
Definition Thread.cpp:472
thread_t locker() const
Definition Thread.h:293
bool tryLock()
Definition Thread.cpp:482
SingleLock(TYPE &_lock)
Definition Thread.h:420
void(* initRoutine)()
Definition Thread.h:158
static void crtLock(const void *_p)
Definition Thread.cpp:269
static long incrementAndGet(volatile long &_n)
static void crtUnlock(const void *_p)
Definition Thread.cpp:275
static void yield()
Definition Thread.cpp:165
static void sleep(unsigned int _mills)
Definition Thread.cpp:152
static thread_t self()
Definition Thread.cpp:175
virtual bool init()
Definition Thread.cpp:99
static long decrementAndGet(volatile long &_n)
static Thread * getSelfThread()
Definition Thread.cpp:104
virtual int run()=0
SingleLock< SpinLock > SingleLockSpin
Definition Thread.h:415
SingleLock< Mutex > SingleLockMutex
Definition Thread.h:413