Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Do throw an exception only when the requested factory was not found, not all the...
[simgrid.git] / src / xbt / xbt_rl_synchro.c
1 /* $Id$ */
2
3 /* xbt_synchro -- Synchronization virtualized depending on whether we are   */
4 /*                in simulation or real life (act on simulated processes)   */
5
6 /* This is the real life implementation, using xbt_os_thread to be portable */
7 /* to windows and linux.                                                    */
8
9 /* Copyright 2006,2007 Malek Cherier, Martin Quinson          
10  * All right reserved.                                                      */
11
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. */
14
15 #include "xbt/sysdep.h"
16 #include "xbt/ex.h"
17 #include "portable.h"
18
19 #include "xbt/synchro.h"       /* This module */
20 #include "xbt/xbt_os_thread.h" /* The implementation we use */
21
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)");
24
25 typedef struct s_xbt_thread_ {   
26    xbt_os_thread_t os_thread;
27    void_f_pvoid_t code;
28    void *userparam;   
29 }s_xbt_thread_t;
30
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);
35    return NULL;
36 }
37
38
39 xbt_thread_t xbt_thread_create(const char*name,void_f_pvoid_t code, void* param) {
40
41    xbt_thread_t res = xbt_new0(s_xbt_thread_t,1);
42    res->userparam = param;
43    res->code = code;
44    DEBUG1("Create thread %p",res);
45    res->os_thread = xbt_os_thread_create(name,xbt_thread_create_wrapper,res);
46    return res;
47 }
48
49 const char* xbt_thread_name(xbt_thread_t t) {
50    return xbt_os_thread_name(t->os_thread);
51 }
52
53 const char* xbt_thread_self_name(void) {
54    return xbt_os_thread_self_name();
55 }
56
57 void 
58 xbt_thread_join(xbt_thread_t thread) {
59    DEBUG1("Join thread %p",thread);
60    xbt_os_thread_join( thread->os_thread, NULL );
61 }                      
62
63 void xbt_thread_exit() {
64    DEBUG0("Thread exits");
65    xbt_os_thread_exit(NULL);
66 }
67
68 xbt_thread_t xbt_thread_self(void) {
69    return (xbt_thread_t)xbt_os_thread_self();
70 }
71
72 void xbt_thread_yield(void) {
73    DEBUG0("Thread yields");
74    xbt_os_thread_yield();
75 }
76 void xbt_thread_cancel(xbt_thread_t t) {
77    DEBUG1("Cancel thread %p",t);
78    xbt_os_thread_cancel(t->os_thread);
79 }
80 /****** mutex related functions ******/
81 struct xbt_mutex_ {
82    /* KEEP IT IN SYNC WITH OS IMPLEMENTATION (both win and lin) */
83 #ifdef HAVE_PTHREAD_H   
84    pthread_mutex_t m;
85 #elif defined(WIN32)
86    CRITICAL_SECTION lock;   
87 #endif   
88 };
89
90 xbt_mutex_t xbt_mutex_init(void) {
91    xbt_mutex_t res = (xbt_mutex_t)xbt_os_mutex_init();
92    DEBUG1("Create mutex %p", res);
93    return res;
94 }
95
96 void xbt_mutex_acquire(xbt_mutex_t mutex) {
97    DEBUG1("Acquire mutex %p", mutex);
98    xbt_os_mutex_acquire( (xbt_os_mutex_t)mutex );
99 }
100
101 void xbt_mutex_timedacquire(xbt_mutex_t mutex, double delay)
102 {
103         DEBUG2("Acquire mutex %p with delay %lf", mutex,delay);
104    xbt_os_mutex_timedacquire( (xbt_os_mutex_t)mutex,delay );
105 }
106
107 void xbt_mutex_release(xbt_mutex_t mutex) {
108    DEBUG1("Unlock mutex %p", mutex);
109    xbt_os_mutex_release( (xbt_os_mutex_t)mutex );
110 }
111
112 void xbt_mutex_destroy(xbt_mutex_t mutex) {
113    DEBUG1("Destroy mutex %p", mutex);
114    xbt_os_mutex_destroy( (xbt_os_mutex_t)mutex );
115 }
116
117 #ifdef WIN32
118  enum { /* KEEP IT IN SYNC WITH OS IMPLEM */
119     SIGNAL = 0,
120     BROADCAST = 1,
121     MAX_EVENTS = 2
122  };
123 #endif
124
125 /***** condition related functions *****/
126 typedef struct xbt_cond_ {
127    /* KEEP IT IN SYNC WITH OS IMPLEMENTATION (both win and lin) */
128 #ifdef HAVE_PTHREAD_H   
129    pthread_cond_t c;
130 #elif defined(WIN32)   
131    HANDLE events[MAX_EVENTS];
132    
133    unsigned int waiters_count;           /* the number of waiters                        */
134    CRITICAL_SECTION waiters_count_lock;  /* protect access to waiters_count  */
135 #endif
136 } s_xbt_cond_t;
137
138 xbt_cond_t xbt_cond_init(void) {
139    xbt_cond_t res = (xbt_cond_t) xbt_os_cond_init();
140    DEBUG1("Create cond %p", res);
141    return res;
142 }
143
144 void xbt_cond_wait(xbt_cond_t cond, xbt_mutex_t mutex) {
145    DEBUG2("Wait cond %p, mutex %p", cond, mutex);
146    xbt_os_cond_wait( (xbt_os_cond_t)cond, (xbt_os_mutex_t)mutex );
147 }
148
149 void xbt_cond_timedwait(xbt_cond_t cond, xbt_mutex_t mutex, double delay) {
150    DEBUG3("Wait cond %p, mutex %p for %f sec", cond, mutex,delay);
151    xbt_os_cond_timedwait( (xbt_os_cond_t)cond, (xbt_os_mutex_t)mutex, delay );
152    DEBUG3("Done waiting cond %p, mutex %p for %f sec", cond, mutex, delay);
153 }
154
155 void xbt_cond_signal(xbt_cond_t cond) {
156    DEBUG1("Signal cond %p", cond);
157    xbt_os_cond_signal( (xbt_os_cond_t)cond );
158 }
159          
160 void xbt_cond_broadcast(xbt_cond_t cond){
161    DEBUG1("Broadcast cond %p", cond);
162    xbt_os_cond_broadcast( (xbt_os_cond_t)cond );
163 }
164 void xbt_cond_destroy(xbt_cond_t cond){
165    DEBUG1("Destroy cond %p", cond);
166    xbt_os_cond_destroy( (xbt_os_cond_t)cond );
167 }