Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
As Arnaud said, one function to rule them all
[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          void *father_data;
28 } s_xbt_thread_t;
29
30 static int xbt_thread_create_wrapper(int argc, char *argv[]) {
31    xbt_thread_t t = (xbt_thread_t)SIMIX_process_get_data(SIMIX_process_self());
32          SIMIX_process_set_data(SIMIX_process_self(),t->father_data);
33    (*t->code)(t->userparam);
34    return 0;
35 }
36
37 xbt_thread_t xbt_thread_create(void_f_pvoid_t* code, void* param)  {
38    xbt_thread_t res = xbt_new0(s_xbt_thread_t,1);
39    res->userparam = param;
40    res->code = code;
41    res->father_data = SIMIX_process_get_data(SIMIX_process_self());
42    char*name = bprintf("%s#%p",SIMIX_process_get_name(SIMIX_process_self()), param);
43    res->s_process = SIMIX_process_create(name, 
44                                          xbt_thread_create_wrapper, res,
45                                          SIMIX_host_get_name(SIMIX_host_self()),
46                                          0, NULL);
47    free(name);
48    return res;
49 }
50
51 void 
52 xbt_thread_join(xbt_thread_t thread) {
53    THROW_UNIMPLEMENTED; /* FIXME */
54 }                      
55
56 void 
57 xbt_thread_cancel(xbt_thread_t thread) {
58    SIMIX_process_kill(thread->s_process);
59    free(thread);
60 }                      
61
62 void xbt_thread_exit() {
63    xbt_thread_t me=SIMIX_process_get_data(SIMIX_process_self());
64    SIMIX_process_kill(me->s_process);
65    free(me);
66 }
67
68 xbt_thread_t xbt_thread_self(void) {
69    return SIMIX_process_get_data(SIMIX_process_self());
70 }
71
72 void xbt_thread_yield(void) {
73    THROW_UNIMPLEMENTED; /* FIXME */   
74 }
75
76 /****** mutex related functions ******/
77 struct s_xbt_mutex_ {
78    
79    /* KEEP IT IN SYNC WITH src/simix/private.h::struct s_smx_mutex */
80    xbt_swag_t sleeping;                 /* list of sleeping process */
81    int using;
82    /* KEEP IT IN SYNC WITH src/simix/private.h::struct s_smx_mutex */
83    
84 };
85
86 xbt_mutex_t xbt_mutex_init(void) {
87    return (xbt_mutex_t)SIMIX_mutex_init();
88 }
89
90 void xbt_mutex_lock(xbt_mutex_t mutex) {
91    SIMIX_mutex_lock( (smx_mutex_t)mutex) ;
92 }
93
94 void xbt_mutex_unlock(xbt_mutex_t mutex) {
95    SIMIX_mutex_unlock( (smx_mutex_t)mutex );
96 }
97
98 void xbt_mutex_destroy(xbt_mutex_t mutex) {
99    SIMIX_mutex_destroy( (smx_mutex_t)mutex );
100 }
101
102 /***** condition related functions *****/
103 struct s_xbt_cond_ {
104    
105    /* KEEP IT IN SYNC WITH src/simix/private.h::struct s_smx_cond */
106    xbt_swag_t sleeping;                         /* list of sleeping process */
107    smx_mutex_t  mutex;
108    xbt_fifo_t actions;                  /* list of actions */
109    /* KEEP IT IN SYNC WITH src/simix/private.h::struct s_smx_cond */
110
111 };
112
113 xbt_cond_t xbt_cond_init(void) {
114    return (xbt_cond_t)SIMIX_cond_init();
115 }
116
117 void xbt_cond_wait(xbt_cond_t cond, xbt_mutex_t mutex) {
118    SIMIX_cond_wait( (smx_cond_t)cond , (smx_mutex_t)mutex );
119 }
120
121 void xbt_cond_timedwait(xbt_cond_t cond, xbt_mutex_t mutex, double delay) {
122    SIMIX_cond_wait_timeout( (smx_cond_t)cond , (smx_mutex_t)mutex, delay );
123 }
124
125 void xbt_cond_signal(xbt_cond_t cond) {
126    SIMIX_cond_signal( (smx_cond_t)cond );
127 }
128          
129 void xbt_cond_broadcast(xbt_cond_t cond){
130    SIMIX_cond_broadcast( (smx_cond_t)cond );
131 }
132 void xbt_cond_destroy(xbt_cond_t cond){
133    SIMIX_cond_destroy( (smx_cond_t)cond );
134 }