3 /* xbt_thread -- portability layer over the pthread API */
5 /* Copyright 2006,2007 Malek Cherier, Martin Quinson
6 * All right reserved. */
8 /* This program is free software; you can redistribute it and/or modify it
9 * under the terms of the license (GNU LGPL) which comes with this package. */
11 #include "xbt/sysdep.h"
14 #include "xbt/xbt_thread.h" /* This module */
15 #include "xbt_modinter.h" /* Initialization/finalization of this module */
19 /* ********************************* PTHREAD IMPLEMENTATION ************************************ */
23 typedef struct xbt_thread_ {
27 /* thread-specific data containing the xbt_thread_t structure */
28 static pthread_key_t xbt_self_thread_key;
30 /* frees the xbt_thread_t corresponding to the current thread */
31 static void xbt_thread_free_thread_data(void*d){
35 void xbt_thread_mod_init(void) {
38 if ((errcode=pthread_key_create(&xbt_self_thread_key, &xbt_thread_free_thread_data)))
39 THROW0(system_error,errcode,"pthread_key_create failed for xbt_self_thread_key");
41 void xbt_thread_mod_exit(void) {
44 if ((errcode=pthread_key_delete(xbt_self_thread_key)))
45 THROW0(system_error,errcode,"pthread_key_delete failed for xbt_self_thread_key");
49 xbt_thread_t xbt_thread_create(pvoid_f_pvoid_t start_routine,
51 xbt_thread_t res = xbt_new(s_xbt_thread_t,1);
54 if ((errcode=pthread_setspecific(xbt_self_thread_key,res)))
55 THROW0(system_error,errcode,"pthread_setspecific failed for xbt_self_thread_key");
57 if ((errcode = pthread_create(&(res->t), NULL, start_routine, param)))
58 THROW0(system_error,errcode, "pthread_create failed");
62 void xbt_thread_exit(int *retval) {
65 xbt_thread_t xbt_thread_self(void) {
66 return pthread_getspecific(xbt_self_thread_key);
70 void xbt_thread_yield(void) {
73 /****** mutex related functions ******/
74 typedef struct xbt_mutex_ {
78 xbt_mutex_t xbt_mutex_init(void) {
79 xbt_mutex_t res = xbt_new(s_xbt_mutex_t,1);
82 if ((errcode = pthread_mutex_init(&(res->m),NULL)))
83 THROW0(system_error,errcode,"pthread_mutex_init() failed");
88 void xbt_mutex_lock(xbt_mutex_t mutex) {
91 if ((errcode=pthread_mutex_lock(&(mutex->m))))
92 THROW1(system_error,errcode,"pthread_mutex_lock(%p) failed",mutex);
95 void xbt_mutex_unlock(xbt_mutex_t mutex) {
98 if ((errcode=pthread_mutex_unlock(&(mutex->m))))
99 THROW1(system_error,errcode,"pthread_mutex_unlock(%p) failed",mutex);
102 void xbt_mutex_destroy(xbt_mutex_t mutex) {
105 if ((errcode=pthread_mutex_destroy(&(mutex->m))))
106 THROW1(system_error,errcode,"pthread_mutex_destroy(%p) failed",mutex);
110 /***** condition related functions *****/
111 typedef struct xbt_thcond_ {
115 xbt_thcond_t xbt_thcond_init(void) {
116 xbt_thcond_t res = xbt_new(s_xbt_thcond_t,1);
118 if ((errcode=pthread_cond_init(&(res->c),NULL)))
119 THROW0(system_error,errcode,"pthread_cond_init() failed");
124 void xbt_thcond_wait(xbt_thcond_t cond, xbt_mutex_t mutex) {
126 if ((errcode=pthread_cond_wait(&(cond->c),&(mutex->m))))
127 THROW2(system_error,errcode,"pthread_cond_wait(%p,%p) failed",cond,mutex);
130 void xbt_thcond_signal(xbt_thcond_t cond) {
132 if ((errcode=pthread_cond_signal(&(cond->c))))
133 THROW1(system_error,errcode,"pthread_cond_signal(%p) failed",cond);
136 void xbt_thcond_broadcast(xbt_thcond_t cond){
138 if ((errcode=pthread_cond_broadcast(&(cond->c))))
139 THROW1(system_error,errcode,"pthread_cond_broadcast(%p) failed",cond);
141 void xbt_thcond_destroy(xbt_thcond_t cond){
143 if ((errcode=pthread_cond_destroy(&(cond->c))))
144 THROW1(system_error,errcode,"pthread_cond_destroy(%p) failed",cond);
148 /* ********************************* WINDOWS IMPLEMENTATION ************************************ */
152 typedef struct xbt_thread_ {
153 HANDLE handle; /* the win thread handle */
154 unsigned long id; /* the win thread id */
157 /* key to the TLS containing the xbt_thread_t structure */
158 static unsigned long xbt_self_thread_key;
160 void xbt_thread_mod_init(void) {
161 xbt_self_thread_key = TlsAlloc();
163 void xbt_thread_mod_exit(void) {
165 if (!(errcode = TlsFree(xbt_self_thread_key)))
166 THROW0(system_error,errcode,"TlsFree() failed to cleanup the thread submodule");
169 xbt_thread_t xbt_thread_create(pvoid_f_pvoid_t start_routine,
172 xbt_thread_t res = xbt_new(s_xbt_thread_t,1);
174 res->handle = CreateThread(NULL,0,
175 (LPTHREAD_START_ROUTINE)start_routine,
180 THROW0(system_error,0,"CreateThread failed");
183 if(!TlsSetValue(xbt_self_thread_key,res))
184 THROW0(system_error,0,"TlsSetValue of data describing the created thread failed");
189 void xbt_thread_exit(int *retval) {
190 xbt_thread_t self = xbt_thread_self();
192 CloseHandle(self->handle);
198 xbt_thread_t xbt_thread_self(void) {
199 return TlsGetValue(xbt_self_thread_key);
202 void xbt_thread_yield(void) {
206 /****** mutex related functions ******/
207 typedef struct xbt_mutex_ {
208 CRITICAL_SECTION lock;
211 xbt_mutex_t xbt_mutex_init(void) {
212 xbt_mutex_t res = xbt_new(s_xbt_mutex_t,1);
214 /* initialize the critical section object */
215 InitializeCriticalSection(&(res->lock));
220 void xbt_mutex_lock(xbt_mutex_t mutex) {
222 EnterCriticalSection(& mutex->lock);
225 void xbt_mutex_unlock(xbt_mutex_t mutex) {
227 LeaveCriticalSection (& mutex->lock);
231 void xbt_mutex_destroy(xbt_mutex_t mutex) {
233 DeleteCriticalSection(& mutex->lock);
238 /***** condition related functions *****/
245 typedef struct xbt_thcond_ {
246 HANDLE events[MAX_EVENTS];
248 unsigned int waiters_count; /* the number of waiters */
249 CRITICAL_SECTION waiters_count_lock; /* protect access to waiters_count */
252 xbt_thcond_t xbt_thcond_init(void) {
254 xbt_thcond_t res = xbt_new0(s_xbt_thcond_t,1);
256 memset(& res->waiters_count_lock,0,sizeof(CRITICAL_SECTION));
258 /* initialize the critical section object */
259 InitializeCriticalSection(& res->waiters_count_lock);
261 res->waiters_count = 0;
263 /* Create an auto-reset event */
264 res->events[SIGNAL] = CreateEvent (NULL, FALSE, FALSE, NULL);
266 if(!res->events[SIGNAL]){
267 DeleteCriticalSection(& res->waiters_count_lock);
269 THROW0(system_error,0,"CreateEvent failed for the signals");
272 /* Create a manual-reset event. */
273 res->events[BROADCAST] = CreateEvent (NULL, TRUE, FALSE,NULL);
275 if(!res->events[BROADCAST]){
277 DeleteCriticalSection(& res->waiters_count_lock);
278 CloseHandle(res->events[SIGNAL]);
280 THROW0(system_error,0,"CreateEvent failed for the broadcasts");
286 void xbt_thcond_wait(xbt_thcond_t cond, xbt_mutex_t mutex) {
288 unsigned long wait_result;
291 /* lock the threads counter and increment it */
292 EnterCriticalSection (& cond->waiters_count_lock);
293 cond->waiters_count++;
294 LeaveCriticalSection (& cond->waiters_count_lock);
296 /* unlock the mutex associate with the condition */
297 LeaveCriticalSection (& mutex->lock);
299 /* wait for a signal (broadcast or no) */
300 wait_result = WaitForMultipleObjects (2, cond->events, FALSE, INFINITE);
302 if(wait_result == WAIT_FAILED)
303 THROW0(system_error,0,"WaitForMultipleObjects failed, so we cannot wait on the condition");
305 /* we have a signal lock the condition */
306 EnterCriticalSection (& cond->waiters_count_lock);
307 cond->waiters_count--;
309 /* it's the last waiter or it's a broadcast ? */
310 is_last_waiter = ((wait_result == WAIT_OBJECT_0 + BROADCAST - 1) && (cond->waiters_count == 0));
312 LeaveCriticalSection (& cond->waiters_count_lock);
314 /* yes it's the last waiter or it's a broadcast
315 * only reset the manual event (the automatic event is reset in the WaitForMultipleObjects() function
319 if(!ResetEvent (cond->events[BROADCAST]))
320 THROW0(system_error,0,"ResetEvent failed");
322 /* relock the mutex associated with the condition in accordance with the posix thread specification */
323 EnterCriticalSection (& mutex->lock);
326 void xbt_thcond_signal(xbt_thcond_t cond) {
329 EnterCriticalSection (& cond->waiters_count_lock);
330 have_waiters = cond->waiters_count > 0;
331 LeaveCriticalSection (& cond->waiters_count_lock);
334 if(!SetEvent(cond->events[SIGNAL]))
335 THROW0(system_error,0,"SetEvent failed");
338 void xbt_thcond_broadcast(xbt_thcond_t cond){
341 EnterCriticalSection (& cond->waiters_count_lock);
342 have_waiters = cond->waiters_count > 0;
343 LeaveCriticalSection (& cond->waiters_count_lock);
346 SetEvent(cond->events[BROADCAST]);
349 void xbt_thcond_destroy(xbt_thcond_t cond){
352 if(!CloseHandle(cond->events[SIGNAL]))
355 if(!CloseHandle(cond->events[BROADCAST]))
358 DeleteCriticalSection(& cond->waiters_count_lock);
363 THROW0(system_error,0,"Error while destroying the condition");