Logo AND Algorithmique Numérique Distribuée

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