27 #ifndef __ETL__THREAD_H_
28 #define __ETL__THREAD_H_
42 #ifdef HAVE_CREATETHREAD
48 #if ( defined (HAVE_PTHREAD_CREATE) || defined (HAVE_CLONE) || defined (HAVE_CREATETHREAD) ) && !defined (NO_THREADS)
49 # define CALLISTO_THREADS
52 #define THREAD_ENTRYPOINT
56 #if defined(CALLISTO_THREADS) && defined(HAVE_PTHREAD_CREATE)
57 static inline void Yield(
void)
69 #ifdef CALLISTO_THREADS
71 #ifdef HAVE_PTHREAD_CREATE
76 typedef void* entrypoint_return;
81 entrypoint_return (*entrypoint)(
void *);
84 Thread(
void *(*ep)(
void *)=
NULL,
void *context=
NULL):
85 references(
NULL),entrypoint(ep),context(context) { }
86 Thread(
const Thread &t)
89 references=t.references;
90 entrypoint=t.entrypoint;
95 const Thread &operator=(
const Thread &rhs)
104 references=rhs.references;
105 entrypoint=rhs.entrypoint;
114 references =
new int;
116 pthread_create(&thread,
NULL,entrypoint,context);
125 pthread_cancel(thread);
126 pthread_join(thread,&exit_status);
129 static void TestStop()
131 pthread_testcancel();
134 static void SyncStop()
137 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,&i);
140 static void AsyncStop()
143 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,&i);
159 pthread_mutex_t
mutex;
166 pthread_mutexattr_t attr;
167 pthread_mutexattr_init(&attr);
171 #ifdef PTHREAD_MUTEX_RECURSIVE
172 pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE);
174 pthread_mutex_init(&
mutex,&attr);
175 pthread_mutexattr_destroy(&attr);
181 { pthread_mutex_destroy(&
mutex); }
185 if(!locker || locker!=pthread_self())
187 pthread_mutex_lock(&
mutex);
188 locker=pthread_self();
196 {
return !(bool) pthread_mutex_trylock(&
mutex); }
205 pthread_mutex_unlock(&
mutex);
210 #ifdef HAVE_PTHREAD_RW_LOCK_INIT
213 pthread_rwlock_t rwlock;
217 { pthread_rwlock_init(&rwlock,
NULL); }
220 { pthread_rwlock_destroy(&rwlock); }
223 { pthread_rwlock_rdlock(&rwlock); }
226 { pthread_rwlock_wrlock(&rwlock); }
229 {
return !(bool)pthread_rwlock_tryrdlock(&rwlock); }
232 {
return !(bool)pthread_rwlock_trywrlock(&rwlock); }
235 { pthread_rwlock_unlock(&rwlock); }
238 { pthread_rwlock_unlock(&rwlock); }
259 {
return TryLock(); }
262 {
return TryLock(); }
293 #else // if defined HAVE_PTHREAD
294 #ifdef HAVE_CREATETHREAD
297 #ifdef THREAD_ENTRYPOINT
298 #undef THREAD_ENTRYPOINT
300 #define THREAD_ENTRYPOINT __stdcall
304 typedef unsigned long entrypoint_return;
307 unsigned long thread;
320 Thread *thread=(Thread *)data;
323 wglMakeCurrent(thread->hdc, thread->hglrc);
325 return thread->entrypoint(thread->context);
330 references(
NULL),entrypoint(ep),context(context) { }
331 Thread(
const Thread &t)
335 references=t.references;
336 entrypoint=t.entrypoint;
342 const Thread &operator=(
const Thread &rhs)
352 references=rhs.references;
353 entrypoint=rhs.entrypoint;
362 references =
new int;
365 hglrc=wglGetCurrentContext();
366 hdc=wglGetCurrentDC();
383 TerminateThread(
handle, FALSE);
390 WaitForSingleObject(
handle, INFINITE);
396 static void TestStop()
400 static void SyncStop()
404 static void AsyncStop()
436 WaitForSingleObject(
handle, INFINITE);
441 return WaitForSingleObject(
handle, INFINITE)==WAIT_FAILED;
451 #endif // if defined HAVE_CREATETHREAD
452 #endif // if defined HAVE_PTHREAD_CREATE
453 #endif // if defined CALLISTO_THREADS
456 #if !defined(CALLISTO_THREADS)