Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
bf285864412c644ac195a3356472e26c27c4533f
[simgrid.git] / src / xbt / xbt_sg_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 simulation implementation, using simix.                      */
7
8 /* Copyright 2006,2007 Malek Cherier, Martin Quinson          
9  * All right reserved.                                                      */
10
11 /* This program is free software; you can redistribute it and/or modify it
12  * under the terms of the license (GNU LGPL) which comes with this package. */
13
14 #include "xbt/ex.h"
15
16 #include "xbt/synchro.h"     /* This module */
17
18 #include "simix/simix.h"     /* used implementation */
19 #include "simix/datatypes.h"
20
21 /* the implementation would be cleaner (and faster) with ELF symbol aliasing */
22
23 typedef struct s_xbt_thread_ {
24    smx_process_t s_process;
25    void_f_pvoid_t *code;
26    void *userparam;
27 } s_xbt_thread_t;
28
29 static int xbt_thread_create_wrapper(int argc, char *argv[]) {
30    xbt_thread_t t = (xbt_thread_t)SIMIX_process_get_data(SIMIX_process_self());
31    (*t->code)(t->userparam);
32    return 0;
33 }
34
35 xbt_thread_t xbt_thread_create(void_f_pvoid_t* code, void* param)  {
36    xbt_thread_t res = xbt_new0(s_xbt_thread_t,1);
37    res->userparam = param;
38    res->code = code;
39    res->s_process = SIMIX_process_create(NULL, 
40                                          xbt_thread_create_wrapper, res,
41                                          SIMIX_host_get_name(SIMIX_host_self()),
42                                          0, NULL);
43    return res;
44 }
45
46 void 
47 xbt_thread_join(xbt_thread_t thread) {
48    THROW_UNIMPLEMENTED; /* FIXME */
49 }                      
50
51 void 
52 xbt_thread_cancel(xbt_thread_t thread) {
53    SIMIX_process_kill(thread->s_process);
54    free(thread);
55 }                      
56
57 void xbt_thread_exit() {
58    xbt_thread_t me=SIMIX_process_get_data(SIMIX_process_self());
59    SIMIX_process_kill(me->s_process);
60    free(me);
61 }
62
63 xbt_thread_t xbt_thread_self(void) {
64    return SIMIX_process_get_data(SIMIX_process_self());
65 }
66
67 void xbt_thread_yield(void) {
68    THROW_UNIMPLEMENTED; /* FIXME */   
69 }
70
71 /****** mutex related functions ******/
72 struct s_xbt_mutex_ {
73    
74    /* KEEP IT IN SYNC WITH src/simix/private.h::struct s_smx_mutex */
75    xbt_swag_t sleeping;                 /* list of sleeping process */
76    int using;
77    /* KEEP IT IN SYNC WITH src/simix/private.h::struct s_smx_mutex */
78    
79 };
80
81 xbt_mutex_t xbt_mutex_init(void) {
82    return (xbt_mutex_t)SIMIX_mutex_init();
83 }
84
85 void xbt_mutex_lock(xbt_mutex_t mutex) {
86    SIMIX_mutex_lock( (smx_mutex_t)mutex) ;
87 }
88
89 void xbt_mutex_unlock(xbt_mutex_t mutex) {
90    SIMIX_mutex_unlock( (smx_mutex_t)mutex );
91 }
92
93 void xbt_mutex_destroy(xbt_mutex_t mutex) {
94    SIMIX_mutex_destroy( (smx_mutex_t)mutex );
95 }
96
97 /***** condition related functions *****/
98 struct s_xbt_cond_ {
99    
100    /* KEEP IT IN SYNC WITH src/simix/private.h::struct s_smx_cond */
101    xbt_swag_t sleeping;                         /* list of sleeping process */
102    smx_mutex_t  mutex;
103    xbt_fifo_t actions;                  /* list of actions */
104    /* KEEP IT IN SYNC WITH src/simix/private.h::struct s_smx_cond */
105
106 };
107
108 xbt_cond_t xbt_cond_init(void) {
109    return (xbt_cond_t)SIMIX_cond_init();
110 }
111
112 void xbt_cond_wait(xbt_cond_t cond, xbt_mutex_t mutex) {
113    SIMIX_cond_wait( (smx_cond_t)cond , (smx_mutex_t)mutex );
114 }
115
116 void xbt_cond_timedwait(xbt_cond_t cond, xbt_mutex_t mutex, double delay) {
117    SIMIX_cond_wait_timeout( (smx_cond_t)cond , (smx_mutex_t)mutex, delay );
118 }
119
120 void xbt_cond_signal(xbt_cond_t cond) {
121    SIMIX_cond_signal( (smx_cond_t)cond );
122 }
123          
124 void xbt_cond_broadcast(xbt_cond_t cond){
125    SIMIX_cond_broadcast( (smx_cond_t)cond );
126 }
127 void xbt_cond_destroy(xbt_cond_t cond){
128    SIMIX_cond_destroy( (smx_cond_t)cond );
129 }