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();
29 sync->sleep = surf_host_sleep(smx_host, timeout);
30 sync->sleep->setData(sync);
35 void SIMIX_synchro_stop_waiting(smx_process_t process, smx_simcall_t simcall)
37 XBT_IN("(%p, %p)",process,simcall);
38 switch (simcall->call) {
40 case SIMCALL_MUTEX_LOCK:
41 xbt_swag_remove(process, simcall_mutex_lock__get__mutex(simcall)->sleeping);
44 case SIMCALL_COND_WAIT:
45 xbt_swag_remove(process, simcall_cond_wait__get__cond(simcall)->sleeping);
48 case SIMCALL_COND_WAIT_TIMEOUT:
49 xbt_swag_remove(process, simcall_cond_wait_timeout__get__cond(simcall)->sleeping);
52 case SIMCALL_SEM_ACQUIRE:
53 xbt_swag_remove(process, simcall_sem_acquire__get__sem(simcall)->sleeping);
56 case SIMCALL_SEM_ACQUIRE_TIMEOUT:
57 xbt_swag_remove(process, simcall_sem_acquire_timeout__get__sem(simcall)->sleeping);
66 void SIMIX_synchro_finish(smx_synchro_t synchro)
68 XBT_IN("(%p)",synchro);
69 smx_simcall_t simcall = (smx_simcall_t) xbt_fifo_shift(synchro->simcalls);
71 switch (synchro->state) {
73 case SIMIX_SRC_TIMEOUT:
74 SMX_EXCEPTION(simcall->issuer, timeout_error, 0, "Synchro's wait timeout");
78 simcall->issuer->context->iwannadie = 1;
79 // SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
87 SIMIX_synchro_stop_waiting(simcall->issuer, simcall);
88 simcall->issuer->waiting_synchro = NULL;
90 SIMIX_simcall_answer(simcall);
93 /*********************************** Mutex ************************************/
95 smx_mutex_t simcall_HANDLER_mutex_init(smx_simcall_t simcall){
96 return SIMIX_mutex_init();
99 * \brief Initialize a mutex.
101 * Allocs and creates the data for the mutex.
104 smx_mutex_t SIMIX_mutex_init(void)
107 simgrid::simix::Process p; /* useful to initialize sleeping swag */
109 smx_mutex_t mutex = xbt_new0(s_smx_mutex_t, 1);
111 mutex->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
117 * \brief Handles a mutex lock simcall.
118 * \param simcall the simcall
120 void simcall_HANDLER_mutex_lock(smx_simcall_t simcall, smx_mutex_t mutex)
122 XBT_IN("(%p)",simcall);
123 /* FIXME: check where to validate the arguments */
124 smx_synchro_t synchro = NULL;
125 smx_process_t process = simcall->issuer;
128 /* FIXME: check if the host is active ? */
129 /* Somebody using the mutex, use a synchronization to get host failures */
130 synchro = SIMIX_synchro_wait(process->host, -1);
131 xbt_fifo_push(synchro->simcalls, simcall);
132 simcall->issuer->waiting_synchro = synchro;
133 xbt_swag_insert(simcall->issuer, mutex->sleeping);
137 mutex->owner = simcall->issuer;
138 SIMIX_simcall_answer(simcall);
143 int simcall_HANDLER_mutex_trylock(smx_simcall_t simcall, smx_mutex_t mutex){
144 return SIMIX_mutex_trylock(mutex, simcall->issuer);
147 * \brief Tries to lock a mutex.
149 * Tries to lock a mutex, return 1 if the mutex is unlocked, else 0.
150 * This function does not block and wait for the mutex to be unlocked.
151 * \param mutex The mutex
152 * \param issuer The process that tries to acquire the mutex
153 * \return 1 - mutex free, 0 - mutex used
155 int SIMIX_mutex_trylock(smx_mutex_t mutex, smx_process_t issuer)
157 XBT_IN("(%p, %p)",mutex,issuer);
164 mutex->owner = issuer;
169 void simcall_HANDLER_mutex_unlock(smx_simcall_t simcall, smx_mutex_t mutex){
170 SIMIX_mutex_unlock(mutex, simcall->issuer);
173 * \brief Unlocks a mutex.
175 * Unlocks the mutex and gives it to a process waiting for it.
176 * If the unlocker is not the owner of the mutex nothing happens.
177 * If there are no process waiting, it sets the mutex as free.
178 * \param mutex The mutex
179 * \param issuer The process trying to unlock the mutex
181 void SIMIX_mutex_unlock(smx_mutex_t mutex, smx_process_t issuer)
183 XBT_IN("(%p, %p)",mutex,issuer);
185 /* If the mutex is not owned by the issuer, that's not good */
186 if (issuer != mutex->owner)
187 THROWF(mismatch_error, 0, "Cannot release that mutex: it was locked by %s (pid:%d), not by you.",
188 SIMIX_process_get_name(mutex->owner),SIMIX_process_get_PID(mutex->owner));
190 if (xbt_swag_size(mutex->sleeping) > 0) {
191 /*process to wake up */
192 smx_process_t p = (smx_process_t) xbt_swag_extract(mutex->sleeping);
193 delete p->waiting_synchro;
194 p->waiting_synchro = NULL;
196 SIMIX_simcall_answer(&p->simcall);
198 /* nobody to wake up */
206 * \brief Destroys a mutex.
208 * Destroys and frees the mutex's memory.
209 * \param mutex A mutex
211 void SIMIX_mutex_destroy(smx_mutex_t mutex)
213 XBT_IN("(%p)",mutex);
215 xbt_swag_free(mutex->sleeping);
221 /********************************* Condition **********************************/
224 * \brief Initialize a condition.
226 * Allocates and creates the data for the condition.
227 * It have to be called before the use of the condition.
228 * \return A condition
230 smx_cond_t SIMIX_cond_init(void)
233 simgrid::simix::Process p;
234 smx_cond_t cond = xbt_new0(s_smx_cond_t, 1);
235 cond->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
242 * \brief Handle a condition waiting simcall without timeouts
243 * \param simcall the simcall
245 void simcall_HANDLER_cond_wait(smx_simcall_t simcall, smx_cond_t cond, smx_mutex_t mutex)
247 XBT_IN("(%p)",simcall);
248 smx_process_t issuer = simcall->issuer;
250 _SIMIX_cond_wait(cond, mutex, -1, issuer, simcall);
255 * \brief Handle a condition waiting simcall with timeouts
256 * \param simcall the simcall
258 void simcall_HANDLER_cond_wait_timeout(smx_simcall_t simcall, smx_cond_t cond,
259 smx_mutex_t mutex, double timeout)
261 XBT_IN("(%p)",simcall);
262 smx_process_t issuer = simcall->issuer;
264 _SIMIX_cond_wait(cond, mutex, timeout, issuer, simcall);
269 static void _SIMIX_cond_wait(smx_cond_t cond, smx_mutex_t mutex, double timeout,
270 smx_process_t issuer, smx_simcall_t simcall)
272 XBT_IN("(%p, %p, %f, %p,%p)",cond,mutex,timeout,issuer,simcall);
273 smx_synchro_t synchro = NULL;
275 XBT_DEBUG("Wait condition %p", cond);
277 /* If there is a mutex unlock it */
278 /* FIXME: what happens if the issuer is not the owner of the mutex? */
281 SIMIX_mutex_unlock(mutex, issuer);
284 synchro = SIMIX_synchro_wait(issuer->host, timeout);
285 xbt_fifo_unshift(synchro->simcalls, simcall);
286 issuer->waiting_synchro = synchro;
287 xbt_swag_insert(simcall->issuer, cond->sleeping);
292 * \brief Signalizes a condition.
294 * Signalizes a condition and wakes up a sleeping process.
295 * If there are no process sleeping, no action is done.
296 * \param cond A condition
298 void SIMIX_cond_signal(smx_cond_t cond)
301 smx_process_t proc = NULL;
302 smx_mutex_t mutex = NULL;
303 smx_simcall_t simcall = NULL;
305 XBT_DEBUG("Signal condition %p", cond);
307 /* If there are processes waiting for the condition choose one and try
308 to make it acquire the mutex */
309 if ((proc = (smx_process_t) xbt_swag_extract(cond->sleeping))) {
311 /* Destroy waiter's synchronization */
312 delete proc->waiting_synchro;
313 proc->waiting_synchro = NULL;
315 /* Now transform the cond wait simcall into a mutex lock one */
316 simcall = &proc->simcall;
317 if(simcall->call == SIMCALL_COND_WAIT)
318 mutex = simcall_cond_wait__get__mutex(simcall);
320 mutex = simcall_cond_wait_timeout__get__mutex(simcall);
321 simcall->call = SIMCALL_MUTEX_LOCK;
323 simcall_HANDLER_mutex_lock(simcall, mutex);
329 * \brief Broadcasts a condition.
331 * Signal ALL processes waiting on a condition.
332 * If there are no process waiting, no action is done.
333 * \param cond A condition
335 void SIMIX_cond_broadcast(smx_cond_t cond)
338 XBT_DEBUG("Broadcast condition %p", cond);
340 /* Signal the condition until nobody is waiting on it */
341 while (xbt_swag_size(cond->sleeping)) {
342 SIMIX_cond_signal(cond);
348 * \brief Destroys a condition.
350 * Destroys and frees the condition's memory.
351 * \param cond A condition
353 void SIMIX_cond_destroy(smx_cond_t cond)
356 XBT_DEBUG("Destroy condition %p", cond);
359 xbt_assert(xbt_swag_size(cond->sleeping) == 0,
360 "Cannot destroy conditional since someone is still using it");
362 xbt_swag_free(cond->sleeping);
368 /******************************** Semaphores **********************************/
369 #define SMX_SEM_NOLIMIT 99999
370 /** @brief Initialize a semaphore */
371 smx_sem_t SIMIX_sem_init(unsigned int value)
373 XBT_IN("(%u)",value);
374 simgrid::simix::Process p;
376 smx_sem_t sem = xbt_new0(s_smx_sem_t, 1);
377 sem->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
383 /** @brief Destroys a semaphore */
384 void SIMIX_sem_destroy(smx_sem_t sem)
387 XBT_DEBUG("Destroy semaphore %p", sem);
389 xbt_assert(xbt_swag_size(sem->sleeping) == 0,
390 "Cannot destroy semaphore since someone is still using it");
391 xbt_swag_free(sem->sleeping);
397 void simcall_HANDLER_sem_release(smx_simcall_t simcall, smx_sem_t sem){
398 SIMIX_sem_release(sem);
400 /** @brief release the semaphore
402 * Unlock a process waiting on the semaphore.
403 * If no one was blocked, the semaphore capacity is increased by 1.
405 void SIMIX_sem_release(smx_sem_t sem)
410 XBT_DEBUG("Sem release semaphore %p", sem);
411 if ((proc = (smx_process_t) xbt_swag_extract(sem->sleeping))) {
412 delete proc->waiting_synchro;
413 proc->waiting_synchro = NULL;
414 SIMIX_simcall_answer(&proc->simcall);
415 } else if (sem->value < SMX_SEM_NOLIMIT) {
421 /** @brief Returns true if acquiring this semaphore would block */
422 int SIMIX_sem_would_block(smx_sem_t sem)
426 return (sem->value <= 0);
429 int simcall_HANDLER_sem_get_capacity(smx_simcall_t simcall, smx_sem_t sem){
430 return SIMIX_sem_get_capacity(sem);
432 /** @brief Returns the current capacity of the semaphore */
433 int SIMIX_sem_get_capacity(smx_sem_t sem)
440 static void _SIMIX_sem_wait(smx_sem_t sem, double timeout, smx_process_t issuer,
441 smx_simcall_t simcall)
443 XBT_IN("(%p, %f, %p, %p)",sem,timeout,issuer,simcall);
444 smx_synchro_t synchro = NULL;
446 XBT_DEBUG("Wait semaphore %p (timeout:%f)", sem, timeout);
447 if (sem->value <= 0) {
448 synchro = SIMIX_synchro_wait(issuer->host, timeout);
449 xbt_fifo_unshift(synchro->simcalls, simcall);
450 issuer->waiting_synchro = synchro;
451 xbt_swag_insert(issuer, sem->sleeping);
454 SIMIX_simcall_answer(simcall);
460 * \brief Handles a sem acquire simcall without timeout.
461 * \param simcall the simcall
463 void simcall_HANDLER_sem_acquire(smx_simcall_t simcall, smx_sem_t sem)
465 XBT_IN("(%p)",simcall);
466 _SIMIX_sem_wait(sem, -1, simcall->issuer, simcall);
471 * \brief Handles a sem acquire simcall with timeout.
472 * \param simcall the simcall
474 void simcall_HANDLER_sem_acquire_timeout(smx_simcall_t simcall, smx_sem_t sem, double timeout)
476 XBT_IN("(%p)",simcall);
477 _SIMIX_sem_wait(sem, timeout, simcall->issuer, simcall);
480 int simcall_HANDLER_sem_would_block(smx_simcall_t simcall, smx_sem_t sem) {
481 return SIMIX_sem_would_block(sem);