Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
removed unused variables
[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
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    (*t->code)(t->userparam);
34    return NULL;
35 }
36
37
38 xbt_thread_t xbt_thread_create(void_f_pvoid_t* code, void* param) {
39
40    xbt_thread_t res = xbt_new0(s_xbt_thread_t,1);
41    res->userparam = param;
42    res->code = code;
43    res->os_thread = xbt_os_thread_create(xbt_thread_create_wrapper,res);
44    return res;
45 }
46
47 void 
48 xbt_thread_join(xbt_thread_t thread) {
49    xbt_os_thread_join( thread->os_thread, NULL );
50 }                      
51
52 void xbt_thread_exit() {
53    xbt_os_thread_exit(NULL);
54 }
55
56 xbt_thread_t xbt_thread_self(void) {
57    return (xbt_thread_t)xbt_os_thread_getparam();
58 }
59
60 void xbt_thread_yield(void) {
61    xbt_os_thread_yield();
62 }
63 void xbt_thread_cancel(xbt_thread_t t) {
64    xbt_os_thread_cancel(t->os_thread);
65 }
66 /****** mutex related functions ******/
67 struct xbt_mutex_ {
68    /* KEEP IT IN SYNC WITH OS IMPLEMENTATION (both win and lin) */
69 #ifdef HAVE_PTHREAD_H   
70    pthread_mutex_t m;
71 #elif defined(WIN32)
72    CRITICAL_SECTION lock;   
73 #endif   
74 };
75
76 xbt_mutex_t xbt_mutex_init(void) {
77    return (xbt_mutex_t)xbt_os_mutex_init();
78 }
79
80 void xbt_mutex_lock(xbt_mutex_t mutex) {
81    xbt_os_mutex_lock( (xbt_os_mutex_t)mutex );
82 }
83
84 void xbt_mutex_unlock(xbt_mutex_t mutex) {
85    xbt_os_mutex_unlock( (xbt_os_mutex_t)mutex );
86 }
87
88 void xbt_mutex_destroy(xbt_mutex_t mutex) {
89    xbt_os_mutex_destroy( (xbt_os_mutex_t)mutex );
90 }
91
92 #ifdef WIN32
93  enum { /* KEEP IT IN SYNC WITH OS IMPLEM */
94     SIGNAL = 0,
95     BROADCAST = 1,
96     MAX_EVENTS = 2
97  };
98 #endif
99
100 /***** condition related functions *****/
101 typedef struct xbt_cond_ {
102    /* KEEP IT IN SYNC WITH OS IMPLEMENTATION (both win and lin) */
103 #ifdef HAVE_PTHREAD_H   
104    pthread_cond_t c;
105 #elif defined(WIN32)   
106    HANDLE events[MAX_EVENTS];
107    
108    unsigned int waiters_count;           /* the number of waiters                        */
109    CRITICAL_SECTION waiters_count_lock;  /* protect access to waiters_count  */
110 #endif
111 } s_xbt_cond_t;
112
113 xbt_cond_t xbt_cond_init(void) {
114    return (xbt_cond_t) xbt_os_cond_init();
115 }
116
117 void xbt_cond_wait(xbt_cond_t cond, xbt_mutex_t mutex) {
118    xbt_os_cond_wait( (xbt_os_cond_t)cond, (xbt_os_mutex_t)mutex );
119 }
120
121 void xbt_cond_timedwait(xbt_cond_t cond, xbt_mutex_t mutex, double delay) {
122    xbt_os_cond_timedwait( (xbt_os_cond_t)cond, (xbt_os_mutex_t)mutex, delay );
123 }
124
125 void xbt_cond_signal(xbt_cond_t cond) {
126    xbt_os_cond_signal( (xbt_os_cond_t)cond );
127 }
128          
129 void xbt_cond_broadcast(xbt_cond_t cond){
130    xbt_os_cond_broadcast( (xbt_os_cond_t)cond );
131 }
132 void xbt_cond_destroy(xbt_cond_t cond){
133    xbt_os_cond_destroy( (xbt_os_cond_t)cond );
134 }