Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Functions added and changed. Keeps the cvs updated.
[simgrid.git] / src / simix / smx_synchro.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2007 Arnaud Legrand, Bruno Donnassolo.
4    All rights reserved.                                          */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include "private.h"
10 #include "xbt/log.h"
11
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_synchro, simix,
14                                 "Logging specific to SIMIX (synchronization)");
15
16
17 /****************************** Synchronization *******************************/
18
19 /*********************************** Mutex ************************************/
20 smx_mutex_t SIMIX_mutex_init()
21 {
22         smx_mutex_t m = xbt_new0(s_smx_mutex_t,1);
23         s_smx_process_t p; /* useful to initialize sleeping swag */
24         /* structures initialization */
25         m->using = 0;
26         m->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
27         return m;
28 }
29
30 void SIMIX_mutex_lock(smx_mutex_t mutex)
31 {
32         smx_process_t self = SIMIX_process_self();
33         xbt_assert0((mutex != NULL), "Invalid parameters");
34         
35
36         if (mutex->using) {
37                 /* somebody using the mutex, block */
38                 xbt_swag_insert(self, mutex->sleeping);
39                 self->simdata->mutex = mutex;
40                 /* wait for some process make the unlock and wake up me from mutex->sleeping */
41                 xbt_context_yield();
42                 self->simdata->mutex = NULL;
43
44                 /* verify if the process was suspended */
45                 while (self->simdata->suspended) {
46                         xbt_context_yield();
47                 }
48
49                 mutex->using = 1;
50         }
51         else {
52                 /* mutex free */
53                 mutex->using = 1;
54         }
55         return;
56 }
57
58 /* return 1 if the process got the mutex, else 0. */
59 int SIMIX_mutex_trylock(smx_mutex_t mutex)
60 {
61         xbt_assert0((mutex != NULL), "Invalid parameters");
62         
63         if (mutex->using) 
64                 return 0;
65         else {
66                 mutex->using = 1;
67                 return 1;
68         }
69 }
70
71 void SIMIX_mutex_unlock(smx_mutex_t mutex)
72 {
73         smx_process_t p;        /*process to wake up */
74         
75         xbt_assert0((mutex != NULL), "Invalid parameters");
76         
77         if (xbt_swag_size(mutex->sleeping) > 0) {
78                 p = xbt_swag_extract(mutex->sleeping);
79                 mutex->using = 0;
80                 xbt_swag_insert(p, simix_global->process_to_run);
81         }
82         else {
83                 /* nobody to wake up */
84                 mutex->using = 0;
85         }
86         return;
87 }
88
89 void SIMIX_mutex_destroy(smx_mutex_t mutex)
90 {
91         if ( mutex == NULL )
92                 return ;
93         else {
94                 xbt_swag_free(mutex->sleeping);
95                 xbt_free(mutex);
96                 return ;
97         }
98 }
99
100 /******************************** Conditional *********************************/
101 smx_cond_t SIMIX_cond_init()
102 {
103         smx_cond_t cond = xbt_new0(s_smx_cond_t,1);
104         s_smx_process_t p;
105         
106         cond->sleeping = xbt_swag_new(xbt_swag_offset(p,synchro_hookup));
107         cond->actions = xbt_fifo_new();
108         cond->mutex = NULL;
109         return cond;
110 }
111
112 void SIMIX_cond_signal(smx_cond_t cond)
113 {
114         xbt_assert0((cond != NULL), "Invalid parameters");
115         smx_process_t proc = NULL;
116
117         if (xbt_swag_size(cond->sleeping) >= 1) {
118                 proc = xbt_swag_extract(cond->sleeping);
119                 xbt_swag_insert(proc, simix_global->process_to_run);
120         }
121
122         return;
123 }
124
125 void SIMIX_cond_wait(smx_cond_t cond,smx_mutex_t mutex)
126 {
127         smx_action_t act_sleep;
128         xbt_assert0((mutex != NULL), "Invalid parameters");
129         
130         cond->mutex = mutex;
131
132         SIMIX_mutex_unlock(mutex);
133         /* create an action null only if there are no actions already on the condition, usefull if the host crashs */
134         if (xbt_fifo_size(cond->actions) ==0 ) {
135                 act_sleep = SIMIX_action_sleep(SIMIX_host_self(), -1);
136                 SIMIX_register_action_to_condition(act_sleep,cond);
137                 SIMIX_register_condition_to_action(act_sleep,cond);
138                 __SIMIX_cond_wait(cond);
139                 xbt_fifo_pop(act_sleep->cond_list);
140                 SIMIX_action_destroy(act_sleep);
141         }
142         else {
143                 __SIMIX_cond_wait(cond);
144         }
145         /* get the mutex again */
146         SIMIX_mutex_lock(cond->mutex);
147
148         return;
149 }
150
151 void __SIMIX_cond_wait(smx_cond_t cond)
152 {
153         smx_process_t self = SIMIX_process_self();
154         xbt_assert0((cond != NULL), "Invalid parameters");
155         
156         /* process status */    
157
158         self->simdata->cond = cond;
159         xbt_swag_insert(self, cond->sleeping);
160         xbt_context_yield();
161         self->simdata->cond = NULL;
162         while (self->simdata->suspended) {
163                 xbt_context_yield();
164         }
165         return;
166
167 }
168
169 void SIMIX_cond_wait_timeout(smx_cond_t cond,smx_mutex_t mutex, double max_duration)
170 {
171         xbt_assert0((mutex != NULL), "Invalid parameters");
172         smx_action_t act_sleep;
173
174         cond->mutex = mutex;
175
176         SIMIX_mutex_unlock(mutex);
177         if (max_duration >=0) {
178                 act_sleep = SIMIX_action_sleep(SIMIX_host_self(), max_duration);
179                 SIMIX_register_action_to_condition(act_sleep,cond);
180                 SIMIX_register_condition_to_action(act_sleep,cond);
181         }
182         __SIMIX_cond_wait(cond);
183
184         /* get the mutex again */
185         SIMIX_mutex_lock(cond->mutex);
186
187         return;
188 }
189
190 void SIMIX_cond_broadcast(smx_cond_t cond)
191 {
192         xbt_assert0((cond != NULL), "Invalid parameters");
193         smx_process_t proc = NULL;
194         smx_process_t proc_next = NULL;
195
196         xbt_swag_foreach_safe(proc,proc_next,cond->sleeping) {
197                 xbt_swag_remove(proc,cond->sleeping);
198                 xbt_swag_insert(proc, simix_global->process_to_run);
199         }
200
201         return;
202 }
203
204 void SIMIX_cond_destroy(smx_cond_t cond)
205 {
206         if ( cond == NULL )
207                 return ;
208         else {
209                 xbt_assert0( xbt_swag_size(cond->sleeping) == 0 , "Cannot destroy conditional");
210                 xbt_swag_free(cond->sleeping);
211                 xbt_fifo_free(cond->actions);
212                 xbt_free(cond);
213                 return;
214         }
215 }
216
217 void SIMIX_register_condition_to_action(smx_action_t action, smx_cond_t cond)
218 {
219         xbt_assert0( (action != NULL) && (cond != NULL), "Invalid parameters");
220
221         xbt_fifo_push(action->cond_list,cond);
222 }
223
224