Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
code simplification around simgrid::surf::NetCardImpl
[simgrid.git] / src / simix / smx_synchro.cpp
1 /* Copyright (c) 2007-2015. 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 "src/surf/surf_interface.hpp"
8 #include "smx_private.h"
9 #include "xbt/log.h"
10
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_synchro, simix,
13                                 "SIMIX Synchronization (mutex, semaphores and conditions)");
14
15 static smx_synchro_t SIMIX_synchro_wait(sg_host_t smx_host, double timeout);
16 static void SIMIX_synchro_finish(smx_synchro_t synchro);
17 static void _SIMIX_cond_wait(smx_cond_t cond, smx_mutex_t mutex, double timeout,
18                              smx_process_t issuer, smx_simcall_t simcall);
19 static void _SIMIX_sem_wait(smx_sem_t sem, double timeout, smx_process_t issuer,
20                             smx_simcall_t simcall);
21
22 /***************************** Raw synchronization *********************************/
23
24 static smx_synchro_t SIMIX_synchro_wait(sg_host_t smx_host, double timeout)
25 {
26   XBT_IN("(%p, %f)",smx_host,timeout);
27
28   smx_synchro_t sync;
29   sync = (smx_synchro_t) xbt_mallocator_get(simix_global->synchro_mallocator);
30   sync->type = SIMIX_SYNC_SYNCHRO;
31   sync->name = xbt_strdup("synchro");
32   sync->synchro.sleep = surf_host_sleep(smx_host, timeout);
33
34   sync->synchro.sleep->setData(sync);
35   XBT_OUT();
36   return sync;
37 }
38
39 void SIMIX_synchro_stop_waiting(smx_process_t process, smx_simcall_t simcall)
40 {
41   XBT_IN("(%p, %p)",process,simcall);
42   switch (simcall->call) {
43
44     case SIMCALL_MUTEX_LOCK:
45       xbt_swag_remove(process, simcall_mutex_lock__get__mutex(simcall)->sleeping);
46       break;
47
48     case SIMCALL_COND_WAIT:
49       xbt_swag_remove(process, simcall_cond_wait__get__cond(simcall)->sleeping);
50       break;
51
52     case SIMCALL_COND_WAIT_TIMEOUT:
53       xbt_swag_remove(process, simcall_cond_wait_timeout__get__cond(simcall)->sleeping);
54       break;
55
56     case SIMCALL_SEM_ACQUIRE:
57       xbt_swag_remove(process, simcall_sem_acquire__get__sem(simcall)->sleeping);
58       break;
59
60     case SIMCALL_SEM_ACQUIRE_TIMEOUT:
61       xbt_swag_remove(process, simcall_sem_acquire_timeout__get__sem(simcall)->sleeping);
62       break;
63
64     default:
65       THROW_IMPOSSIBLE;
66   }
67   XBT_OUT();
68 }
69
70 void SIMIX_synchro_destroy(smx_synchro_t synchro)
71 {
72   XBT_IN("(%p)",synchro);
73   XBT_DEBUG("Destroying synchro %p", synchro);
74   xbt_assert(synchro->type == SIMIX_SYNC_SYNCHRO);
75   synchro->synchro.sleep->unref();
76   xbt_free(synchro->name);
77   xbt_mallocator_release(simix_global->synchro_mallocator, synchro);
78   XBT_OUT();
79 }
80
81 void SIMIX_post_synchro(smx_synchro_t synchro)
82 {
83   XBT_IN("(%p)",synchro);
84   xbt_assert(synchro->type == SIMIX_SYNC_SYNCHRO);
85   if (synchro->synchro.sleep->getState() == SURF_ACTION_FAILED)
86     synchro->state = SIMIX_FAILED;
87   else if(synchro->synchro.sleep->getState() == SURF_ACTION_DONE)
88     synchro->state = SIMIX_SRC_TIMEOUT;
89
90   SIMIX_synchro_finish(synchro);  
91   XBT_OUT();
92 }
93
94 static void SIMIX_synchro_finish(smx_synchro_t synchro)
95 {
96   XBT_IN("(%p)",synchro);
97   smx_simcall_t simcall = (smx_simcall_t) xbt_fifo_shift(synchro->simcalls);
98
99   switch (synchro->state) {
100
101     case SIMIX_SRC_TIMEOUT:
102       SMX_EXCEPTION(simcall->issuer, timeout_error, 0, "Synchro's wait timeout");
103       break;
104
105     case SIMIX_FAILED:
106         simcall->issuer->context->iwannadie = 1;
107 //      SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
108       break;
109
110     default:
111       THROW_IMPOSSIBLE;
112       break;
113   }
114
115   SIMIX_synchro_stop_waiting(simcall->issuer, simcall);
116   simcall->issuer->waiting_synchro = NULL;
117   SIMIX_synchro_destroy(synchro);
118   SIMIX_simcall_answer(simcall);
119   XBT_OUT();
120 }
121 /*********************************** Mutex ************************************/
122
123 smx_mutex_t simcall_HANDLER_mutex_init(smx_simcall_t simcall){
124   return SIMIX_mutex_init();
125 }
126 /**
127  * \brief Initialize a mutex.
128  *
129  * Allocs and creates the data for the mutex.
130  * \return A mutex
131  */
132 smx_mutex_t SIMIX_mutex_init(void)
133 {
134   XBT_IN("()");
135   s_smx_process_t p;            /* useful to initialize sleeping swag */
136
137   smx_mutex_t mutex = xbt_new0(s_smx_mutex_t, 1);
138   mutex->locked = 0;
139   mutex->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
140   XBT_OUT();
141   return mutex;
142 }
143
144 /**
145  * \brief Handles a mutex lock simcall.
146  * \param simcall the simcall
147  */
148 void simcall_HANDLER_mutex_lock(smx_simcall_t simcall, smx_mutex_t mutex)
149 {
150   XBT_IN("(%p)",simcall);
151   /* FIXME: check where to validate the arguments */
152   smx_synchro_t synchro = NULL;
153   smx_process_t process = simcall->issuer;
154
155   if (mutex->locked) {
156     /* FIXME: check if the host is active ? */
157     /* Somebody using the mutex, use a synchronization to get host failures */
158     synchro = SIMIX_synchro_wait(process->host, -1);
159     xbt_fifo_push(synchro->simcalls, simcall);
160     simcall->issuer->waiting_synchro = synchro;
161     xbt_swag_insert(simcall->issuer, mutex->sleeping);   
162   } else {
163     /* mutex free */
164     mutex->locked = 1;
165     mutex->owner = simcall->issuer;
166     SIMIX_simcall_answer(simcall);
167   }
168   XBT_OUT();
169 }
170
171 int simcall_HANDLER_mutex_trylock(smx_simcall_t simcall, smx_mutex_t mutex){
172   return SIMIX_mutex_trylock(mutex, simcall->issuer);
173 }
174 /**
175  * \brief Tries to lock a mutex.
176  *
177  * Tries to lock a mutex, return 1 if the mutex is unlocked, else 0.
178  * This function does not block and wait for the mutex to be unlocked.
179  * \param mutex The mutex
180  * \param issuer The process that tries to acquire the mutex
181  * \return 1 - mutex free, 0 - mutex used
182  */
183 int SIMIX_mutex_trylock(smx_mutex_t mutex, smx_process_t issuer)
184 {
185   XBT_IN("(%p, %p)",mutex,issuer);
186   if (mutex->locked){
187     XBT_OUT();
188     return 0;
189   }
190
191   mutex->locked = 1;
192   mutex->owner = issuer;
193   XBT_OUT();
194   return 1;
195 }
196
197 void simcall_HANDLER_mutex_unlock(smx_simcall_t simcall, smx_mutex_t mutex){
198    SIMIX_mutex_unlock(mutex, simcall->issuer);
199 }
200 /**
201  * \brief Unlocks a mutex.
202  *
203  * Unlocks the mutex and gives it to a process waiting for it. 
204  * If the unlocker is not the owner of the mutex nothing happens.
205  * If there are no process waiting, it sets the mutex as free.
206  * \param mutex The mutex
207  * \param issuer The process trying to unlock the mutex
208  */
209 void SIMIX_mutex_unlock(smx_mutex_t mutex, smx_process_t issuer)
210 {
211   XBT_IN("(%p, %p)",mutex,issuer);
212
213   /* If the mutex is not owned by the issuer, that's not good */
214   if (issuer != mutex->owner)
215     THROWF(mismatch_error, 0, "Cannot release that mutex: it was locked by %s (pid:%d), not by you.",
216         SIMIX_process_get_name(mutex->owner),SIMIX_process_get_PID(mutex->owner));
217
218   if (xbt_swag_size(mutex->sleeping) > 0) {
219     /*process to wake up */
220     smx_process_t p = (smx_process_t) xbt_swag_extract(mutex->sleeping);
221     SIMIX_synchro_destroy(p->waiting_synchro);
222     p->waiting_synchro = NULL;
223     mutex->owner = p;
224     SIMIX_simcall_answer(&p->simcall);
225   } else {
226     /* nobody to wake up */
227     mutex->locked = 0;
228     mutex->owner = NULL;
229   }
230   XBT_OUT();
231 }
232
233 /**
234  * \brief Destroys a mutex.
235  *
236  * Destroys and frees the mutex's memory. 
237  * \param mutex A mutex
238  */
239 void SIMIX_mutex_destroy(smx_mutex_t mutex)
240 {
241   XBT_IN("(%p)",mutex);
242   if (mutex){
243     xbt_swag_free(mutex->sleeping);
244     xbt_free(mutex);
245   }
246   XBT_OUT();
247 }
248
249 /********************************* Condition **********************************/
250
251 /**
252  * \brief Initialize a condition.
253  *
254  * Allocates and creates the data for the condition.
255  * It have to be called before the use of the condition.
256  * \return A condition
257  */
258 smx_cond_t SIMIX_cond_init(void)
259 {
260   XBT_IN("()");
261   s_smx_process_t p;
262   smx_cond_t cond = xbt_new0(s_smx_cond_t, 1);
263   cond->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
264   cond->mutex = NULL;
265   XBT_OUT();
266   return cond;
267 }
268
269 /**
270  * \brief Handle a condition waiting simcall without timeouts
271  * \param simcall the simcall
272  */
273 void simcall_HANDLER_cond_wait(smx_simcall_t simcall, smx_cond_t cond, smx_mutex_t mutex)
274 {
275   XBT_IN("(%p)",simcall);
276   smx_process_t issuer = simcall->issuer;
277
278   _SIMIX_cond_wait(cond, mutex, -1, issuer, simcall);
279   XBT_OUT();
280 }
281
282 /**
283  * \brief Handle a condition waiting simcall with timeouts
284  * \param simcall the simcall
285  */
286 void simcall_HANDLER_cond_wait_timeout(smx_simcall_t simcall, smx_cond_t cond,
287                      smx_mutex_t mutex, double timeout)
288 {
289   XBT_IN("(%p)",simcall);
290   smx_process_t issuer = simcall->issuer;
291
292   _SIMIX_cond_wait(cond, mutex, timeout, issuer, simcall);
293   XBT_OUT();
294 }
295
296
297 static void _SIMIX_cond_wait(smx_cond_t cond, smx_mutex_t mutex, double timeout,
298                              smx_process_t issuer, smx_simcall_t simcall)
299 {
300   XBT_IN("(%p, %p, %f, %p,%p)",cond,mutex,timeout,issuer,simcall);
301   smx_synchro_t synchro = NULL;
302
303   XBT_DEBUG("Wait condition %p", cond);
304
305   /* If there is a mutex unlock it */
306   /* FIXME: what happens if the issuer is not the owner of the mutex? */
307   if (mutex != NULL) {
308     cond->mutex = mutex;
309     SIMIX_mutex_unlock(mutex, issuer);
310   }
311
312   synchro = SIMIX_synchro_wait(issuer->host, timeout);
313   xbt_fifo_unshift(synchro->simcalls, simcall);
314   issuer->waiting_synchro = synchro;
315   xbt_swag_insert(simcall->issuer, cond->sleeping);   
316   XBT_OUT();
317 }
318
319 /**
320  * \brief Signalizes a condition.
321  *
322  * Signalizes a condition and wakes up a sleeping process. 
323  * If there are no process sleeping, no action is done.
324  * \param cond A condition
325  */
326 void SIMIX_cond_signal(smx_cond_t cond)
327 {
328   XBT_IN("(%p)",cond);
329   smx_process_t proc = NULL;
330   smx_mutex_t mutex = NULL;
331   smx_simcall_t simcall = NULL;
332
333   XBT_DEBUG("Signal condition %p", cond);
334
335   /* If there are processes waiting for the condition choose one and try 
336      to make it acquire the mutex */
337   if ((proc = (smx_process_t) xbt_swag_extract(cond->sleeping))) {
338
339     /* Destroy waiter's synchronization */
340     SIMIX_synchro_destroy(proc->waiting_synchro);
341     proc->waiting_synchro = NULL;
342
343     /* Now transform the cond wait simcall into a mutex lock one */
344     simcall = &proc->simcall;
345     if(simcall->call == SIMCALL_COND_WAIT)
346       mutex = simcall_cond_wait__get__mutex(simcall);
347     else
348       mutex = simcall_cond_wait_timeout__get__mutex(simcall);
349     simcall->call = SIMCALL_MUTEX_LOCK;
350
351     simcall_HANDLER_mutex_lock(simcall, mutex);
352   }
353   XBT_OUT();
354 }
355
356 /**
357  * \brief Broadcasts a condition.
358  *
359  * Signal ALL processes waiting on a condition.
360  * If there are no process waiting, no action is done.
361  * \param cond A condition
362  */
363 void SIMIX_cond_broadcast(smx_cond_t cond)
364 {
365   XBT_IN("(%p)",cond);
366   XBT_DEBUG("Broadcast condition %p", cond);
367
368   /* Signal the condition until nobody is waiting on it */
369   while (xbt_swag_size(cond->sleeping)) {
370     SIMIX_cond_signal(cond);
371   }
372   XBT_OUT();
373 }
374
375 /**
376  * \brief Destroys a condition.
377  *
378  * Destroys and frees the condition's memory. 
379  * \param cond A condition
380  */
381 void SIMIX_cond_destroy(smx_cond_t cond)
382 {
383   XBT_IN("(%p)",cond);
384   XBT_DEBUG("Destroy condition %p", cond);
385
386   if (cond != NULL) {
387     xbt_assert(xbt_swag_size(cond->sleeping) == 0,
388                 "Cannot destroy conditional since someone is still using it");
389
390     xbt_swag_free(cond->sleeping);
391     xbt_free(cond);
392   }
393   XBT_OUT();
394 }
395
396 /******************************** Semaphores **********************************/
397 #define SMX_SEM_NOLIMIT 99999
398 /** @brief Initialize a semaphore */
399 smx_sem_t SIMIX_sem_init(unsigned int value)
400 {
401   XBT_IN("(%u)",value);
402   s_smx_process_t p;
403
404   smx_sem_t sem = xbt_new0(s_smx_sem_t, 1);
405   sem->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
406   sem->value = value;
407   XBT_OUT();
408   return sem;
409 }
410
411 /** @brief Destroys a semaphore */
412 void SIMIX_sem_destroy(smx_sem_t sem)
413 {
414   XBT_IN("(%p)",sem);
415   XBT_DEBUG("Destroy semaphore %p", sem);
416   if (sem != NULL) {
417     xbt_assert(xbt_swag_size(sem->sleeping) == 0,
418                 "Cannot destroy semaphore since someone is still using it");
419     xbt_swag_free(sem->sleeping);
420     xbt_free(sem);
421   }
422   XBT_OUT();
423 }
424
425 void simcall_HANDLER_sem_release(smx_simcall_t simcall, smx_sem_t sem){
426   SIMIX_sem_release(sem);
427 }
428 /** @brief release the semaphore
429  *
430  * Unlock a process waiting on the semaphore.
431  * If no one was blocked, the semaphore capacity is increased by 1.
432  */
433 void SIMIX_sem_release(smx_sem_t sem)
434 {
435   XBT_IN("(%p)",sem);
436   smx_process_t proc;
437
438   XBT_DEBUG("Sem release semaphore %p", sem);
439   if ((proc = (smx_process_t) xbt_swag_extract(sem->sleeping))) {
440     SIMIX_synchro_destroy(proc->waiting_synchro);
441     proc->waiting_synchro = NULL;
442     SIMIX_simcall_answer(&proc->simcall);
443   } else if (sem->value < SMX_SEM_NOLIMIT) {
444     sem->value++;
445   }
446   XBT_OUT();
447 }
448
449 /** @brief Returns true if acquiring this semaphore would block */
450 int SIMIX_sem_would_block(smx_sem_t sem)
451 {
452   XBT_IN("(%p)",sem);
453   XBT_OUT();
454   return (sem->value <= 0);
455 }
456
457 int simcall_HANDLER_sem_get_capacity(smx_simcall_t simcall, smx_sem_t sem){
458   return SIMIX_sem_get_capacity(sem);
459 }
460 /** @brief Returns the current capacity of the semaphore */
461 int SIMIX_sem_get_capacity(smx_sem_t sem)
462 {
463   XBT_IN("(%p)",sem);
464   XBT_OUT();
465   return sem->value;
466 }
467
468 static void _SIMIX_sem_wait(smx_sem_t sem, double timeout, smx_process_t issuer,
469                             smx_simcall_t simcall)
470 {
471   XBT_IN("(%p, %f, %p, %p)",sem,timeout,issuer,simcall);
472   smx_synchro_t synchro = NULL;
473
474   XBT_DEBUG("Wait semaphore %p (timeout:%f)", sem, timeout);
475   if (sem->value <= 0) {
476     synchro = SIMIX_synchro_wait(issuer->host, timeout);
477     xbt_fifo_unshift(synchro->simcalls, simcall);
478     issuer->waiting_synchro = synchro;
479     xbt_swag_insert(issuer, sem->sleeping);
480   } else {
481     sem->value--;
482     SIMIX_simcall_answer(simcall);
483   }
484   XBT_OUT();
485 }
486
487 /**
488  * \brief Handles a sem acquire simcall without timeout.
489  * \param simcall the simcall
490  */
491 void simcall_HANDLER_sem_acquire(smx_simcall_t simcall, smx_sem_t sem)
492 {
493   XBT_IN("(%p)",simcall);
494   _SIMIX_sem_wait(sem, -1, simcall->issuer, simcall);
495   XBT_OUT();
496 }
497
498 /**
499  * \brief Handles a sem acquire simcall with timeout.
500  * \param simcall the simcall
501  */
502 void simcall_HANDLER_sem_acquire_timeout(smx_simcall_t simcall, smx_sem_t sem, double timeout)
503 {
504   XBT_IN("(%p)",simcall);
505   _SIMIX_sem_wait(sem, timeout, simcall->issuer, simcall);  
506   XBT_OUT();
507 }
508 int simcall_HANDLER_sem_would_block(smx_simcall_t simcall, smx_sem_t sem) {
509   return SIMIX_sem_would_block(sem);
510 }