Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
1a6b52f48b51fe1d841ced2a767b777413bf142f
[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 struct xbt_thread_ {
26    /* KEEP IT IN SYNC WITH xbt_os_thread (both win and lin parts) */
27 #ifdef HAVE_PTHREAD_H
28    pthread_t t;
29    void *param;
30    pvoid_f_pvoid_t *start_routine;
31 #elif defined(WIN32)
32    HANDLE handle;                  /* the win thread handle        */
33    unsigned long id;               /* the win thread id            */
34    pvoid_f_pvoid_t *start_routine;
35    void* param;
36 #endif
37 };
38
39 xbt_thread_t xbt_thread_create(pvoid_f_pvoid_t start_routine,
40                                void* param)  {
41    return (xbt_thread_t)xbt_os_thread_create(start_routine,param);
42 }
43
44 void 
45 xbt_thread_join(xbt_thread_t thread,void ** thread_return) {
46    xbt_os_thread_join( (xbt_os_thread_t)thread, thread_return );
47 }                      
48
49 void xbt_thread_exit(int *retval) {
50    xbt_os_thread_exit( retval );
51 }
52
53 xbt_thread_t xbt_thread_self(void) {
54    return (xbt_thread_t)xbt_os_thread_self();
55 }
56
57 void xbt_thread_yield(void) {
58    xbt_os_thread_yield();
59 }
60 /****** mutex related functions ******/
61 struct xbt_mutex_ {
62    /* KEEP IT IN SYNC WITH OS IMPLEMENTATION (both win and lin) */
63 #ifdef HAVE_PTHREAD_H   
64    pthread_mutex_t m;
65 #elif defined(WIN32)
66    CRITICAL_SECTION lock;   
67 #endif   
68 };
69
70 xbt_mutex_t xbt_mutex_init(void) {
71    return (xbt_mutex_t)xbt_os_mutex_init();
72 }
73
74 void xbt_mutex_lock(xbt_mutex_t mutex) {
75    xbt_os_mutex_lock( (xbt_os_mutex_t)mutex );
76 }
77
78 void xbt_mutex_unlock(xbt_mutex_t mutex) {
79    xbt_os_mutex_unlock( (xbt_os_mutex_t)mutex );
80 }
81
82 void xbt_mutex_destroy(xbt_mutex_t mutex) {
83    xbt_os_mutex_destroy( (xbt_os_mutex_t)mutex );
84 }
85
86 /***** condition related functions *****/
87 typedef struct xbt_cond_ {
88    /* KEEP IT IN SYNC WITH OS IMPLEMENTATION (both win and lin) */
89 #ifdef HAVE_PTHREAD_H   
90    pthread_cond_t c;
91 #elif defined(WIN32)   
92    HANDLE events[MAX_EVENTS];
93    
94    unsigned int waiters_count;           /* the number of waiters                        */
95    CRITICAL_SECTION waiters_count_lock;  /* protect access to waiters_count  */
96 #endif
97 } s_xbt_cond_t;
98
99 xbt_cond_t xbt_cond_init(void) {
100    return (xbt_cond_t) xbt_os_cond_init();
101 }
102
103 void xbt_cond_wait(xbt_cond_t cond, xbt_mutex_t mutex) {
104    xbt_os_cond_wait( (xbt_os_cond_t)cond, (xbt_os_mutex_t)mutex );
105 }
106
107 void xbt_cond_signal(xbt_cond_t cond) {
108    xbt_os_cond_signal( (xbt_os_cond_t)cond );
109 }
110          
111 void xbt_cond_broadcast(xbt_cond_t cond){
112    xbt_os_cond_broadcast( (xbt_os_cond_t)cond );
113 }
114 void xbt_cond_destroy(xbt_cond_t cond){
115    xbt_os_cond_destroy( (xbt_os_cond_t)cond );
116 }