Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Implement a dynamic parallel execution mode.
[simgrid.git] / src / simix / smx_synchro.c
1 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "private.h"
8 #include "xbt/log.h"
9
10
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_synchro, simix,
12                                 "Logging specific to SIMIX (synchronization)");
13
14 static smx_action_t SIMIX_synchro_wait(smx_host_t smx_host, double timeout);
15 static void SIMIX_synchro_finish(smx_action_t action);
16 static void _SIMIX_cond_wait(smx_cond_t cond, smx_mutex_t mutex, double timeout,
17                              smx_process_t issuer, smx_req_t req);
18 static void _SIMIX_sem_wait(smx_sem_t sem, double timeout, smx_process_t issuer,
19                             smx_req_t req);
20 static void SIMIX_sem_block_onto(smx_sem_t sem);
21
22 /***************************** Synchro action *********************************/
23
24 static smx_action_t SIMIX_synchro_wait(smx_host_t smx_host, double timeout)
25 {
26   smx_action_t action;
27   action = xbt_new0(s_smx_action_t, 1);
28   action->type = SIMIX_ACTION_SYNCHRO;
29   action->request_list = xbt_fifo_new();
30   action->name = xbt_strdup("synchro");
31   action->synchro.sleep = 
32     surf_workstation_model->extension.workstation.sleep(smx_host->host, timeout);
33
34   surf_workstation_model->action_data_set(action->synchro.sleep, action);
35   return action;
36 }
37
38 void SIMIX_synchro_stop_waiting(smx_process_t process, smx_req_t req)
39 {
40   switch (req->call) {
41
42     case REQ_MUTEX_LOCK:
43       xbt_swag_remove(process, req->mutex_lock.mutex->sleeping);
44       break;
45
46     case REQ_COND_WAIT:
47       xbt_swag_remove(process, req->cond_wait.cond->sleeping);
48       break;
49
50     case REQ_COND_WAIT_TIMEOUT:
51       xbt_swag_remove(process, req->cond_wait_timeout.cond->sleeping);
52       break;
53
54     case REQ_SEM_ACQUIRE:
55       xbt_swag_remove(process, req->sem_acquire.sem->sleeping);
56       break;
57
58     case REQ_SEM_ACQUIRE_TIMEOUT:
59       xbt_swag_remove(process, req->sem_acquire_timeout.sem->sleeping);
60       break;
61
62     default:
63       THROW_IMPOSSIBLE;
64   }
65 }
66
67 void SIMIX_synchro_destroy(smx_action_t action)
68 {
69   DEBUG1("Destroying synchro %p", action);
70   action->synchro.sleep->model_type->action_unref(action->synchro.sleep);
71   xbt_fifo_free(action->request_list);
72   xbt_free(action->name);
73   xbt_free(action);
74 }
75
76 void SIMIX_post_synchro(smx_action_t action)
77 {
78   if (surf_workstation_model->action_state_get(action->synchro.sleep) == SURF_ACTION_FAILED)
79     action->state = SIMIX_FAILED;
80   else if(surf_workstation_model->action_state_get(action->synchro.sleep) == SURF_ACTION_DONE)
81     action->state = SIMIX_SRC_TIMEOUT;
82
83   SIMIX_synchro_finish(action);  
84 }
85
86 static void SIMIX_synchro_finish(smx_action_t action)
87 {
88   smx_req_t req = xbt_fifo_shift(action->request_list);
89
90   switch (action->state) {
91
92     case SIMIX_SRC_TIMEOUT:
93       TRY {
94         THROW0(timeout_error, 0, "Synchro's wait timeout");
95       } CATCH(req->issuer->running_ctx->exception) {
96         req->issuer->doexception = 1;
97       }
98       break;
99
100     case SIMIX_FAILED:
101       TRY {
102         THROW0(host_error, 0, "Host failed");
103       } CATCH(req->issuer->running_ctx->exception) {
104         req->issuer->doexception = 1;
105       }
106       break;
107
108     default:
109       THROW_IMPOSSIBLE;
110       break;
111   }
112
113   SIMIX_synchro_stop_waiting(req->issuer, req);
114   SIMIX_synchro_destroy(action);
115   SIMIX_request_answer(req);
116 }
117 /*********************************** Mutex ************************************/
118
119 /**
120  * \brief Initialize a mutex.
121  *
122  * Allocs and creates the data for the mutex.
123  * \return A mutex
124  */
125 smx_mutex_t SIMIX_mutex_init(void)
126 {
127   s_smx_process_t p;            /* useful to initialize sleeping swag */
128
129   smx_mutex_t mutex = xbt_new0(s_smx_mutex_t, 1);
130   mutex->locked = 0;
131   mutex->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
132   return mutex;
133 }
134
135 /**
136  * \brief Handle mutex lock request
137  * \param req The request
138  */
139 void SIMIX_pre_mutex_lock(smx_req_t req)
140 {
141   /* FIXME: check where to validate the arguments */
142   smx_action_t sync_act = NULL;
143   smx_mutex_t mutex = req->mutex_lock.mutex;
144   smx_process_t process = req->issuer;
145
146   if (mutex->locked) {
147     /* FIXME: check if the host is active ? */
148     /* Somebody using the mutex, use a synchro action to get host failures */
149     sync_act = SIMIX_synchro_wait(process->smx_host, -1);
150     xbt_fifo_push(sync_act->request_list, req);
151     req->issuer->waiting_action = sync_act;
152     xbt_swag_insert(req->issuer, mutex->sleeping);   
153   } else {
154     /* mutex free */
155     mutex->locked = 1;
156     mutex->owner = req->issuer;
157     SIMIX_request_answer(req);
158   }
159 }
160
161 /**
162  * \brief Tries to lock a mutex.
163  *
164  * Tries to lock a mutex, return 1 if the mutex is unlocked, else 0.
165  * This function does not block and wait for the mutex to be unlocked.
166  * \param mutex The mutex
167  * \param issuer The process that tries to acquire the mutex
168  * \return 1 - mutex free, 0 - mutex used
169  */
170 int SIMIX_mutex_trylock(smx_mutex_t mutex, smx_process_t issuer)
171 {
172   if (mutex->locked)
173     return 0;
174
175   mutex->locked = 1;
176   mutex->owner = issuer;
177   return 1;
178 }
179
180 /**
181  * \brief Unlocks a mutex.
182  *
183  * Unlocks the mutex and gives it to a process waiting for it. 
184  * If the unlocker is not the owner of the mutex nothing happens.
185  * If there are no process waiting, it sets the mutex as free.
186  * \param mutex The mutex
187  * \param issuer The process trying to unlock the mutex
188  */
189 void SIMIX_mutex_unlock(smx_mutex_t mutex, smx_process_t issuer)
190 {
191   smx_process_t p;              /*process to wake up */
192
193   /* If the mutex is not owned by the issuer do nothing */
194   if (issuer != mutex->owner)
195     return;
196
197   if (xbt_swag_size(mutex->sleeping) > 0) {
198     p = xbt_swag_extract(mutex->sleeping);
199     SIMIX_synchro_destroy(p->waiting_action);
200     p->waiting_action = NULL;
201     mutex->owner = p;
202     SIMIX_request_answer(&p->request);
203   } else {
204     /* nobody to wake up */
205     mutex->locked = 0;
206     mutex->owner = NULL;
207   }
208 }
209
210 /**
211  * \brief Destroys a mutex.
212  *
213  * Destroys and frees the mutex's memory. 
214  * \param mutex A mutex
215  */
216 void SIMIX_mutex_destroy(smx_mutex_t mutex)
217 {
218   if (mutex){
219     xbt_swag_free(mutex->sleeping);
220     xbt_free(mutex);
221   }
222 }
223
224 /********************************* Condition **********************************/
225
226 /**
227  * \brief Initialize a condition.
228  *
229  * Allocates and creates the data for the condition.
230  * It have to be called before the use of the condition.
231  * \return A condition
232  */
233 smx_cond_t SIMIX_cond_init()
234 {
235   s_smx_process_t p;
236   smx_cond_t cond = xbt_new0(s_smx_cond_t, 1);
237   cond->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
238   cond->mutex = NULL;
239   return cond;
240 }
241
242 /**
243  * \brief Handle condition waiting requests without timeouts
244  * \param The request
245  */
246 void SIMIX_pre_cond_wait(smx_req_t req)
247 {
248   smx_process_t issuer = req->issuer;
249   smx_cond_t cond = req->cond_wait.cond;
250   smx_mutex_t mutex = req->cond_wait.mutex;
251
252   _SIMIX_cond_wait(cond, mutex, -1, issuer, req);
253 }
254
255 /**
256  * \brief Handle condition waiting requests with timeouts
257  * \param The request
258  */
259 void SIMIX_pre_cond_wait_timeout(smx_req_t req)
260 {
261   smx_process_t issuer = req->issuer;
262   smx_cond_t cond = req->cond_wait_timeout.cond;
263   smx_mutex_t mutex = req->cond_wait_timeout.mutex;
264   double timeout = req->cond_wait_timeout.timeout;
265
266   _SIMIX_cond_wait(cond, mutex, timeout, issuer, req);
267 }
268
269
270 static void _SIMIX_cond_wait(smx_cond_t cond, smx_mutex_t mutex, double timeout,
271                              smx_process_t issuer, smx_req_t req)
272 {
273   smx_action_t sync_act = NULL;
274
275   DEBUG1("Wait condition %p", cond);
276
277   /* If there is a mutex unlock it */
278   /* FIXME: what happens if the issuer is not the owner of the mutex? */
279   if (mutex != NULL) {
280     cond->mutex = mutex;
281     SIMIX_mutex_unlock(mutex, issuer);
282   }
283
284   sync_act = SIMIX_synchro_wait(issuer->smx_host, timeout);
285   xbt_fifo_unshift(sync_act->request_list, req);
286   issuer->waiting_action = sync_act;
287   xbt_swag_insert(req->issuer, cond->sleeping);   
288 }
289
290 /**
291  * \brief Signalizes a condition.
292  *
293  * Signalizes a condition and wakes up a sleeping process. 
294  * If there are no process sleeping, no action is done.
295  * \param cond A condition
296  */
297 void SIMIX_cond_signal(smx_cond_t cond)
298 {
299   smx_process_t proc = NULL;
300   smx_mutex_t mutex = NULL;
301   smx_req_t req = NULL;
302
303   DEBUG1("Signal condition %p", cond);
304
305   /* If there are processes waiting for the condition choose one and try 
306      to make it acquire the mutex */
307   if ((proc = xbt_swag_extract(cond->sleeping))) {
308
309     /* Destroy waiter's synchro action */
310     SIMIX_synchro_destroy(proc->waiting_action);
311     proc->waiting_action = NULL;
312
313     /* Now transform the cond wait request into a mutex lock one */
314     req = &proc->request;
315     if(req->call == REQ_COND_WAIT)
316       mutex = req->cond_wait.mutex;
317     else
318       mutex = req->cond_wait_timeout.mutex;
319
320     req->call = REQ_MUTEX_LOCK;
321     req->mutex_lock.mutex = mutex;
322
323     SIMIX_pre_mutex_lock(req);
324   }
325 }
326
327 /**
328  * \brief Broadcasts a condition.
329  *
330  * Signal ALL processes waiting on a condition.
331  * If there are no process waiting, no action is done.
332  * \param cond A condition
333  */
334 void SIMIX_cond_broadcast(smx_cond_t cond)
335 {
336   DEBUG1("Broadcast condition %p", cond);
337
338   /* Signal the condition until nobody is waiting on it */
339   while (xbt_swag_size(cond->sleeping)) {
340     SIMIX_cond_signal(cond);
341   }
342 }
343
344 /**
345  * \brief Destroys a contidion.
346  *
347  * Destroys and frees the condition's memory. 
348  * \param cond A condition
349  */
350 void SIMIX_cond_destroy(smx_cond_t cond)
351 {
352   DEBUG1("Destroy condition %p", cond);
353
354   if (cond != NULL) {
355     xbt_assert0(xbt_swag_size(cond->sleeping) == 0,
356                 "Cannot destroy conditional since someone is still using it");
357
358     xbt_swag_free(cond->sleeping);
359     xbt_free(cond);
360   }
361 }
362
363 /******************************** Semaphores **********************************/
364 #define SMX_SEM_NOLIMIT 99999
365 /** @brief Initialize a semaphore */
366 smx_sem_t SIMIX_sem_init(unsigned int value)
367 {
368   s_smx_process_t p;
369
370   smx_sem_t sem = xbt_new0(s_smx_sem_t, 1);
371   sem->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
372   sem->value = value;
373   return sem;
374 }
375
376 /** @brief Destroys a semaphore */
377 void SIMIX_sem_destroy(smx_sem_t sem)
378 {
379   DEBUG1("Destroy semaphore %p", sem);
380   if (sem != NULL) {
381     xbt_assert0(xbt_swag_size(sem->sleeping) == 0,
382                 "Cannot destroy semaphore since someone is still using it");
383     xbt_swag_free(sem->sleeping);
384     xbt_free(sem);
385   }
386 }
387
388 /** @brief release the semaphore
389  *
390  * Unlock a process waiting on the semaphore.
391  * If no one was blocked, the semaphore capacity is increased by 1.
392  */
393 void SIMIX_sem_release(smx_sem_t sem)
394 {
395   smx_process_t proc;
396
397   DEBUG1("Sem release semaphore %p", sem);
398   if ((proc = xbt_swag_extract(sem->sleeping))) {
399     proc = xbt_swag_extract(sem->sleeping);
400     SIMIX_synchro_destroy(proc->waiting_action);
401     proc->waiting_action = NULL;
402     SIMIX_request_answer(&proc->request);
403   } else if (sem->value < SMX_SEM_NOLIMIT) {
404     sem->value++;
405   }
406 }
407
408 /** @brief Returns true if acquiring this semaphore would block */
409 XBT_INLINE int SIMIX_sem_would_block(smx_sem_t sem)
410 {
411   return (sem->value <= 0);
412 }
413
414 /** @brief Returns the current capacity of the semaphore */
415 int SIMIX_sem_get_capacity(smx_sem_t sem)
416 {
417   return sem->value;
418 }
419
420 static void _SIMIX_sem_wait(smx_sem_t sem, double timeout, smx_process_t issuer,
421                             smx_req_t req)
422 {
423   smx_action_t sync_act = NULL;
424
425   DEBUG2("Wait semaphore %p (timeout:%f)", sem, timeout);
426   if (sem->value <= 0) {
427     sync_act = SIMIX_synchro_wait(issuer->smx_host, timeout);
428     xbt_fifo_unshift(sync_act->request_list, req);
429     issuer->waiting_action = sync_act;
430     xbt_swag_insert(issuer, sem->sleeping);
431   } else {
432     sem->value--;
433     SIMIX_request_answer(req);
434   }
435 }
436
437 /**
438  * \brief Handle sem acquire requests without timeouts
439  */
440 void SIMIX_pre_sem_acquire(smx_req_t req)
441 {
442   _SIMIX_sem_wait(req->sem_acquire.sem, -1, req->issuer, req);
443 }
444
445 /**
446  * \brief Handle sem acquire requests with timeouts
447  */
448 void SIMIX_pre_sem_acquire_timeout(smx_req_t req)
449 {
450   _SIMIX_sem_wait(req->sem_acquire_timeout.sem,
451                   req->sem_acquire_timeout.timeout, req->issuer, req);  
452 }