1 /* Copyright (c) 2007-2015. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #include "src/surf/surf_interface.hpp"
8 #include "smx_private.h"
11 #include "src/simix/SynchroRaw.hpp"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_synchro, simix,
14 "SIMIX Synchronization (mutex, semaphores and conditions)");
16 static smx_synchro_t SIMIX_synchro_wait(sg_host_t smx_host, double timeout);
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);
22 /***************************** Raw synchronization *********************************/
24 static smx_synchro_t SIMIX_synchro_wait(sg_host_t smx_host, double timeout)
26 XBT_IN("(%p, %f)",smx_host,timeout);
28 simgrid::simix::Raw *sync = new simgrid::simix::Raw();
30 sync->sleep = surf_host_sleep(smx_host, timeout);
31 sync->sleep->setData(sync);
36 void SIMIX_synchro_stop_waiting(smx_process_t process, smx_simcall_t simcall)
38 XBT_IN("(%p, %p)",process,simcall);
39 switch (simcall->call) {
41 case SIMCALL_MUTEX_LOCK:
42 xbt_swag_remove(process, simcall_mutex_lock__get__mutex(simcall)->sleeping);
45 case SIMCALL_COND_WAIT:
46 xbt_swag_remove(process, simcall_cond_wait__get__cond(simcall)->sleeping);
49 case SIMCALL_COND_WAIT_TIMEOUT:
50 xbt_swag_remove(process, simcall_cond_wait_timeout__get__cond(simcall)->sleeping);
53 case SIMCALL_SEM_ACQUIRE:
54 xbt_swag_remove(process, simcall_sem_acquire__get__sem(simcall)->sleeping);
57 case SIMCALL_SEM_ACQUIRE_TIMEOUT:
58 xbt_swag_remove(process, simcall_sem_acquire_timeout__get__sem(simcall)->sleeping);
67 void SIMIX_synchro_destroy(smx_synchro_t synchro)
69 XBT_DEBUG("Destroying synchro %p", synchro);
70 simgrid::simix::Raw *raw = static_cast<simgrid::simix::Raw*>(synchro);
76 void SIMIX_synchro_finish(smx_synchro_t synchro)
78 XBT_IN("(%p)",synchro);
79 smx_simcall_t simcall = (smx_simcall_t) xbt_fifo_shift(synchro->simcalls);
81 switch (synchro->state) {
83 case SIMIX_SRC_TIMEOUT:
84 SMX_EXCEPTION(simcall->issuer, timeout_error, 0, "Synchro's wait timeout");
88 simcall->issuer->context->iwannadie = 1;
89 // SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
97 SIMIX_synchro_stop_waiting(simcall->issuer, simcall);
98 simcall->issuer->waiting_synchro = NULL;
99 SIMIX_synchro_destroy(synchro);
100 SIMIX_simcall_answer(simcall);
103 /*********************************** Mutex ************************************/
105 smx_mutex_t simcall_HANDLER_mutex_init(smx_simcall_t simcall){
106 return SIMIX_mutex_init();
109 * \brief Initialize a mutex.
111 * Allocs and creates the data for the mutex.
114 smx_mutex_t SIMIX_mutex_init(void)
117 s_smx_process_t p; /* useful to initialize sleeping swag */
119 smx_mutex_t mutex = xbt_new0(s_smx_mutex_t, 1);
121 mutex->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
127 * \brief Handles a mutex lock simcall.
128 * \param simcall the simcall
130 void simcall_HANDLER_mutex_lock(smx_simcall_t simcall, smx_mutex_t mutex)
132 XBT_IN("(%p)",simcall);
133 /* FIXME: check where to validate the arguments */
134 smx_synchro_t synchro = NULL;
135 smx_process_t process = simcall->issuer;
138 /* FIXME: check if the host is active ? */
139 /* Somebody using the mutex, use a synchronization to get host failures */
140 synchro = SIMIX_synchro_wait(process->host, -1);
141 xbt_fifo_push(synchro->simcalls, simcall);
142 simcall->issuer->waiting_synchro = synchro;
143 xbt_swag_insert(simcall->issuer, mutex->sleeping);
147 mutex->owner = simcall->issuer;
148 SIMIX_simcall_answer(simcall);
153 int simcall_HANDLER_mutex_trylock(smx_simcall_t simcall, smx_mutex_t mutex){
154 return SIMIX_mutex_trylock(mutex, simcall->issuer);
157 * \brief Tries to lock a mutex.
159 * Tries to lock a mutex, return 1 if the mutex is unlocked, else 0.
160 * This function does not block and wait for the mutex to be unlocked.
161 * \param mutex The mutex
162 * \param issuer The process that tries to acquire the mutex
163 * \return 1 - mutex free, 0 - mutex used
165 int SIMIX_mutex_trylock(smx_mutex_t mutex, smx_process_t issuer)
167 XBT_IN("(%p, %p)",mutex,issuer);
174 mutex->owner = issuer;
179 void simcall_HANDLER_mutex_unlock(smx_simcall_t simcall, smx_mutex_t mutex){
180 SIMIX_mutex_unlock(mutex, simcall->issuer);
183 * \brief Unlocks a mutex.
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
191 void SIMIX_mutex_unlock(smx_mutex_t mutex, smx_process_t issuer)
193 XBT_IN("(%p, %p)",mutex,issuer);
195 /* If the mutex is not owned by the issuer, that's not good */
196 if (issuer != mutex->owner)
197 THROWF(mismatch_error, 0, "Cannot release that mutex: it was locked by %s (pid:%d), not by you.",
198 SIMIX_process_get_name(mutex->owner),SIMIX_process_get_PID(mutex->owner));
200 if (xbt_swag_size(mutex->sleeping) > 0) {
201 /*process to wake up */
202 smx_process_t p = (smx_process_t) xbt_swag_extract(mutex->sleeping);
203 SIMIX_synchro_destroy(p->waiting_synchro);
204 p->waiting_synchro = NULL;
206 SIMIX_simcall_answer(&p->simcall);
208 /* nobody to wake up */
216 * \brief Destroys a mutex.
218 * Destroys and frees the mutex's memory.
219 * \param mutex A mutex
221 void SIMIX_mutex_destroy(smx_mutex_t mutex)
223 XBT_IN("(%p)",mutex);
225 xbt_swag_free(mutex->sleeping);
231 /********************************* Condition **********************************/
234 * \brief Initialize a condition.
236 * Allocates and creates the data for the condition.
237 * It have to be called before the use of the condition.
238 * \return A condition
240 smx_cond_t SIMIX_cond_init(void)
244 smx_cond_t cond = xbt_new0(s_smx_cond_t, 1);
245 cond->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
252 * \brief Handle a condition waiting simcall without timeouts
253 * \param simcall the simcall
255 void simcall_HANDLER_cond_wait(smx_simcall_t simcall, smx_cond_t cond, smx_mutex_t mutex)
257 XBT_IN("(%p)",simcall);
258 smx_process_t issuer = simcall->issuer;
260 _SIMIX_cond_wait(cond, mutex, -1, issuer, simcall);
265 * \brief Handle a condition waiting simcall with timeouts
266 * \param simcall the simcall
268 void simcall_HANDLER_cond_wait_timeout(smx_simcall_t simcall, smx_cond_t cond,
269 smx_mutex_t mutex, double timeout)
271 XBT_IN("(%p)",simcall);
272 smx_process_t issuer = simcall->issuer;
274 _SIMIX_cond_wait(cond, mutex, timeout, issuer, simcall);
279 static void _SIMIX_cond_wait(smx_cond_t cond, smx_mutex_t mutex, double timeout,
280 smx_process_t issuer, smx_simcall_t simcall)
282 XBT_IN("(%p, %p, %f, %p,%p)",cond,mutex,timeout,issuer,simcall);
283 smx_synchro_t synchro = NULL;
285 XBT_DEBUG("Wait condition %p", cond);
287 /* If there is a mutex unlock it */
288 /* FIXME: what happens if the issuer is not the owner of the mutex? */
291 SIMIX_mutex_unlock(mutex, issuer);
294 synchro = SIMIX_synchro_wait(issuer->host, timeout);
295 xbt_fifo_unshift(synchro->simcalls, simcall);
296 issuer->waiting_synchro = synchro;
297 xbt_swag_insert(simcall->issuer, cond->sleeping);
302 * \brief Signalizes a condition.
304 * Signalizes a condition and wakes up a sleeping process.
305 * If there are no process sleeping, no action is done.
306 * \param cond A condition
308 void SIMIX_cond_signal(smx_cond_t cond)
311 smx_process_t proc = NULL;
312 smx_mutex_t mutex = NULL;
313 smx_simcall_t simcall = NULL;
315 XBT_DEBUG("Signal condition %p", cond);
317 /* If there are processes waiting for the condition choose one and try
318 to make it acquire the mutex */
319 if ((proc = (smx_process_t) xbt_swag_extract(cond->sleeping))) {
321 /* Destroy waiter's synchronization */
322 SIMIX_synchro_destroy(proc->waiting_synchro);
323 proc->waiting_synchro = NULL;
325 /* Now transform the cond wait simcall into a mutex lock one */
326 simcall = &proc->simcall;
327 if(simcall->call == SIMCALL_COND_WAIT)
328 mutex = simcall_cond_wait__get__mutex(simcall);
330 mutex = simcall_cond_wait_timeout__get__mutex(simcall);
331 simcall->call = SIMCALL_MUTEX_LOCK;
333 simcall_HANDLER_mutex_lock(simcall, mutex);
339 * \brief Broadcasts a condition.
341 * Signal ALL processes waiting on a condition.
342 * If there are no process waiting, no action is done.
343 * \param cond A condition
345 void SIMIX_cond_broadcast(smx_cond_t cond)
348 XBT_DEBUG("Broadcast condition %p", cond);
350 /* Signal the condition until nobody is waiting on it */
351 while (xbt_swag_size(cond->sleeping)) {
352 SIMIX_cond_signal(cond);
358 * \brief Destroys a condition.
360 * Destroys and frees the condition's memory.
361 * \param cond A condition
363 void SIMIX_cond_destroy(smx_cond_t cond)
366 XBT_DEBUG("Destroy condition %p", cond);
369 xbt_assert(xbt_swag_size(cond->sleeping) == 0,
370 "Cannot destroy conditional since someone is still using it");
372 xbt_swag_free(cond->sleeping);
378 /******************************** Semaphores **********************************/
379 #define SMX_SEM_NOLIMIT 99999
380 /** @brief Initialize a semaphore */
381 smx_sem_t SIMIX_sem_init(unsigned int value)
383 XBT_IN("(%u)",value);
386 smx_sem_t sem = xbt_new0(s_smx_sem_t, 1);
387 sem->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
393 /** @brief Destroys a semaphore */
394 void SIMIX_sem_destroy(smx_sem_t sem)
397 XBT_DEBUG("Destroy semaphore %p", sem);
399 xbt_assert(xbt_swag_size(sem->sleeping) == 0,
400 "Cannot destroy semaphore since someone is still using it");
401 xbt_swag_free(sem->sleeping);
407 void simcall_HANDLER_sem_release(smx_simcall_t simcall, smx_sem_t sem){
408 SIMIX_sem_release(sem);
410 /** @brief release the semaphore
412 * Unlock a process waiting on the semaphore.
413 * If no one was blocked, the semaphore capacity is increased by 1.
415 void SIMIX_sem_release(smx_sem_t sem)
420 XBT_DEBUG("Sem release semaphore %p", sem);
421 if ((proc = (smx_process_t) xbt_swag_extract(sem->sleeping))) {
422 SIMIX_synchro_destroy(proc->waiting_synchro);
423 proc->waiting_synchro = NULL;
424 SIMIX_simcall_answer(&proc->simcall);
425 } else if (sem->value < SMX_SEM_NOLIMIT) {
431 /** @brief Returns true if acquiring this semaphore would block */
432 int SIMIX_sem_would_block(smx_sem_t sem)
436 return (sem->value <= 0);
439 int simcall_HANDLER_sem_get_capacity(smx_simcall_t simcall, smx_sem_t sem){
440 return SIMIX_sem_get_capacity(sem);
442 /** @brief Returns the current capacity of the semaphore */
443 int SIMIX_sem_get_capacity(smx_sem_t sem)
450 static void _SIMIX_sem_wait(smx_sem_t sem, double timeout, smx_process_t issuer,
451 smx_simcall_t simcall)
453 XBT_IN("(%p, %f, %p, %p)",sem,timeout,issuer,simcall);
454 smx_synchro_t synchro = NULL;
456 XBT_DEBUG("Wait semaphore %p (timeout:%f)", sem, timeout);
457 if (sem->value <= 0) {
458 synchro = SIMIX_synchro_wait(issuer->host, timeout);
459 xbt_fifo_unshift(synchro->simcalls, simcall);
460 issuer->waiting_synchro = synchro;
461 xbt_swag_insert(issuer, sem->sleeping);
464 SIMIX_simcall_answer(simcall);
470 * \brief Handles a sem acquire simcall without timeout.
471 * \param simcall the simcall
473 void simcall_HANDLER_sem_acquire(smx_simcall_t simcall, smx_sem_t sem)
475 XBT_IN("(%p)",simcall);
476 _SIMIX_sem_wait(sem, -1, simcall->issuer, simcall);
481 * \brief Handles a sem acquire simcall with timeout.
482 * \param simcall the simcall
484 void simcall_HANDLER_sem_acquire_timeout(smx_simcall_t simcall, smx_sem_t sem, double timeout)
486 XBT_IN("(%p)",simcall);
487 _SIMIX_sem_wait(sem, timeout, simcall->issuer, simcall);
490 int simcall_HANDLER_sem_would_block(smx_simcall_t simcall, smx_sem_t sem) {
491 return SIMIX_sem_would_block(sem);