Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Biggest commit ever (SIMIX2): the user processes can now run in parallel
[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     case SIMIX_SRC_TIMEOUT:
94       TRY {
95         THROW0(timeout_error, 0, "Synchro's wait timeout");
96       } CATCH(req->issuer->running_ctx->exception) {
97         req->issuer->doexception = 1;
98       }
99       break;
100
101     case SIMIX_FAILED:
102       TRY {
103         THROW0(host_error, 0, "Host failed");
104       } CATCH(req->issuer->running_ctx->exception) {
105         req->issuer->doexception = 1;
106       }
107       break;
108       
109     default:
110       THROW_IMPOSSIBLE;
111       break;
112   }
113
114   SIMIX_synchro_stop_waiting(req->issuer, req);
115   SIMIX_synchro_destroy(action);
116   SIMIX_request_answer(req);
117 }
118 /*********************************** Mutex ************************************/
119
120 /**
121  * \brief Initialize a mutex.
122  *
123  * Allocs and creates the data for the mutex.
124  * \return A mutex
125  */
126 smx_mutex_t SIMIX_mutex_init(void)
127 {
128   s_smx_process_t p;            /* useful to initialize sleeping swag */
129
130   smx_mutex_t mutex = xbt_new0(s_smx_mutex_t, 1);
131   mutex->locked = 0;
132   mutex->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
133   return mutex;
134 }
135
136 /**
137  * \brief Handle mutex lock request
138  * \param req The request
139  */
140 void SIMIX_pre_mutex_lock(smx_req_t req)
141 {
142   /*FIXME: check where to validate the arguments */
143   smx_action_t sync_act = NULL;
144   smx_mutex_t mutex = req->mutex_lock.mutex;
145   smx_process_t process = req->issuer;
146   
147   if (mutex->locked) {
148     /* FIXME: check if the host is active ? */
149     /* Somebody using the mutex, use a synchro action to get host failures */
150     sync_act = SIMIX_synchro_wait(process->smx_host, -1);
151     xbt_fifo_push(sync_act->request_list, req);
152     req->issuer->waiting_action = sync_act;
153     xbt_swag_insert(req->issuer, mutex->sleeping);   
154   } else {
155     /* mutex free */
156     mutex->locked = 1;
157     mutex->owner = req->issuer;
158     SIMIX_request_answer(req);
159   }
160 }
161
162 /**
163  * \brief Tries to lock a mutex.
164  *
165  * Tries to lock a mutex, return 1 if the mutex is unlocked, else 0.
166  * This function does not block and wait for the mutex to be unlocked.
167  * \param mutex The mutex
168  * \param issuer The process that tries to acquire the mutex
169  * \return 1 - mutex free, 0 - mutex used
170  */
171 int SIMIX_mutex_trylock(smx_mutex_t mutex, smx_process_t issuer)
172 {
173   if (mutex->locked)
174     return 0;
175   else {
176     mutex->locked = 1;
177     mutex->owner = issuer;
178     return 1;
179   }
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 happen 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     return;
363   }
364 }
365
366 /******************************** Semaphores **********************************/
367 #define SMX_SEM_NOLIMIT 99999
368 /** @brief Initialize a semaphore */
369 smx_sem_t SIMIX_sem_init(unsigned int value)
370 {
371   s_smx_process_t p;
372
373   smx_sem_t sem = xbt_new0(s_smx_sem_t, 1);
374   sem->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
375   sem->value = value;
376   return sem;
377 }
378
379 /** @brief Destroys a semaphore */
380 void SIMIX_sem_destroy(smx_sem_t sem)
381 {
382   DEBUG1("Destroy semaphore %p", sem);
383   if (sem != NULL){
384     xbt_assert0(xbt_swag_size(sem->sleeping) == 0,
385                 "Cannot destroy semaphore since someone is still using it");
386     xbt_swag_free(sem->sleeping);
387     xbt_free(sem);
388   }
389 }
390
391 /** @brief release the semaphore
392  *
393  * Unlock a process waiting on the semaphore.
394  * If no one was blocked, the semaphore capacity is increased by 1.
395  * */
396 void SIMIX_sem_release(smx_sem_t sem)
397 {
398   smx_process_t proc;
399   
400   DEBUG1("Sem release semaphore %p", sem);
401   if ((proc = xbt_swag_extract(sem->sleeping))) {
402     proc = xbt_swag_extract(sem->sleeping);
403     SIMIX_synchro_destroy(proc->waiting_action);
404     proc->waiting_action = NULL;
405     SIMIX_request_answer(proc->request);
406   }else if(sem->value < SMX_SEM_NOLIMIT){
407     sem->value++;
408   }
409 }
410
411 /** @brief Returns true if acquiring this semaphore would block */
412 XBT_INLINE int SIMIX_sem_would_block(smx_sem_t sem)
413 {
414   return (sem->value <= 0);
415 }
416
417 /** @brief Returns the current capacity of the semaphore */
418 int SIMIX_sem_get_capacity(smx_sem_t sem)
419 {
420   return sem->value;
421 }
422
423 static void _SIMIX_sem_wait(smx_sem_t sem, double timeout, smx_process_t issuer,
424                             smx_req_t req)
425 {
426   smx_action_t sync_act = NULL;
427   
428   DEBUG2("Wait semaphore %p (timeout:%f)", sem, timeout);
429   if(sem->value <= 0){
430     sync_act = SIMIX_synchro_wait(issuer->smx_host, timeout);
431     xbt_fifo_unshift(sync_act->request_list, req);
432     issuer->waiting_action = sync_act;
433     xbt_swag_insert(issuer, sem->sleeping);
434   }else{
435     sem->value--;
436     SIMIX_request_answer(req);
437   }
438 }
439
440 /**
441  * \brief Handle sem acquire requests without timeouts
442  */
443 void SIMIX_pre_sem_acquire(smx_req_t req)
444 {
445   _SIMIX_sem_wait(req->sem_acquire.sem, -1, req->issuer, req);
446 }
447
448 /**
449  * \brief Handle sem acquire requests with timeouts
450  */
451 void SIMIX_pre_sem_acquire_timeout(smx_req_t req)
452 {
453   _SIMIX_sem_wait(req->sem_acquire_timeout.sem,
454                   req->sem_acquire_timeout.timeout, req->issuer, req);  
455 }