Logo AND Algorithmique Numérique Distribuée

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