Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
b6d418b0c6014729bcfa0a3eaa05d0331f22b073
[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
34         xbt_assert0((mutex != NULL), "Invalid parameters");
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                 /* verify if the process was suspended */
43                 while (self->simdata->suspended) {
44                         xbt_context_yield();
45                 }
46
47                 self->simdata->mutex = NULL;
48                 mutex->using = 1;
49         }
50         else {
51                 /* mutex free */
52                 mutex->using = 1;
53         }
54         return;
55 }
56
57 /* return 1 if the process got the mutex, else 0. */
58 int SIMIX_mutex_trylock(smx_mutex_t mutex)
59 {
60         xbt_assert0((mutex != NULL), "Invalid parameters");
61         
62         if (mutex->using) 
63                 return 0;
64         else {
65                 mutex->using = 1;
66                 return 1;
67         }
68 }
69
70 void SIMIX_mutex_unlock(smx_mutex_t mutex)
71 {
72         smx_process_t p;        /*process to wake up */
73         
74         xbt_assert0((mutex != NULL), "Invalid parameters");
75         
76         if (xbt_swag_size(mutex->sleeping) > 0) {
77                 p = xbt_swag_extract(mutex->sleeping);
78                 mutex->using = 0;
79                 xbt_swag_insert(p, simix_global->process_to_run);
80         }
81         else {
82                 /* nobody to wake up */
83                 mutex->using = 0;
84         }
85         return;
86 }
87
88 void SIMIX_mutex_destroy(smx_mutex_t mutex)
89 {
90         if ( mutex == NULL )
91                 return ;
92         else {
93                 xbt_swag_free(mutex->sleeping);
94                 xbt_free(mutex);
95                 return ;
96         }
97 }
98
99 /******************************** Conditional *********************************/
100 smx_cond_t SIMIX_cond_init()
101 {
102         smx_cond_t cond = xbt_new0(s_smx_cond_t,1);
103         s_smx_process_t p;
104         
105         cond->sleeping = xbt_swag_new(xbt_swag_offset(p,synchro_hookup));
106         cond->actions = xbt_fifo_new();
107         cond->mutex = NULL;
108         return cond;
109 }
110
111 void SIMIX_cond_signal(smx_cond_t cond)
112 {
113         xbt_assert0((cond != NULL), "Invalid parameters");
114         smx_process_t proc = NULL;
115
116         if (xbt_swag_size(cond->sleeping) >= 1) {
117                 proc = xbt_swag_extract(cond->sleeping);
118                 xbt_swag_insert(proc, simix_global->process_to_run);
119         }
120
121         return;
122 }
123
124 void SIMIX_cond_wait(smx_cond_t cond,smx_mutex_t mutex)
125 {
126         smx_process_t self = SIMIX_process_self();
127         xbt_assert0((mutex != NULL), "Invalid parameters");
128         
129         cond->mutex = mutex;
130
131         SIMIX_mutex_unlock(mutex);
132         __SIMIX_cond_wait(cond);
133         /* get the mutex again */
134         self->simdata->mutex = cond->mutex;
135         SIMIX_mutex_lock(cond->mutex);
136
137         return;
138 }
139
140 void __SIMIX_cond_wait(smx_cond_t cond)
141 {
142         smx_process_t self = SIMIX_process_self();
143         xbt_assert0((cond != NULL), "Invalid parameters");
144         
145         /* process status */    
146         self->simdata->cond = cond;
147
148         xbt_swag_insert(self, cond->sleeping);
149         
150         xbt_context_yield();
151         self->simdata->cond = NULL;
152         while (self->simdata->suspended) {
153                 xbt_context_yield();
154         }
155         return;
156
157 }
158
159 void SIMIX_cond_wait_timeout(smx_cond_t cond,smx_mutex_t mutex, double max_duration)
160 {
161         smx_process_t self = SIMIX_process_self();
162         xbt_assert0((mutex != NULL), "Invalid parameters");
163         smx_action_t act_sleep;
164
165         cond->mutex = mutex;
166
167         SIMIX_mutex_unlock(mutex);
168         if (max_duration >=0) {
169                 act_sleep = SIMIX_action_sleep(SIMIX_host_self(), max_duration);
170                 SIMIX_register_action_to_condition(act_sleep,cond);
171                 SIMIX_register_condition_to_action(act_sleep,cond);
172         }
173         __SIMIX_cond_wait(cond);
174
175         /* get the mutex again */
176         self->simdata->mutex = cond->mutex;
177         SIMIX_mutex_lock(cond->mutex);
178
179         return;
180 }
181
182 void SIMIX_cond_broadcast(smx_cond_t cond)
183 {
184         xbt_assert0((cond != NULL), "Invalid parameters");
185         smx_process_t proc = NULL;
186         smx_process_t proc_next = NULL;
187
188         xbt_swag_foreach_safe(proc,proc_next,cond->sleeping) {
189                 xbt_swag_remove(proc,cond->sleeping);
190                 xbt_swag_insert(proc, simix_global->process_to_run);
191         }
192
193         return;
194 }
195
196 void SIMIX_cond_destroy(smx_cond_t cond)
197 {
198         
199         if ( cond == NULL )
200                 return ;
201         else {
202                 xbt_assert0( xbt_swag_size(cond->sleeping) == 0 , "Cannot destroy conditional");
203
204                 xbt_swag_free(cond->sleeping);
205                 xbt_fifo_free(cond->actions);
206                 xbt_free(cond);
207                 return;
208         }
209 }
210
211 void SIMIX_register_condition_to_action(smx_action_t action, smx_cond_t cond)
212 {
213         xbt_assert0( (action != NULL) && (cond != NULL), "Invalid parameters");
214
215         xbt_fifo_push(action->cond_list,cond);
216 }
217
218