5 #include <VersionHelpers.h>
13 #include <sys/filio.h>
24#if __DCL_HAVE_THIS_FILE__
41Thread::Thread(
const char_t* _name )
53 #define __key_t pthread_key_t
54 #define __key_create(key) pthread_key_create(&key, NULL)
55 #define __key_delete(key) pthread_key_delete(key)
56 #define __key_get(key) pthread_getspecific(key)
57 #define __key_set(key, value) pthread_setspecific(key, value)
58 #define __key_set_success(key, value) pthread_setspecific(key, value) == 0
59#elif defined(__WINNT__)
61 #define __key_create(key) key = TlsAlloc()
62 #define __key_delete(key) TlsFree(key)
63 #define __key_get(key) TlsGetValue(key)
64 #define __key_set(key, value) TlsSetValue(key, value)
65 #define __key_set_success(key, value) TlsSetValue(key, value) == TRUE
68static __key_t __keySelfThread = (__key_t) -1;
76 int n = pthread_create(
78 (pthread_attr_t*)
NULL,
79 Thread::startRoutine, (
void*)
this
82 throw new SysError(n);
83#elif defined(__WINNT__)
85 __hThread = CreateThread(
94 if (__hThread ==
NULL)
95 throw new SysError(GetLastError());
101 return __key_set_success(__keySelfThread,
this);
107 return (Thread*) __key_get(__keySelfThread);
112#elif defined(__WINNT__)
115Thread::startRoutine(
void* _pThread)
123 catch (Exception* cause) {
129 return (
void*)(size_t)
r;
130#elif defined(__WINNT__)
139 pthread_join(__threadId, &
r);
140#elif defined(__WINNT__)
142 WaitForSingleObject(__hThread,
INFINITE);
143 GetExitCodeThread(__hThread, &
r);
144 CloseHandle(__hThread);
149 return (
int)(size_t)
r;
159 poll(
NULL, 0, _mills);
160#elif defined(__WINNT__)
170#elif defined(__WINNT__)
178 return pthread_self();
179#elif defined(__WINNT__)
180 return GetCurrentThreadId();
184void Thread::once(
thread_once_t& _onceControl, initRoutine _initRoutine)
188 pthread_once(&_onceControl, _initRoutine);
189#elif defined(__WINNT__)
190 InitOnceExecuteOnce(&_onceControl, (PINIT_ONCE_FN)_initRoutine,
NULL,
NULL);
198 int failed = pthread_key_create(&rKey,
NULL);
200 throw new SysError(failed);
202#elif defined(__WINNT__)
204 if (rKey == TLS_OUT_OF_INDEXES)
205 throw new SysError(GetLastError());
213 int failed = pthread_key_delete(_key);
216#elif defined(__WINNT__)
224 int failed = pthread_setspecific(_key, _value);
227#elif defined(__WINNT__)
228 if (!TlsSetValue(_key, _value))
235 return pthread_getspecific(_key);
236#elif defined(__WINNT__)
237 void* p = TlsGetValue(_key);
238 if (p ==
NULL && GetLastError() != ERROR_SUCCESS)
245#if defined(__APPLE__)
246 #define __lock_t pthread_mutex_t
247 #define __init(lock) pthread_mutex_init(&lock, NULL)
248 #define __destroy(lock) pthread_mutex_destroy(&lock)
249 #define __lock(lock) pthread_mutex_lock(&lock)
250 #define __unlock(lock) pthread_mutex_unlock(&lock)
252 #define __lock_t pthread_spinlock_t
253 #define __init(lock) pthread_spin_init(&lock, PTHREAD_PROCESS_PRIVATE)
254 #define __destroy(lock) pthread_spin_destroy(&lock)
255 #define __lock(lock) pthread_spin_lock(&lock)
256 #define __unlock(lock) pthread_spin_unlock(&lock)
258#elif defined(__WINNT__)
259 #define __lock_t CRITICAL_SECTION
260 #define __init(lock) InitializeCriticalSectionAndSpinCount(&lock, INFINITE)
261 #define __destroy(lock) DeleteCriticalSection(&lock)
262 #define __lock(lock) EnterCriticalSection(&lock)
263 #define __unlock(lock) LeaveCriticalSection(&lock)
266#define __LOCK_COUNT 7
304 long long n = ++(_n);
313 long long n = --(_n);
317#elif defined(__WINNT__)
320 return InterlockedIncrement(&_n);
325 return InterlockedDecrement(&_n);
330 return InterlockedDecrement64(&_n);
335 return InterlockedIncrement64(&_n);
341 __key_create(__keySelfThread);
349 __key_delete(__keySelfThread);
352 __destroy(__lock[i]);
355Thread::Event::Event()
361#elif defined(__WINNT__)
372Thread::Event::~Event()
377#elif defined(__WINNT__)
382void Thread::Event::set()
386 __DCL_VERIFY(write(__fds[1], &c,
sizeof(c)) ==
sizeof(c));
387#elif defined(__WINNT__)
392void Thread::Event::reset()
396 ioctl(__fds[READ_FD_INDEX], FIONREAD, &nbytes);
399 int n = read(__fds[0], buf, nbytes < 100 ? nbytes : 100);
407#elif defined(__WINNT__)
412bool Thread::Event::wait(
unsigned int _milliseconds)
418 pfd.fd = __fds[READ_FD_INDEX];
422 int n = poll(&pfd, 1, _milliseconds);
428#elif defined(__WINNT__)
429 switch(WaitForSingleObject(__hEvent, _milliseconds)) {
434 SetLastError(ERROR_TIMEOUT);
449 InitializeCriticalSectionAndSpinCount(&__cs, _dwSpinCount);
458#elif defined(__WINNT__)
459 InitializeCriticalSection(&__cs);
467#elif defined(__WINNT__)
468 DeleteCriticalSection(&__cs);
476#elif defined(__WINNT__)
477 EnterCriticalSection(&__cs);
486 int r_ = pthread_mutex_trylock(&__mutex);
490#elif defined(__WINNT__)
491 r = TryEnterCriticalSection(&__cs) ==
TRUE;
505#elif defined(__WINNT__)
506 LeaveCriticalSection(&__cs);
510#if __DCL_PTHREAD && !defined(__APPLE__)
511Thread::SpinLock::SpinLock(
bool _pshared )
515 _pshared ? PTHREAD_PROCESS_SHARED : PTHREAD_PROCESS_PRIVATE
519Thread::SpinLock::~SpinLock()
524void Thread::SpinLock::lock()
530bool Thread::SpinLock::tryLock()
532 int r_ = pthread_spin_trylock(&__spinLock);
542void Thread::SpinLock::unlock()
550#if __DCL_PTHREAD || (defined(__WINNT__) && _WIN32_WINNT >= 0x0600)
551Thread::ReadWriteLock::ReadWriteLock()
556 InitializeSRWLock(&__srwlock);
560Thread::ReadWriteLock::~ReadWriteLock()
567void Thread::ReadWriteLock::readLock()
572 AcquireSRWLockShared(&__srwlock);
576void Thread::ReadWriteLock::writeLock()
581 AcquireSRWLockExclusive(&__srwlock);
586bool Thread::ReadWriteLock::tryReadLock()
588 int r = pthread_rwlock_tryrdlock(&__rwlock);
594bool Thread::ReadWriteLock::tryWriteLock()
596 int r = pthread_rwlock_trywrlock(&__rwlock);
602void Thread::ReadWriteLock::unlock()
607void Thread::ReadWriteLock::readUnlock()
609 ReleaseSRWLockShared(&__srwlock);
611void Thread::ReadWriteLock::writeUnlock()
613 ReleaseSRWLockExclusive(&__srwlock);
623#elif defined(__WINNT__) && _WIN32_WINNT >= 0x0600
626 InitializeConditionVariable(&__cond);
642#elif defined(__WINNT__)
643 #if _WIN32_WINNT >= 0x0600
644 WakeConditionVariable(&__cond);
651#if __DCL_PTHREAD || (defined(__WINNT__) && _WIN32_WINNT >= 0x0600)
652void Thread::CondVar::broadcast()
656#elif defined(__WINNT__)
657 WakeAllConditionVariable(&__cond);
668 if (_milliseconds == (
unsigned int)
INFINITE) {
669 __DCL_VERIFY(pthread_cond_wait(&__cond, (pthread_mutex_t*)&_mutex) == 0);
680 ts.tv_sec = tv.tv_sec + (_milliseconds / 1000);
681 ts.tv_nsec = (tv.tv_usec * 1000) + ((_milliseconds % 1000) * 1000000);
682 int n = pthread_cond_timedwait(&__cond, (pthread_mutex_t*)&_mutex, &ts);
689#elif defined(__WINNT__)
690 #if _WIN32_WINNT >= 0x0600
691 if (!SleepConditionVariableCS(&__cond,
692 (CRITICAL_SECTION*)&_mutex, _milliseconds)) {
709#if defined(__WINNT__) && _WIN32_WINNT >= 0x0600
714 if (!SleepConditionVariableSRW(&__cond, (SRWLOCK *)&_lock,
715 _shared ? CONDITION_VARIABLE_LOCKMODE_SHARED : 0, _milliseconds)) {
724#if __DCL_PTHREAD && !defined(__APPLE__)
725Thread::Barrier::Barrier(
unsigned int _count)
730Thread::Barrier::~Barrier()
735void Thread::Barrier::wait()
#define __DCL_ASSERT_PARAM(expr)
#define __DCL_VERIFY(expr)
#define __DCL_ASSERT(expr)
#define IMPLEMENT_CLASSINFO(class_name, base_class_name)
#define __DCL_TRACE2(fmt, arg1, arg2)
void __cleanupThreadEnvironment()
void __initializeThreadEnvironment()
pthread_key_t thread_key_t
pthread_once_t thread_once_t
String toStringAll() const
virtual String toString() const
bool wait(Mutex &_mutex, unsigned int _milliseconds=INFINITE)
static void crtLock(const void *_p)
static long incrementAndGet(volatile long &_n)
static void crtUnlock(const void *_p)
static void sleep(unsigned int _mills)
static long decrementAndGet(volatile long &_n)
static Thread * getSelfThread()