Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
e530de4399dc1b2fdca06d97900a76366897247b
[simgrid.git] / src / xbt / xbt_os_thread.c
1 /* xbt_os_thread -- portability layer over the pthread API                  */
2 /* Used in RL to get win/lin portability, and in SG when CONTEXT_THREAD     */
3 /* in SG, when using HAVE_UCONTEXT_CONTEXTS, xbt_os_thread_stub is used instead   */
4
5 /* Copyright (c) 2007-2015. The SimGrid Team.
6  * All rights reserved.                                                     */
7
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. */
10
11 #if defined(WIN32)
12 #elif defined(__MACH__) && defined(__APPLE__)
13 #include <stdint.h>
14 #include <sys/types.h>
15 #include <sys/sysctl.h>
16 #else
17 #include <unistd.h>
18 #endif
19
20 #include "src/internal_config.h"
21 #include "xbt/sysdep.h"
22 #include "xbt/ex.h"
23 #include "src/xbt/ex_interface.h"   /* We play crude games with exceptions */
24 #include "src/portable.h"
25 #include "xbt/xbt_os_time.h"    /* Portable time facilities */
26 #include "xbt/xbt_os_thread.h"  /* This module */
27 #include "src/xbt_modinter.h"       /* Initialization/finalization of this module */
28
29 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_sync_os, xbt, "Synchronization mechanism (OS-level)");
30
31 /* ********************************* PTHREAD IMPLEMENTATION ************************************ */
32 #include <pthread.h>
33 #include <limits.h>
34 #include <semaphore.h>
35
36 #ifdef CORE_BINDING
37 #define _GNU_SOURCE
38 #include <sched.h>
39 #endif
40
41 /* use named sempahore when sem_init() does not work */
42 #ifndef HAVE_SEM_INIT
43 static int next_sem_ID = 0;
44 static xbt_os_mutex_t next_sem_ID_lock;
45 #endif
46
47 typedef struct xbt_os_thread_ {
48   pthread_t t;
49   int detached;
50   char *name;
51   void *param;
52   pvoid_f_pvoid_t start_routine;
53   xbt_running_ctx_t *running_ctx;
54   void *extra_data;
55 } s_xbt_os_thread_t;
56 static xbt_os_thread_t main_thread = NULL;
57
58 /* thread-specific data containing the xbt_os_thread_t structure */
59 static pthread_key_t xbt_self_thread_key;
60 static int thread_mod_inited = 0;
61
62 /* defaults attribute for pthreads */
63 //FIXME: find where to put this
64 static pthread_attr_t thread_attr;
65
66 /* frees the xbt_os_thread_t corresponding to the current thread */
67 static void xbt_os_thread_free_thread_data(xbt_os_thread_t thread)
68 {
69   if (thread == main_thread)    /* just killed main thread */
70     main_thread = NULL;
71
72   free(thread->running_ctx);
73   free(thread->name);
74   free(thread);
75 }
76
77 /* callback: context fetching */
78 static xbt_running_ctx_t *_os_thread_get_running_ctx(void)
79 {
80   return xbt_os_thread_self()->running_ctx;
81 }
82
83 /* callback: termination */
84 static void _os_thread_ex_terminate(xbt_ex_t * e)
85 {
86   xbt_ex_display(e);
87   xbt_abort();
88   /* FIXME: there should be a configuration variable to choose to kill everyone or only this one */
89 }
90
91 void xbt_os_thread_mod_preinit(void)
92 {
93   if (thread_mod_inited)
94     return;
95
96   int errcode = pthread_key_create(&xbt_self_thread_key, NULL);
97   xbt_assert(errcode == 0, "pthread_key_create failed for xbt_self_thread_key");
98   
99   main_thread = xbt_new(s_xbt_os_thread_t, 1);
100   main_thread->name = NULL;
101   main_thread->detached = 0;
102   main_thread->name = (char *) "main";
103   main_thread->param = NULL;
104   main_thread->start_routine = NULL;
105   main_thread->running_ctx = xbt_new(xbt_running_ctx_t, 1);
106   main_thread->extra_data = NULL;
107   XBT_RUNNING_CTX_INITIALIZE(main_thread->running_ctx);
108
109   if ((errcode = pthread_setspecific(xbt_self_thread_key, main_thread)))
110     THROWF(system_error, errcode,
111            "Impossible to set the SimGrid identity descriptor to the main thread (pthread_setspecific failed)");
112   
113   __xbt_running_ctx_fetch = _os_thread_get_running_ctx;
114   __xbt_ex_terminate = _os_thread_ex_terminate;
115
116   pthread_attr_init(&thread_attr);
117
118   thread_mod_inited = 1;
119
120 #ifndef HAVE_SEM_INIT
121   next_sem_ID_lock = xbt_os_mutex_init();
122 #endif
123 }
124
125 void xbt_os_thread_mod_postexit(void)
126 {
127   /* FIXME: don't try to free our key on shutdown.
128      Valgrind detects no leak if we don't, and whine if we try to */
129   //   int errcode;
130
131   //   if ((errcode=pthread_key_delete(xbt_self_thread_key)))
132   //     THROWF(system_error,errcode,"pthread_key_delete failed for xbt_self_thread_key");
133   free(main_thread->running_ctx);
134   free(main_thread);
135   main_thread = NULL;
136   thread_mod_inited = 0;
137 #ifndef HAVE_SEM_INIT
138   xbt_os_mutex_destroy(next_sem_ID_lock);
139 #endif
140
141   /* Restore the default exception setup */
142   __xbt_running_ctx_fetch = &__xbt_ex_ctx_default;
143   __xbt_ex_terminate = &__xbt_ex_terminate_default;
144 }
145
146 /* this function is critical to tesh+mmalloc, don't mess with it */
147 int xbt_os_thread_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(void))
148 {
149   return pthread_atfork(prepare, parent, child);
150 }
151
152 static void *wrapper_start_routine(void *s)
153 {
154   xbt_os_thread_t t = s;
155
156   int errcode = pthread_setspecific(xbt_self_thread_key, t);
157   xbt_assert(errcode == 0, "pthread_setspecific failed for xbt_self_thread_key");
158
159   void *res = t->start_routine(t->param);
160   if (t->detached)
161     xbt_os_thread_free_thread_data(t);
162   return res;
163 }
164
165
166 xbt_os_thread_t xbt_os_thread_create(const char *name,  pvoid_f_pvoid_t start_routine, void *param, void *extra_data)
167 {
168   xbt_os_thread_t res_thread = xbt_new(s_xbt_os_thread_t, 1);
169   res_thread->detached = 0;
170   res_thread->name = xbt_strdup(name);
171   res_thread->start_routine = start_routine;
172   res_thread->param = param;
173   res_thread->running_ctx = xbt_new(xbt_running_ctx_t, 1);
174   XBT_RUNNING_CTX_INITIALIZE(res_thread->running_ctx);
175   res_thread->extra_data = extra_data;
176   
177   int errcode = pthread_create(&(res_thread->t), &thread_attr, wrapper_start_routine, res_thread);
178   xbt_assert(errcode == 0, "pthread_create failed: %s", strerror(errcode));
179
180   return res_thread;
181 }
182
183
184 #ifdef CORE_BINDING
185 int xbt_os_thread_bind(xbt_os_thread_t thread, int cpu){
186   pthread_t pthread = thread->t;
187   int errcode = 0;
188   cpu_set_t cpuset;
189   CPU_ZERO(&cpuset);
190   CPU_SET(cpu, &cpuset);
191   errcode = pthread_setaffinity_np(pthread, sizeof(cpu_set_t), &cpuset);
192   return errcode;
193 }
194 #endif
195
196 void xbt_os_thread_setstacksize(int stack_size)
197 {
198   size_t alignment[] = {
199     xbt_pagesize,
200 #ifdef PTHREAD_STACK_MIN
201     PTHREAD_STACK_MIN,
202 #endif
203     0
204   };
205
206   xbt_assert(stack_size >= 0, "stack size %d is negative, maybe it exceeds MAX_INT?", stack_size);
207
208   size_t sz = stack_size;
209   int res = pthread_attr_setstacksize(&thread_attr, sz);
210
211   for (int i = 0; res == EINVAL && alignment[i] > 0; i++) {
212     /* Invalid size, try again with next multiple of alignment[i]. */
213     size_t rem = sz % alignment[i];
214     if (rem != 0 || sz == 0) {
215       size_t sz2 = sz - rem + alignment[i];
216       XBT_DEBUG("pthread_attr_setstacksize failed for %zd, try again with %zd", sz, sz2);
217       sz = sz2;
218       res = pthread_attr_setstacksize(&thread_attr, sz);
219     }
220   }
221
222   if (res == EINVAL)
223     XBT_WARN("invalid stack size (maybe too big): %zd", sz);
224   else if (res != 0)
225     XBT_WARN("unknown error %d in pthread stacksize setting: %zd", res, sz);
226 }
227
228 void xbt_os_thread_setguardsize(int guard_size)
229 {
230 #ifdef WIN32
231   THROW_UNIMPLEMENTED; //pthread_attr_setguardsize is not implemented in pthread.h on windows
232 #else
233   size_t sz = guard_size;
234   int res = pthread_attr_setguardsize(&thread_attr, sz);
235   if (res)
236     XBT_WARN("pthread_attr_setguardsize failed (%d) for size: %zd", res, sz);
237 #endif
238 }
239
240 const char *xbt_os_thread_name(xbt_os_thread_t t)
241 {
242   return t->name;
243 }
244
245 const char *xbt_os_thread_self_name(void)
246 {
247   xbt_os_thread_t me = xbt_os_thread_self();
248   return me ? me->name : "main";
249 }
250
251 void xbt_os_thread_join(xbt_os_thread_t thread, void **thread_return)
252 {
253   int errcode = pthread_join(thread->t, thread_return);
254
255   xbt_assert(errcode==0, "pthread_join failed: %s", strerror(errcode));
256   xbt_os_thread_free_thread_data(thread);
257 }
258
259 void xbt_os_thread_exit(int *retval)
260 {
261   pthread_exit(retval);
262 }
263
264 xbt_os_thread_t xbt_os_thread_self(void)
265 {
266   if (!thread_mod_inited)
267     return NULL;
268
269   return pthread_getspecific(xbt_self_thread_key);
270 }
271
272 void xbt_os_thread_key_create(xbt_os_thread_key_t* key)
273 {
274   int errcode = pthread_key_create(key, NULL);
275   xbt_assert(errcode==0 , "pthread_key_create failed");
276 }
277
278 void xbt_os_thread_set_specific(xbt_os_thread_key_t key, void* value) {
279
280   int errcode = pthread_setspecific(key, value);
281   xbt_assert(errcode==0, "pthread_setspecific failed");
282 }
283
284 void* xbt_os_thread_get_specific(xbt_os_thread_key_t key) {
285   return pthread_getspecific(key);
286 }
287
288 void xbt_os_thread_detach(xbt_os_thread_t thread)
289 {
290   thread->detached = 1;
291   pthread_detach(thread->t);
292 }
293
294 #include <sched.h>
295 void xbt_os_thread_yield(void)
296 {
297   sched_yield();
298 }
299
300 void xbt_os_thread_cancel(xbt_os_thread_t t)
301 {
302   pthread_cancel(t->t);
303 }
304
305 /****** mutex related functions ******/
306 typedef struct xbt_os_mutex_ {
307   /* KEEP IT IN SYNC WITH xbt_thread.c */
308   pthread_mutex_t m;
309 } s_xbt_os_mutex_t;
310
311 #include <time.h>
312 #include <math.h>
313
314 xbt_os_mutex_t xbt_os_mutex_init(void)
315 {
316   xbt_os_mutex_t res = xbt_new(s_xbt_os_mutex_t, 1);
317   int errcode = pthread_mutex_init(&(res->m), NULL);
318   xbt_assert(errcode==0, "pthread_mutex_init() failed: %s", strerror(errcode));
319
320   return res;
321 }
322
323 void xbt_os_mutex_acquire(xbt_os_mutex_t mutex)
324 {
325   int errcode = pthread_mutex_lock(&(mutex->m));
326   xbt_assert(errcode==0, "pthread_mutex_lock(%p) failed: %s", mutex, strerror(errcode));
327 }
328
329
330 void xbt_os_mutex_timedacquire(xbt_os_mutex_t mutex, double delay)
331 {
332   int errcode;
333
334   if (delay < 0) {
335     xbt_os_mutex_acquire(mutex);
336
337   } else if (delay == 0) {
338     errcode = pthread_mutex_trylock(&(mutex->m));
339
340     switch (errcode) {
341     case 0:
342       return;
343     case ETIMEDOUT:
344       THROWF(timeout_error, 0, "mutex %p not ready", mutex);
345     default:
346       THROWF(system_error, errcode,
347              "xbt_os_mutex_timedacquire(%p) failed: %s", mutex,
348              strerror(errcode));
349     }
350
351
352   } else {
353
354 #ifdef HAVE_MUTEX_TIMEDLOCK
355     struct timespec ts_end;
356     double end = delay + xbt_os_time();
357
358     ts_end.tv_sec = (time_t) floor(end);
359     ts_end.tv_nsec = (long) ((end - ts_end.tv_sec) * 1000000000);
360     XBT_DEBUG("pthread_mutex_timedlock(%p,%p)", &(mutex->m), &ts_end);
361
362     int errcode = pthread_mutex_timedlock(&(mutex->m), &ts_end);
363
364 #else            /* reimplement it since those lazy libc dudes didn't (Mac OSX, hu?) */
365     double start = xbt_os_time();
366     do {
367       errcode = pthread_mutex_trylock(&(mutex->m));
368       if (errcode == EBUSY)
369         xbt_os_thread_yield();
370     } while (errcode == EBUSY && xbt_os_time() - start < delay);
371
372     if (errcode == EBUSY)
373       errcode = ETIMEDOUT;
374
375 #endif                          /* HAVE_MUTEX_TIMEDLOCK */
376
377     switch (errcode) {
378     case 0:
379       return;
380
381     case ETIMEDOUT:
382       THROWF(timeout_error, delay,
383              "mutex %p wasn't signaled before timeout (%f)", mutex, delay);
384
385     default:
386       THROWF(system_error, errcode,
387              "pthread_mutex_timedlock(%p,%f) failed: %s", mutex, delay,
388              strerror(errcode));
389     }
390   }
391 }
392
393 void xbt_os_mutex_release(xbt_os_mutex_t mutex)
394 {
395   int errcode = pthread_mutex_unlock(&(mutex->m));
396   xbt_assert(errcode==0, "pthread_mutex_unlock(%p) failed: %s", mutex, strerror(errcode));
397 }
398
399 void xbt_os_mutex_destroy(xbt_os_mutex_t mutex)
400 {
401   if (!mutex)
402     return;
403
404   int errcode = pthread_mutex_destroy(&(mutex->m));
405   xbt_assert(errcode == 0, "pthread_mutex_destroy(%p) failed: %s", mutex, strerror(errcode));
406   free(mutex);
407 }
408
409 /***** condition related functions *****/
410 typedef struct xbt_os_cond_ {
411   /* KEEP IT IN SYNC WITH xbt_thread.c */
412   pthread_cond_t c;
413 } s_xbt_os_cond_t;
414
415 xbt_os_cond_t xbt_os_cond_init(void)
416 {
417   xbt_os_cond_t res = xbt_new(s_xbt_os_cond_t, 1);
418   int errcode = pthread_cond_init(&(res->c), NULL);
419   xbt_assert(errcode==0, "pthread_cond_init() failed: %s", strerror(errcode));
420   return res;
421 }
422
423 void xbt_os_cond_wait(xbt_os_cond_t cond, xbt_os_mutex_t mutex)
424 {
425   int errcode = pthread_cond_wait(&(cond->c), &(mutex->m));
426   xbt_assert(errcode==0, "pthread_cond_wait(%p,%p) failed: %s", cond, mutex, strerror(errcode));
427 }
428
429
430 void xbt_os_cond_timedwait(xbt_os_cond_t cond, xbt_os_mutex_t mutex, double delay)
431 {
432   int errcode;
433   struct timespec ts_end;
434   double end = delay + xbt_os_time();
435
436   if (delay < 0) {
437     xbt_os_cond_wait(cond, mutex);
438   } else {
439     ts_end.tv_sec = (time_t) floor(end);
440     ts_end.tv_nsec = (long) ((end - ts_end.tv_sec) * 1000000000);
441     XBT_DEBUG("pthread_cond_timedwait(%p,%p,%p)", &(cond->c), &(mutex->m), &ts_end);
442     switch ((errcode = pthread_cond_timedwait(&(cond->c), &(mutex->m), &ts_end))) {
443     case 0:
444       return;
445     case ETIMEDOUT:
446       THROWF(timeout_error, errcode,
447              "condition %p (mutex %p) wasn't signaled before timeout (%f)",
448              cond, mutex, delay);
449     default:
450       THROWF(system_error, errcode, "pthread_cond_timedwait(%p,%p,%f) failed: %s", cond, mutex, delay, strerror(errcode));
451     }
452   }
453 }
454
455 void xbt_os_cond_signal(xbt_os_cond_t cond)
456 {
457   int errcode = pthread_cond_signal(&(cond->c));
458   xbt_assert(errcode==0, "pthread_cond_signal(%p) failed: %s", cond, strerror(errcode));
459 }
460
461 void xbt_os_cond_broadcast(xbt_os_cond_t cond)
462 {
463   int errcode = pthread_cond_broadcast(&(cond->c));
464   xbt_assert(errcode==0, "pthread_cond_broadcast(%p) failed: %s", cond, strerror(errcode));
465 }
466
467 void xbt_os_cond_destroy(xbt_os_cond_t cond)
468 {
469   if (!cond)
470     return;
471
472   int errcode = pthread_cond_destroy(&(cond->c));
473   xbt_assert(errcode==0, "pthread_cond_destroy(%p) failed: %s", cond, strerror(errcode));
474   free(cond);
475 }
476
477 void *xbt_os_thread_getparam(void)
478 {
479   xbt_os_thread_t t = xbt_os_thread_self();
480   return t ? t->param : NULL;
481 }
482
483 typedef struct xbt_os_sem_ {
484 #ifndef HAVE_SEM_INIT
485   char *name;
486 #endif
487   sem_t s;
488   sem_t *ps;
489 } s_xbt_os_sem_t;
490
491 #ifndef SEM_FAILED
492 #define SEM_FAILED (-1)
493 #endif
494
495 xbt_os_sem_t xbt_os_sem_init(unsigned int value)
496 {
497   xbt_os_sem_t res = xbt_new(s_xbt_os_sem_t, 1);
498
499   /* On some systems (MAC OS X), only the stub of sem_init is to be found.
500    * Any attempt to use it leads to ENOSYS (function not implemented).
501    * If such a prehistoric system is detected, do the job with sem_open instead
502    */
503 #ifdef HAVE_SEM_INIT
504   if (sem_init(&(res->s), 0, value) != 0)
505     THROWF(system_error, errno, "sem_init() failed: %s", strerror(errno));
506   res->ps = &(res->s);
507
508 #else                           /* damn, no sem_init(). Reimplement it */
509
510   xbt_os_mutex_acquire(next_sem_ID_lock);
511   res->name = bprintf("/%d", ++next_sem_ID);
512   xbt_os_mutex_release(next_sem_ID_lock);
513
514   res->ps = sem_open(res->name, O_CREAT, 0644, value);
515   if ((res->ps == (sem_t *) SEM_FAILED) && (errno == ENAMETOOLONG)) {
516     /* Old darwins only allow 13 chars. Did you create *that* amount of semaphores? */
517     res->name[13] = '\0';
518     res->ps = sem_open(res->name, O_CREAT, 0644, value);
519   }
520   if (res->ps == (sem_t *) SEM_FAILED)
521     THROWF(system_error, errno, "sem_open() failed: %s", strerror(errno));
522
523   /* Remove the name from the semaphore namespace: we never join on it */
524   if (sem_unlink(res->name) < 0)
525     THROWF(system_error, errno, "sem_unlink() failed: %s",
526            strerror(errno));
527
528 #endif
529
530   return res;
531 }
532
533 void xbt_os_sem_acquire(xbt_os_sem_t sem)
534 {
535   if (sem_wait(sem->ps) < 0)
536     THROWF(system_error, errno, "sem_wait() failed: %s", strerror(errno));
537 }
538
539 void xbt_os_sem_timedacquire(xbt_os_sem_t sem, double delay)
540 {
541   int errcode;
542
543   if (delay < 0) {
544     xbt_os_sem_acquire(sem);
545   } else if (delay == 0) {
546     errcode = sem_trywait(sem->ps);
547
548     switch (errcode) {
549     case 0:
550       return;
551     case ETIMEDOUT:
552       THROWF(timeout_error, 0, "semaphore %p not ready", sem);
553     default:
554       THROWF(system_error, errcode,
555              "xbt_os_sem_timedacquire(%p) failed: %s", sem,
556              strerror(errcode));
557     }
558
559   } else {
560 #ifdef HAVE_SEM_WAIT
561     struct timespec ts_end;
562     double end = delay + xbt_os_time();
563
564     ts_end.tv_sec = (time_t) floor(end);
565     ts_end.tv_nsec = (long) ((end - ts_end.tv_sec) * 1000000000);
566     XBT_DEBUG("sem_timedwait(%p,%p)", sem->ps, &ts_end);
567     errcode = sem_timedwait(sem->s, &ts_end);
568
569 #else                           /* Okay, reimplement this function then */
570     double start = xbt_os_time();
571     do {
572       errcode = sem_trywait(sem->ps);
573       if (errcode == EBUSY)
574         xbt_os_thread_yield();
575     } while (errcode == EBUSY && xbt_os_time() - start < delay);
576
577     if (errcode == EBUSY)
578       errcode = ETIMEDOUT;
579 #endif
580
581     switch (errcode) {
582     case 0:
583       return;
584
585     case ETIMEDOUT:
586       THROWF(timeout_error, delay,
587              "semaphore %p wasn't signaled before timeout (%f)", sem,
588              delay);
589
590     default:
591       THROWF(system_error, errcode, "sem_timedwait(%p,%f) failed: %s", sem,
592              delay, strerror(errcode));
593     }
594   }
595 }
596
597 void xbt_os_sem_release(xbt_os_sem_t sem)
598 {
599   if (sem_post(sem->ps) < 0)
600     THROWF(system_error, errno, "sem_post() failed: %s", strerror(errno));
601 }
602
603 void xbt_os_sem_destroy(xbt_os_sem_t sem)
604 {
605 #ifdef HAVE_SEM_INIT
606   if (sem_destroy(sem->ps) < 0)
607     THROWF(system_error, errno, "sem_destroy() failed: %s",
608            strerror(errno));
609 #else
610   if (sem_close(sem->ps) < 0)
611     THROWF(system_error, errno, "sem_close() failed: %s", strerror(errno));
612   xbt_free(sem->name);
613
614 #endif
615   xbt_free(sem);
616 }
617
618 void xbt_os_sem_get_value(xbt_os_sem_t sem, int *svalue)
619 {
620   if (sem_getvalue(&(sem->s), svalue) < 0)
621     THROWF(system_error, errno, "sem_getvalue() failed: %s",
622            strerror(errno));
623 }
624
625
626 /** @brief Returns the amount of cores on the current host */
627 int xbt_os_get_numcores(void) {
628 #ifdef WIN32
629     SYSTEM_INFO sysinfo;
630     GetSystemInfo(&sysinfo);
631     return sysinfo.dwNumberOfProcessors;
632 #elif defined(__APPLE__) && defined(__MACH__)
633     int nm[2];
634     size_t len = 4;
635     uint32_t count;
636
637     nm[0] = CTL_HW; nm[1] = HW_AVAILCPU;
638     sysctl(nm, 2, &count, &len, NULL, 0);
639
640     if(count < 1) {
641         nm[1] = HW_NCPU;
642         sysctl(nm, 2, &count, &len, NULL, 0);
643         if(count < 1) { count = 1; }
644     }
645     return count;
646 #else
647     return sysconf(_SC_NPROCESSORS_ONLN);
648 #endif
649 }
650
651
652 /***** reentrant mutexes *****/
653 typedef struct xbt_os_rmutex_ {
654   xbt_os_mutex_t mutex;
655   xbt_os_thread_t owner;
656   int count;
657 } s_xbt_os_rmutex_t;
658
659 void xbt_os_thread_set_extra_data(void *data)
660 {
661   xbt_os_thread_self()->extra_data = data;
662 }
663
664 void *xbt_os_thread_get_extra_data(void)
665 {
666   xbt_os_thread_t thread = xbt_os_thread_self();
667   if (thread)
668     return xbt_os_thread_self()->extra_data;
669   else
670     return NULL;
671 }
672
673 xbt_os_rmutex_t xbt_os_rmutex_init(void)
674 {
675   xbt_os_rmutex_t rmutex = xbt_new0(struct xbt_os_rmutex_, 1);
676   rmutex->mutex = xbt_os_mutex_init();
677   rmutex->owner = NULL;
678   rmutex->count = 0;
679   return rmutex;
680 }
681
682 void xbt_os_rmutex_acquire(xbt_os_rmutex_t rmutex)
683 {
684   xbt_os_thread_t self = xbt_os_thread_self();
685
686   if (self == NULL) {
687     /* the thread module is not initialized yet */
688     rmutex->owner = NULL;
689     return;
690   }
691
692   if (self != rmutex->owner) {
693     xbt_os_mutex_acquire(rmutex->mutex);
694     rmutex->owner = self;
695     rmutex->count = 1;
696   } else {
697     rmutex->count++;
698  }
699 }
700
701 void xbt_os_rmutex_release(xbt_os_rmutex_t rmutex)
702 {
703   if (rmutex->owner == NULL) {
704     /* the thread module was not initialized */
705     return;
706   }
707
708   xbt_assert(rmutex->owner == xbt_os_thread_self());
709
710   if (--rmutex->count == 0) {
711     rmutex->owner = NULL;
712     xbt_os_mutex_release(rmutex->mutex);
713   }
714 }
715
716 void xbt_os_rmutex_destroy(xbt_os_rmutex_t rmutex)
717 {
718   xbt_os_mutex_destroy(rmutex->mutex);
719   xbt_free(rmutex);
720 }