X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/5bf49e9cedf84ccdf5c6cb604386d2c54f63436e..d224717198062a7db0c69aa4b653df0e80a1c891:/src/xbt/xbt_os_thread.c diff --git a/src/xbt/xbt_os_thread.c b/src/xbt/xbt_os_thread.c index c79377a68e..b2be4c7fe6 100644 --- a/src/xbt/xbt_os_thread.c +++ b/src/xbt/xbt_os_thread.c @@ -23,7 +23,6 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_sync_os, xbt, /* ********************************* PTHREAD IMPLEMENTATION ************************************ */ #ifdef HAVE_PTHREAD_H -#include #include #ifdef HAVE_MUTEX_TIMEDLOCK @@ -219,6 +218,24 @@ xbt_os_thread_t xbt_os_thread_self(void) return res; } +void xbt_os_thread_key_create(xbt_os_thread_key_t* key) { + + int errcode; + if ((errcode = pthread_key_create(key, NULL))) + THROWF(system_error, errcode, "pthread_key_create failed"); +} + +void xbt_os_thread_set_specific(xbt_os_thread_key_t key, void* value) { + + int errcode; + if ((errcode = pthread_setspecific(key, value))) + THROWF(system_error, errcode, "pthread_setspecific failed"); +} + +void* xbt_os_thread_get_specific(xbt_os_thread_key_t key) { + return pthread_getspecific(key); +} + void xbt_os_thread_detach(xbt_os_thread_t thread) { thread->detached = 1; @@ -242,12 +259,6 @@ typedef struct xbt_os_mutex_ { pthread_mutex_t m; } s_xbt_os_mutex_t; -typedef struct xbt_os_rmutex_ { - xbt_os_mutex_t mutex; - xbt_os_thread_t owner; - int count; -} s_xbt_os_rmutex_t; - #include #include @@ -484,7 +495,7 @@ xbt_os_sem_t xbt_os_sem_init(unsigned int value) if ((res->ps == (sem_t *) SEM_FAILED) && (errno == ENAMETOOLONG)) { /* Old darwins only allow 13 chars. Did you create *that* amount of semaphores? */ res->name[13] = '\0'; - res->ps = sem_open(res->name, O_CREAT, 0644, 1); + res->ps = sem_open(res->name, O_CREAT, 0644, value); } if ((res->ps == (sem_t *) SEM_FAILED)) THROWF(system_error, errno, "sem_open() failed: %s", strerror(errno)); @@ -732,6 +743,21 @@ void xbt_os_thread_exit(int *retval) ExitThread(0); } +void xbt_os_thread_key_create(xbt_os_thread_key_t* key) { + + *key = TlsAlloc(); +} + +void xbt_os_thread_set_specific(xbt_os_thread_key_t key, void* value) { + + if (!TlsSetValue(key, value)) + THROWF(system_error, (int) GetLastError(), "TlsSetValue() failed"); +} + +void* xbt_os_thread_get_specific(xbt_os_thread_key_t key) { + return TlsGetValue(key); +} + void xbt_os_thread_detach(xbt_os_thread_t thread) { THROW_UNIMPLEMENTED; @@ -1134,6 +1160,13 @@ void xbt_os_sem_get_value(xbt_os_sem_t sem, int *svalue) #endif +/***** reentrant mutexes *****/ +typedef struct xbt_os_rmutex_ { + xbt_os_mutex_t mutex; + xbt_os_thread_t owner; + int count; +} s_xbt_os_rmutex_t; + void xbt_os_thread_set_extra_data(void *data) { xbt_os_thread_self()->extra_data = data; @@ -1146,7 +1179,7 @@ void *xbt_os_thread_get_extra_data(void) xbt_os_rmutex_t xbt_os_rmutex_init(void) { - xbt_os_rmutex_t rmutex = xbt_new0(struct xbt_os_rmutex_, 0); + xbt_os_rmutex_t rmutex = xbt_new0(struct xbt_os_rmutex_, 1); rmutex->mutex = xbt_os_mutex_init(); rmutex->owner = NULL; rmutex->count = 0; @@ -1156,7 +1189,12 @@ xbt_os_rmutex_t xbt_os_rmutex_init(void) void xbt_os_rmutex_acquire(xbt_os_rmutex_t rmutex) { xbt_os_thread_t self = xbt_os_thread_self(); - xbt_assert(self != NULL, "Cannot get my own thread object (is the thread module initialized?)"); + + if (self == NULL) { + /* the thread module is not initialized yet */ + rmutex->owner = NULL; + return; + } if (self != rmutex->owner) { xbt_os_mutex_acquire(rmutex->mutex); @@ -1169,6 +1207,11 @@ void xbt_os_rmutex_acquire(xbt_os_rmutex_t rmutex) void xbt_os_rmutex_release(xbt_os_rmutex_t rmutex) { + if (rmutex->owner == NULL) { + /* the thread module was not initialized */ + return; + } + xbt_assert(rmutex->owner == xbt_os_thread_self()); if (--rmutex->count == 0) {