1#ifndef __DCL_THREAD_H__
2#define __DCL_THREAD_H__ 20110109
4#ifndef __DCL_CONFIG_H__
10 #error "Required windows.h, See dcl/_windows.h"
16 #include <bits/pthreadtypes.h>
20#ifndef __DCL_OBJECT_H__
23#ifndef __DCL_STRING_H__
67 bool started()
const {
return __threadId != 0; }
72 unsigned long id()
const {
return __threadId; }
77 const String& name()
const {
return __name; }
96 virtual int run() = 0;
99 unsigned long __threadId;
103 static void* startRoutine(
void* _pThread);
106 static DWORD WINAPI startRoutine(
void* _pThread);
113 static void sleep(
unsigned int _mills);
136 typedef pthread_once_t OnceType;
137 enum { ONCE_INIT = 0 };
139 typedef long OnceType;
140 enum { ONCE_INIT = (long)0 };
155 static void once(OnceType& _onceControl,
initRoutine _initRoutine);
158 typedef pthread_key_t KeyType;
160 typedef DWORD KeyType;
220 static
void crtLock(const
void* _p);
233 enum { READ_FD_INDEX = 0 };
241 int handle() {
return __fds[READ_FD_INDEX]; }
243 HANDLE handle() {
return __hEvent; }
257 bool wait(
unsigned int _milliseconds =
INFINITE);
258 bool isWaiting() {
return __bWaiting; }
260 volatile bool __bWaiting;
267 pthread_mutex_t __mutex;
269 CRITICAL_SECTION __cs;
274 Mutex(DWORD _dwSpinCount);
288 pthread_spinlock_t __spinLock;
291 SpinLock(
bool _pshared =
false);
298 class DCLCAPI SpinLock :
public Mutex
306#if __DCL_PTHREAD || (__DCL_WINDOWS && _WIN32_WINNT >= 0x0600)
311 pthread_rwlock_t __rwlock;
341 pthread_cond_t __cond;
343 #if _WIN32_WINNT >= 0x0600
344 CONDITION_VARIABLE __cond;
354#if __DCL_PTHREAD || (__DCL_WINDOWS && _WIN32_WINNT >= 0x0600)
364#if __DCL_WINDOWS && _WIN32_WINNT >= 0x0600
365 bool wait(ReadWriteLock& _lock,
bool shared,
unsigned int _milliseconds =
INFINITE);
369 volatile bool __bWaiting;
376 pthread_barrier_t __barrier;
379 Barrier(
unsigned int _count);
385 template<
typename TYPE>
406 template<
typename TYPE>
415 __locked = __lock.tryLock();
433 return InterlockedIncrement(&_n);
438 return InterlockedDecrement(&_n);
441#if _WIN32_WINNT >= 0x0600
444 return InterlockedDecrement64(&_n);
449 return InterlockedIncrement64(&_n);
#define DECLARE_CLASSINFO(class_name)
virtual String toString() const
bool wait(Mutex &_mutex, unsigned int _milliseconds=INFINITE)
SingleTryLock(TYPE &_lock)
static void crtLock(const void *_p)
SingleTryLock< Mutex > SingleTryLockMutex
static long incrementAndGet(volatile long &_n)
static void crtUnlock(const void *_p)
SingleTryLock< SpinLock > SingleTryLockSpin
static void sleep(unsigned int _mills)
static unsigned long getCurrentThreadId()
static Thread * getCurrentThread()
static long decrementAndGet(volatile long &_n)
SingleLock< SpinLock > SingleLockSpin
SingleLock< Mutex > SingleLockMutex