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_finish(smx_synchro_t synchro)
69 XBT_IN("(%p)",synchro);
70 smx_simcall_t simcall = (smx_simcall_t) xbt_fifo_shift(synchro->simcalls);
72 switch (synchro->state) {
74 case SIMIX_SRC_TIMEOUT:
75 SMX_EXCEPTION(simcall->issuer, timeout_error, 0, "Synchro's wait timeout");
79 simcall->issuer->context->iwannadie = 1;
80 // SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
88 SIMIX_synchro_stop_waiting(simcall->issuer, simcall);
89 simcall->issuer->waiting_synchro = NULL;
91 SIMIX_simcall_answer(simcall);
94 /*********************************** Mutex ************************************/
96 smx_mutex_t simcall_HANDLER_mutex_init(smx_simcall_t simcall){
97 return SIMIX_mutex_init();
100 * \brief Initialize a mutex.
102 * Allocs and creates the data for the mutex.
105 smx_mutex_t SIMIX_mutex_init(void)
108 s_smx_process_t p; /* useful to initialize sleeping swag */
110 smx_mutex_t mutex = xbt_new0(s_smx_mutex_t, 1);
112 mutex->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
118 * \brief Handles a mutex lock simcall.
119 * \param simcall the simcall
121 void simcall_HANDLER_mutex_lock(smx_simcall_t simcall, smx_mutex_t mutex)
123 XBT_IN("(%p)",simcall);
124 /* FIXME: check where to validate the arguments */
125 smx_synchro_t synchro = NULL;
126 smx_process_t process = simcall->issuer;
129 /* FIXME: check if the host is active ? */
130 /* Somebody using the mutex, use a synchronization to get host failures */
131 synchro = SIMIX_synchro_wait(process->host, -1);
132 xbt_fifo_push(synchro->simcalls, simcall);
133 simcall->issuer->waiting_synchro = synchro;
134 xbt_swag_insert(simcall->issuer, mutex->sleeping);
138 mutex->owner = simcall->issuer;
139 SIMIX_simcall_answer(simcall);
144 int simcall_HANDLER_mutex_trylock(smx_simcall_t simcall, smx_mutex_t mutex){
145 return SIMIX_mutex_trylock(mutex, simcall->issuer);
148 * \brief Tries to lock a mutex.
150 * Tries to lock a mutex, return 1 if the mutex is unlocked, else 0.
151 * This function does not block and wait for the mutex to be unlocked.
152 * \param mutex The mutex
153 * \param issuer The process that tries to acquire the mutex
154 * \return 1 - mutex free, 0 - mutex used
156 int SIMIX_mutex_trylock(smx_mutex_t mutex, smx_process_t issuer)
158 XBT_IN("(%p, %p)",mutex,issuer);
165 mutex->owner = issuer;
170 void simcall_HANDLER_mutex_unlock(smx_simcall_t simcall, smx_mutex_t mutex){
171 SIMIX_mutex_unlock(mutex, simcall->issuer);
174 * \brief Unlocks a mutex.
176 * Unlocks the mutex and gives it to a process waiting for it.
177 * If the unlocker is not the owner of the mutex nothing happens.
178 * If there are no process waiting, it sets the mutex as free.
179 * \param mutex The mutex
180 * \param issuer The process trying to unlock the mutex
182 void SIMIX_mutex_unlock(smx_mutex_t mutex, smx_process_t issuer)
184 XBT_IN("(%p, %p)",mutex,issuer);
186 /* If the mutex is not owned by the issuer, that's not good */
187 if (issuer != mutex->owner)
188 THROWF(mismatch_error, 0, "Cannot release that mutex: it was locked by %s (pid:%d), not by you.",
189 SIMIX_process_get_name(mutex->owner),SIMIX_process_get_PID(mutex->owner));
191 if (xbt_swag_size(mutex->sleeping) > 0) {
192 /*process to wake up */
193 smx_process_t p = (smx_process_t) xbt_swag_extract(mutex->sleeping);
194 delete p->waiting_synchro;
195 p->waiting_synchro = NULL;
197 SIMIX_simcall_answer(&p->simcall);
199 /* nobody to wake up */
207 * \brief Destroys a mutex.
209 * Destroys and frees the mutex's memory.
210 * \param mutex A mutex
212 void SIMIX_mutex_destroy(smx_mutex_t mutex)
214 XBT_IN("(%p)",mutex);
216 xbt_swag_free(mutex->sleeping);
222 /********************************* Condition **********************************/
225 * \brief Initialize a condition.
227 * Allocates and creates the data for the condition.
228 * It have to be called before the use of the condition.
229 * \return A condition
231 smx_cond_t SIMIX_cond_init(void)
235 smx_cond_t cond = xbt_new0(s_smx_cond_t, 1);
236 cond->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
243 * \brief Handle a condition waiting simcall without timeouts
244 * \param simcall the simcall
246 void simcall_HANDLER_cond_wait(smx_simcall_t simcall, smx_cond_t cond, smx_mutex_t mutex)
248 XBT_IN("(%p)",simcall);
249 smx_process_t issuer = simcall->issuer;
251 _SIMIX_cond_wait(cond, mutex, -1, issuer, simcall);
256 * \brief Handle a condition waiting simcall with timeouts
257 * \param simcall the simcall
259 void simcall_HANDLER_cond_wait_timeout(smx_simcall_t simcall, smx_cond_t cond,
260 smx_mutex_t mutex, double timeout)
262 XBT_IN("(%p)",simcall);
263 smx_process_t issuer = simcall->issuer;
265 _SIMIX_cond_wait(cond, mutex, timeout, issuer, simcall);
270 static void _SIMIX_cond_wait(smx_cond_t cond, smx_mutex_t mutex, double timeout,
271 smx_process_t issuer, smx_simcall_t simcall)
273 XBT_IN("(%p, %p, %f, %p,%p)",cond,mutex,timeout,issuer,simcall);
274 smx_synchro_t synchro = NULL;
276 XBT_DEBUG("Wait condition %p", cond);
278 /* If there is a mutex unlock it */
279 /* FIXME: what happens if the issuer is not the owner of the mutex? */
282 SIMIX_mutex_unlock(mutex, issuer);
285 synchro = SIMIX_synchro_wait(issuer->host, timeout);
286 xbt_fifo_unshift(synchro->simcalls, simcall);
287 issuer->waiting_synchro = synchro;
288 xbt_swag_insert(simcall->issuer, cond->sleeping);
293 * \brief Signalizes a condition.
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
299 void SIMIX_cond_signal(smx_cond_t cond)
302 smx_process_t proc = NULL;
303 smx_mutex_t mutex = NULL;
304 smx_simcall_t simcall = NULL;
306 XBT_DEBUG("Signal condition %p", cond);
308 /* If there are processes waiting for the condition choose one and try
309 to make it acquire the mutex */
310 if ((proc = (smx_process_t) xbt_swag_extract(cond->sleeping))) {
312 /* Destroy waiter's synchronization */
313 delete proc->waiting_synchro;
314 proc->waiting_synchro = NULL;
316 /* Now transform the cond wait simcall into a mutex lock one */
317 simcall = &proc->simcall;
318 if(simcall->call == SIMCALL_COND_WAIT)
319 mutex = simcall_cond_wait__get__mutex(simcall);
321 mutex = simcall_cond_wait_timeout__get__mutex(simcall);
322 simcall->call = SIMCALL_MUTEX_LOCK;
324 simcall_HANDLER_mutex_lock(simcall, mutex);
330 * \brief Broadcasts a condition.
332 * Signal ALL processes waiting on a condition.
333 * If there are no process waiting, no action is done.
334 * \param cond A condition
336 void SIMIX_cond_broadcast(smx_cond_t cond)
339 XBT_DEBUG("Broadcast condition %p", cond);
341 /* Signal the condition until nobody is waiting on it */
342 while (xbt_swag_size(cond->sleeping)) {
343 SIMIX_cond_signal(cond);
349 * \brief Destroys a condition.
351 * Destroys and frees the condition's memory.
352 * \param cond A condition
354 void SIMIX_cond_destroy(smx_cond_t cond)
357 XBT_DEBUG("Destroy condition %p", cond);
360 xbt_assert(xbt_swag_size(cond->sleeping) == 0,
361 "Cannot destroy conditional since someone is still using it");
363 xbt_swag_free(cond->sleeping);
369 /******************************** Semaphores **********************************/
370 #define SMX_SEM_NOLIMIT 99999
371 /** @brief Initialize a semaphore */
372 smx_sem_t SIMIX_sem_init(unsigned int value)
374 XBT_IN("(%u)",value);
377 smx_sem_t sem = xbt_new0(s_smx_sem_t, 1);
378 sem->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
384 /** @brief Destroys a semaphore */
385 void SIMIX_sem_destroy(smx_sem_t sem)
388 XBT_DEBUG("Destroy semaphore %p", sem);
390 xbt_assert(xbt_swag_size(sem->sleeping) == 0,
391 "Cannot destroy semaphore since someone is still using it");
392 xbt_swag_free(sem->sleeping);
398 void simcall_HANDLER_sem_release(smx_simcall_t simcall, smx_sem_t sem){
399 SIMIX_sem_release(sem);
401 /** @brief release the semaphore
403 * Unlock a process waiting on the semaphore.
404 * If no one was blocked, the semaphore capacity is increased by 1.
406 void SIMIX_sem_release(smx_sem_t sem)
411 XBT_DEBUG("Sem release semaphore %p", sem);
412 if ((proc = (smx_process_t) xbt_swag_extract(sem->sleeping))) {
413 delete proc->waiting_synchro;
414 proc->waiting_synchro = NULL;
415 SIMIX_simcall_answer(&proc->simcall);
416 } else if (sem->value < SMX_SEM_NOLIMIT) {
422 /** @brief Returns true if acquiring this semaphore would block */
423 int SIMIX_sem_would_block(smx_sem_t sem)
427 return (sem->value <= 0);
430 int simcall_HANDLER_sem_get_capacity(smx_simcall_t simcall, smx_sem_t sem){
431 return SIMIX_sem_get_capacity(sem);
433 /** @brief Returns the current capacity of the semaphore */
434 int SIMIX_sem_get_capacity(smx_sem_t sem)
441 static void _SIMIX_sem_wait(smx_sem_t sem, double timeout, smx_process_t issuer,
442 smx_simcall_t simcall)
444 XBT_IN("(%p, %f, %p, %p)",sem,timeout,issuer,simcall);
445 smx_synchro_t synchro = NULL;
447 XBT_DEBUG("Wait semaphore %p (timeout:%f)", sem, timeout);
448 if (sem->value <= 0) {
449 synchro = SIMIX_synchro_wait(issuer->host, timeout);
450 xbt_fifo_unshift(synchro->simcalls, simcall);
451 issuer->waiting_synchro = synchro;
452 xbt_swag_insert(issuer, sem->sleeping);
455 SIMIX_simcall_answer(simcall);
461 * \brief Handles a sem acquire simcall without timeout.
462 * \param simcall the simcall
464 void simcall_HANDLER_sem_acquire(smx_simcall_t simcall, smx_sem_t sem)
466 XBT_IN("(%p)",simcall);
467 _SIMIX_sem_wait(sem, -1, simcall->issuer, simcall);
472 * \brief Handles a sem acquire simcall with timeout.
473 * \param simcall the simcall
475 void simcall_HANDLER_sem_acquire_timeout(smx_simcall_t simcall, smx_sem_t sem, double timeout)
477 XBT_IN("(%p)",simcall);
478 _SIMIX_sem_wait(sem, timeout, simcall->issuer, simcall);
481 int simcall_HANDLER_sem_would_block(smx_simcall_t simcall, smx_sem_t sem) {
482 return SIMIX_sem_would_block(sem);