3 /* xbt_synchro -- Synchronization virtualized depending on whether we are */
4 /* in simulation or real life (act on simulated processes) */
6 /* This is the real life implementation, using xbt_os_thread to be portable */
7 /* to windows and linux. */
9 /* Copyright 2006,2007 Malek Cherier, Martin Quinson
10 * All right reserved. */
12 /* This program is free software; you can redistribute it and/or modify it
13 * under the terms of the license (GNU LGPL) which comes with this package. */
15 #include "xbt/sysdep.h"
19 #include "xbt/synchro.h" /* This module */
20 #include "xbt/xbt_os_thread.h" /* The implementation we use */
22 /* the implementation would be cleaner (and faster) with ELF symbol aliasing */
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_sync_rl,xbt,"Synchronization mechanism (RL)");
25 typedef struct s_xbt_thread_ {
26 xbt_os_thread_t os_thread;
31 static void *xbt_thread_create_wrapper(void *p) {
32 xbt_thread_t t = (xbt_thread_t)p;
33 DEBUG1("I'm thread %p",p);
34 (*t->code)(t->userparam);
39 xbt_thread_t xbt_thread_create(const char*name,void_f_pvoid_t* code, void* param) {
41 xbt_thread_t res = xbt_new0(s_xbt_thread_t,1);
42 res->userparam = param;
44 DEBUG1("Create thread %p",res);
45 res->os_thread = xbt_os_thread_create(name,xbt_thread_create_wrapper,res);
50 xbt_thread_join(xbt_thread_t thread) {
51 DEBUG1("Join thread %p",thread);
52 xbt_os_thread_join( thread->os_thread, NULL );
55 void xbt_thread_exit() {
56 DEBUG0("Thread exits");
57 xbt_os_thread_exit(NULL);
60 xbt_thread_t xbt_thread_self(void) {
61 return (xbt_thread_t)xbt_os_thread_self();
64 void xbt_thread_yield(void) {
65 DEBUG0("Thread yields");
66 xbt_os_thread_yield();
68 void xbt_thread_cancel(xbt_thread_t t) {
69 DEBUG1("Cancel thread %p",t);
70 xbt_os_thread_cancel(t->os_thread);
72 /****** mutex related functions ******/
74 /* KEEP IT IN SYNC WITH OS IMPLEMENTATION (both win and lin) */
78 CRITICAL_SECTION lock;
82 xbt_mutex_t xbt_mutex_init(void) {
83 xbt_mutex_t res = (xbt_mutex_t)xbt_os_mutex_init();
84 DEBUG1("Create mutex %p", res);
85 xbt_backtrace_display_current();
89 void xbt_mutex_lock(xbt_mutex_t mutex) {
90 DEBUG1("Lock mutex %p", mutex);
91 xbt_os_mutex_lock( (xbt_os_mutex_t)mutex );
94 void xbt_mutex_unlock(xbt_mutex_t mutex) {
95 DEBUG1("Unlock mutex %p", mutex);
96 xbt_os_mutex_unlock( (xbt_os_mutex_t)mutex );
97 xbt_backtrace_display_current();
100 void xbt_mutex_destroy(xbt_mutex_t mutex) {
101 DEBUG1("Destroy mutex %p", mutex);
102 xbt_os_mutex_destroy( (xbt_os_mutex_t)mutex );
106 enum { /* KEEP IT IN SYNC WITH OS IMPLEM */
113 /***** condition related functions *****/
114 typedef struct xbt_cond_ {
115 /* KEEP IT IN SYNC WITH OS IMPLEMENTATION (both win and lin) */
116 #ifdef HAVE_PTHREAD_H
119 HANDLE events[MAX_EVENTS];
121 unsigned int waiters_count; /* the number of waiters */
122 CRITICAL_SECTION waiters_count_lock; /* protect access to waiters_count */
126 xbt_cond_t xbt_cond_init(void) {
127 xbt_cond_t res = (xbt_cond_t) xbt_os_cond_init();
128 DEBUG1("Create cond %p", res);
129 return (xbt_cond_t) xbt_os_cond_init();
132 void xbt_cond_wait(xbt_cond_t cond, xbt_mutex_t mutex) {
133 DEBUG2("Wait cond %p, mutex %p", cond, mutex);
134 xbt_os_cond_wait( (xbt_os_cond_t)cond, (xbt_os_mutex_t)mutex );
137 void xbt_cond_timedwait(xbt_cond_t cond, xbt_mutex_t mutex, double delay) {
138 DEBUG3("Wait cond %p, mutex %p for %f sec", cond, mutex,delay);
139 xbt_backtrace_display_current();
140 xbt_os_cond_timedwait( (xbt_os_cond_t)cond, (xbt_os_mutex_t)mutex, delay );
141 DEBUG3("Done waiting cond %p, mutex %p for %f sec", cond, mutex, delay);
144 void xbt_cond_signal(xbt_cond_t cond) {
145 DEBUG1("Signal cond %p", cond);
146 xbt_os_cond_signal( (xbt_os_cond_t)cond );
149 void xbt_cond_broadcast(xbt_cond_t cond){
150 DEBUG1("Broadcast cond %p", cond);
151 xbt_os_cond_broadcast( (xbt_os_cond_t)cond );
153 void xbt_cond_destroy(xbt_cond_t cond){
154 DEBUG1("Destroy cond %p", cond);
155 xbt_os_cond_destroy( (xbt_os_cond_t)cond );