21 static ThreadMutex
*threads_mutex
;
23 static Thread
*mainThread
;
25 #define LOCK_THEN_UNLOCK(var, code) do { \
26 ThreadMutex_lock(var ## _mutex); \
28 ThreadMutex_unlock(var ## _mutex); \
31 ThreadReturnCode
Thread_Init(void)
34 HANDLE rawMainThread
= GetCurrentThread();
36 pthread_t rawMainThread
= pthread_self();
41 return THREAD_SUCCESS
; // already initialized
45 threads_mutex
= ThreadMutex_new();
47 mainThread
= Thread_new();
50 return THREAD_FAILURE
;
53 mainThread
->thread
= rawMainThread
;
55 mainThread
->id
= GetCurrentThreadId();
58 return THREAD_SUCCESS
;
61 void Thread_Shutdown(void)
63 // need to shutdown threads
64 //if (List_size(threads) > 0)
68 LOCK_THEN_UNLOCK(threads
,
69 List_remove_(threads
, (void *)mainThread
);
71 Thread_destroy(mainThread
);
78 LOCK_THEN_UNLOCK(threads
,
82 ThreadMutex_destroy(threads_mutex
);
90 Thread
*Thread_CurrentThread(void)
93 HANDLE rawCurrentThread
= GetCurrentThread();
95 pthread_t rawCurrentThread
= pthread_self();
99 Thread
*currentThread
= NULL
;
101 LOCK_THEN_UNLOCK(threads
,
102 LIST_FOREACH(threads
, index
, thread
,
103 isSameThread
= (((Thread
*)thread
)->thread
== rawCurrentThread
);
106 currentThread
= thread
;
112 LOCK_THEN_UNLOCK(threads
,
113 LIST_FOREACH(threads
, index
, thread
,
114 isSameThread
= pthread_equal(((Thread
*)thread
)->thread
, rawCurrentThread
);
117 currentThread
= thread
;
128 fprintf(stderr
, "\nYou found a bug in libThread. Please tell trevor on freenode or email trevor@fancher.org.\n");
133 return currentThread
;
136 ThreadReturnCode
Thread_WaitOnThread_(Thread
*thread
)
140 WaitForSingleObject(thread
->thread
, INFINITE
);
142 err
= pthread_join(thread
->thread
, NULL
);
145 return THREAD_FAILURE
;
148 return THREAD_SUCCESS
;
151 List
*Thread_Threads(void)
153 return List_clone(threads
);
156 Thread
*Thread_new(void)
158 Thread
*self
= malloc(sizeof(Thread
));
162 self
->funcArg
= NULL
;
164 LOCK_THEN_UNLOCK(threads
,
165 List_append_(threads
, (void *)self
);
171 Thread
*Thread_newWithFunc_arg_(ThreadFunc func
, void *funcArg
)
173 Thread
*self
= Thread_new();
176 Thread_setFunc_arg_(self
, func
, funcArg
);
181 void Thread_destroy(Thread
*self
)
183 LOCK_THEN_UNLOCK(threads
,
184 List_remove_(threads
, (void *)self
);
189 void Thread_setFunc_(Thread
*self
, ThreadFunc func
)
194 void Thread_setFuncArg_(Thread
*self
, void *funcArg
)
196 self
->funcArg
= funcArg
;
199 void Thread_setFunc_arg_(Thread
*self
, ThreadFunc func
, void *funcArg
)
201 Thread_setFunc_(self
, func
);
202 Thread_setFuncArg_(self
, funcArg
);
205 ThreadReturnCode
Thread_start(Thread
*self
)
208 self
->thread
= CreateThread(NULL
, 0, (LPTHREAD_START_ROUTINE
)(self
->func
), self
->funcArg
, 0, &(self
->id
));
211 err
= pthread_create(&(self
->thread
), NULL
, self
->func
, self
->funcArg
);
214 return THREAD_FAILURE
;
219 return THREAD_SUCCESS
;
222 ThreadReturnCode
Thread_stop(Thread
*self
)
225 TerminateThread(self
->thread
, 0);
227 pthread_cancel(self
->thread
);
231 return THREAD_SUCCESS
;
234 void Thread_exitWithValue_(Thread
*self
, void *returnValue
)
236 self
->returnValue
= returnValue
;
244 ThreadFunc
Thread_func(Thread
*self
)
249 void *Thread_funcArg(Thread
*self
)
251 return self
->funcArg
;
254 void *Thread_userData(Thread
*self
)
256 return self
->userData
;
259 void Thread_setUserData_(Thread
*self
, void *userData
)
261 self
->userData
= userData
;