1 /* Copyright (c) 2007, 2008, 2009, 2010. 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. */
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_synchro, simix,
12 "Logging specific to SIMIX (synchronization)");
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,
20 static void SIMIX_sem_block_onto(smx_sem_t sem);
22 /***************************** Synchro action *********************************/
24 static smx_action_t SIMIX_synchro_wait(smx_host_t smx_host, double timeout)
26 XBT_IN("(%p, %f)",smx_host,timeout);
28 action = xbt_mallocator_get(simix_global->action_mallocator);
29 action->type = SIMIX_ACTION_SYNCHRO;
30 action->name = xbt_strdup("synchro");
31 action->synchro.sleep =
32 surf_workstation_model->extension.workstation.sleep(smx_host->host, timeout);
34 surf_workstation_model->action_data_set(action->synchro.sleep, action);
39 void SIMIX_synchro_stop_waiting(smx_process_t process, smx_req_t req)
41 XBT_IN("(%p, %p)",process,req);
45 xbt_swag_remove(process, req->mutex_lock.mutex->sleeping);
49 xbt_swag_remove(process, req->cond_wait.cond->sleeping);
52 case REQ_COND_WAIT_TIMEOUT:
53 xbt_swag_remove(process, req->cond_wait_timeout.cond->sleeping);
57 xbt_swag_remove(process, req->sem_acquire.sem->sleeping);
60 case REQ_SEM_ACQUIRE_TIMEOUT:
61 xbt_swag_remove(process, req->sem_acquire_timeout.sem->sleeping);
70 void SIMIX_synchro_destroy(smx_action_t action)
72 XBT_IN("(%p)",action);
73 XBT_DEBUG("Destroying synchro %p", action);
74 action->synchro.sleep->model_type->action_unref(action->synchro.sleep);
75 xbt_free(action->name);
76 xbt_mallocator_release(simix_global->action_mallocator, action);
80 void SIMIX_post_synchro(smx_action_t action)
82 XBT_IN("(%p)",action);
83 if (surf_workstation_model->action_state_get(action->synchro.sleep) == SURF_ACTION_FAILED)
84 action->state = SIMIX_FAILED;
85 else if(surf_workstation_model->action_state_get(action->synchro.sleep) == SURF_ACTION_DONE)
86 action->state = SIMIX_SRC_TIMEOUT;
88 SIMIX_synchro_finish(action);
92 static void SIMIX_synchro_finish(smx_action_t action)
94 XBT_IN("(%p)",action);
95 smx_req_t req = xbt_fifo_shift(action->request_list);
97 switch (action->state) {
99 case SIMIX_SRC_TIMEOUT:
101 THROWF(timeout_error, 0, "Synchro's wait timeout");
103 CATCH(req->issuer->running_ctx->exception) {
104 req->issuer->doexception = 1;
110 THROWF(host_error, 0, "Host failed");
112 CATCH(req->issuer->running_ctx->exception) {
113 req->issuer->doexception = 1;
122 SIMIX_synchro_stop_waiting(req->issuer, req);
123 SIMIX_synchro_destroy(action);
124 SIMIX_request_answer(req);
127 /*********************************** Mutex ************************************/
130 * \brief Initialize a mutex.
132 * Allocs and creates the data for the mutex.
135 smx_mutex_t SIMIX_mutex_init(void)
138 s_smx_process_t p; /* useful to initialize sleeping swag */
140 smx_mutex_t mutex = xbt_new0(s_smx_mutex_t, 1);
142 mutex->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
148 * \brief Handle mutex lock request
149 * \param req The request
151 void SIMIX_pre_mutex_lock(smx_req_t req)
154 /* FIXME: check where to validate the arguments */
155 smx_action_t sync_act = NULL;
156 smx_mutex_t mutex = req->mutex_lock.mutex;
157 smx_process_t process = req->issuer;
160 /* FIXME: check if the host is active ? */
161 /* Somebody using the mutex, use a synchro action to get host failures */
162 sync_act = SIMIX_synchro_wait(process->smx_host, -1);
163 xbt_fifo_push(sync_act->request_list, req);
164 req->issuer->waiting_action = sync_act;
165 xbt_swag_insert(req->issuer, mutex->sleeping);
169 mutex->owner = req->issuer;
170 SIMIX_request_answer(req);
176 * \brief Tries to lock a mutex.
178 * Tries to lock a mutex, return 1 if the mutex is unlocked, else 0.
179 * This function does not block and wait for the mutex to be unlocked.
180 * \param mutex The mutex
181 * \param issuer The process that tries to acquire the mutex
182 * \return 1 - mutex free, 0 - mutex used
184 int SIMIX_mutex_trylock(smx_mutex_t mutex, smx_process_t issuer)
186 XBT_IN("(%p, %p)",mutex,issuer);
193 mutex->owner = issuer;
199 * \brief Unlocks a mutex.
201 * Unlocks the mutex and gives it to a process waiting for it.
202 * If the unlocker is not the owner of the mutex nothing happens.
203 * If there are no process waiting, it sets the mutex as free.
204 * \param mutex The mutex
205 * \param issuer The process trying to unlock the mutex
207 void SIMIX_mutex_unlock(smx_mutex_t mutex, smx_process_t issuer)
209 XBT_IN("(%p, %p)",mutex,issuer);
210 smx_process_t p; /*process to wake up */
212 /* If the mutex is not owned by the issuer do nothing */
213 if (issuer != mutex->owner){
218 if (xbt_swag_size(mutex->sleeping) > 0) {
219 p = xbt_swag_extract(mutex->sleeping);
220 SIMIX_synchro_destroy(p->waiting_action);
221 p->waiting_action = NULL;
223 SIMIX_request_answer(&p->request);
225 /* nobody to wake up */
233 * \brief Destroys a mutex.
235 * Destroys and frees the mutex's memory.
236 * \param mutex A mutex
238 void SIMIX_mutex_destroy(smx_mutex_t mutex)
240 XBT_IN("(%p)",mutex);
242 xbt_swag_free(mutex->sleeping);
248 /********************************* Condition **********************************/
251 * \brief Initialize a condition.
253 * Allocates and creates the data for the condition.
254 * It have to be called before the use of the condition.
255 * \return A condition
257 smx_cond_t SIMIX_cond_init()
261 smx_cond_t cond = xbt_new0(s_smx_cond_t, 1);
262 cond->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
269 * \brief Handle condition waiting requests without timeouts
272 void SIMIX_pre_cond_wait(smx_req_t req)
275 smx_process_t issuer = req->issuer;
276 smx_cond_t cond = req->cond_wait.cond;
277 smx_mutex_t mutex = req->cond_wait.mutex;
279 _SIMIX_cond_wait(cond, mutex, -1, issuer, req);
284 * \brief Handle condition waiting requests with timeouts
287 void SIMIX_pre_cond_wait_timeout(smx_req_t req)
290 smx_process_t issuer = req->issuer;
291 smx_cond_t cond = req->cond_wait_timeout.cond;
292 smx_mutex_t mutex = req->cond_wait_timeout.mutex;
293 double timeout = req->cond_wait_timeout.timeout;
295 _SIMIX_cond_wait(cond, mutex, timeout, issuer, req);
300 static void _SIMIX_cond_wait(smx_cond_t cond, smx_mutex_t mutex, double timeout,
301 smx_process_t issuer, smx_req_t req)
303 XBT_IN("(%p, %p, %f, %p,%p)",cond,mutex,timeout,issuer,req);
304 smx_action_t sync_act = NULL;
306 XBT_DEBUG("Wait condition %p", cond);
308 /* If there is a mutex unlock it */
309 /* FIXME: what happens if the issuer is not the owner of the mutex? */
312 SIMIX_mutex_unlock(mutex, issuer);
315 sync_act = SIMIX_synchro_wait(issuer->smx_host, timeout);
316 xbt_fifo_unshift(sync_act->request_list, req);
317 issuer->waiting_action = sync_act;
318 xbt_swag_insert(req->issuer, cond->sleeping);
323 * \brief Signalizes a condition.
325 * Signalizes a condition and wakes up a sleeping process.
326 * If there are no process sleeping, no action is done.
327 * \param cond A condition
329 void SIMIX_cond_signal(smx_cond_t cond)
332 smx_process_t proc = NULL;
333 smx_mutex_t mutex = NULL;
334 smx_req_t req = NULL;
336 XBT_DEBUG("Signal condition %p", cond);
338 /* If there are processes waiting for the condition choose one and try
339 to make it acquire the mutex */
340 if ((proc = xbt_swag_extract(cond->sleeping))) {
342 /* Destroy waiter's synchro action */
343 SIMIX_synchro_destroy(proc->waiting_action);
344 proc->waiting_action = NULL;
346 /* Now transform the cond wait request into a mutex lock one */
347 req = &proc->request;
348 if(req->call == REQ_COND_WAIT)
349 mutex = req->cond_wait.mutex;
351 mutex = req->cond_wait_timeout.mutex;
353 req->call = REQ_MUTEX_LOCK;
354 req->mutex_lock.mutex = mutex;
356 SIMIX_pre_mutex_lock(req);
362 * \brief Broadcasts a condition.
364 * Signal ALL processes waiting on a condition.
365 * If there are no process waiting, no action is done.
366 * \param cond A condition
368 void SIMIX_cond_broadcast(smx_cond_t cond)
371 XBT_DEBUG("Broadcast condition %p", cond);
373 /* Signal the condition until nobody is waiting on it */
374 while (xbt_swag_size(cond->sleeping)) {
375 SIMIX_cond_signal(cond);
381 * \brief Destroys a contidion.
383 * Destroys and frees the condition's memory.
384 * \param cond A condition
386 void SIMIX_cond_destroy(smx_cond_t cond)
389 XBT_DEBUG("Destroy condition %p", cond);
392 xbt_assert(xbt_swag_size(cond->sleeping) == 0,
393 "Cannot destroy conditional since someone is still using it");
395 xbt_swag_free(cond->sleeping);
401 /******************************** Semaphores **********************************/
402 #define SMX_SEM_NOLIMIT 99999
403 /** @brief Initialize a semaphore */
404 smx_sem_t SIMIX_sem_init(unsigned int value)
406 XBT_IN("(%d)",value);
409 smx_sem_t sem = xbt_new0(s_smx_sem_t, 1);
410 sem->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
416 /** @brief Destroys a semaphore */
417 void SIMIX_sem_destroy(smx_sem_t sem)
420 XBT_DEBUG("Destroy semaphore %p", sem);
422 xbt_assert(xbt_swag_size(sem->sleeping) == 0,
423 "Cannot destroy semaphore since someone is still using it");
424 xbt_swag_free(sem->sleeping);
430 /** @brief release the semaphore
432 * Unlock a process waiting on the semaphore.
433 * If no one was blocked, the semaphore capacity is increased by 1.
435 void SIMIX_sem_release(smx_sem_t sem)
440 XBT_DEBUG("Sem release semaphore %p", sem);
441 if ((proc = xbt_swag_extract(sem->sleeping))) {
442 proc = xbt_swag_extract(sem->sleeping);
443 SIMIX_synchro_destroy(proc->waiting_action);
444 proc->waiting_action = NULL;
445 SIMIX_request_answer(&proc->request);
446 } else if (sem->value < SMX_SEM_NOLIMIT) {
452 /** @brief Returns true if acquiring this semaphore would block */
453 XBT_INLINE int SIMIX_sem_would_block(smx_sem_t sem)
457 return (sem->value <= 0);
460 /** @brief Returns the current capacity of the semaphore */
461 int SIMIX_sem_get_capacity(smx_sem_t sem)
468 static void _SIMIX_sem_wait(smx_sem_t sem, double timeout, smx_process_t issuer,
471 XBT_IN("(%p, %f, %p, %p)",sem,timeout,issuer,req);
472 smx_action_t sync_act = NULL;
474 XBT_DEBUG("Wait semaphore %p (timeout:%f)", sem, timeout);
475 if (sem->value <= 0) {
476 sync_act = SIMIX_synchro_wait(issuer->smx_host, timeout);
477 xbt_fifo_unshift(sync_act->request_list, req);
478 issuer->waiting_action = sync_act;
479 xbt_swag_insert(issuer, sem->sleeping);
482 SIMIX_request_answer(req);
488 * \brief Handle sem acquire requests without timeouts
490 void SIMIX_pre_sem_acquire(smx_req_t req)
493 _SIMIX_sem_wait(req->sem_acquire.sem, -1, req->issuer, req);
498 * \brief Handle sem acquire requests with timeouts
500 void SIMIX_pre_sem_acquire_timeout(smx_req_t req)
503 _SIMIX_sem_wait(req->sem_acquire_timeout.sem,
504 req->sem_acquire_timeout.timeout, req->issuer, req);