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)
27 action = xbt_new0(s_smx_action_t, 1);
28 action->type = SIMIX_ACTION_SYNCHRO;
29 action->request_list = xbt_fifo_new();
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);
38 void SIMIX_synchro_stop_waiting(smx_process_t process, smx_req_t req)
43 xbt_swag_remove(process, req->mutex_lock.mutex->sleeping);
47 xbt_swag_remove(process, req->cond_wait.cond->sleeping);
50 case REQ_COND_WAIT_TIMEOUT:
51 xbt_swag_remove(process, req->cond_wait_timeout.cond->sleeping);
55 xbt_swag_remove(process, req->sem_acquire.sem->sleeping);
58 case REQ_SEM_ACQUIRE_TIMEOUT:
59 xbt_swag_remove(process, req->sem_acquire_timeout.sem->sleeping);
67 void SIMIX_synchro_destroy(smx_action_t action)
69 DEBUG1("Destroying synchro %p", action);
70 action->synchro.sleep->model_type->action_unref(action->synchro.sleep);
71 xbt_fifo_free(action->request_list);
72 xbt_free(action->name);
76 void SIMIX_post_synchro(smx_action_t action)
78 if (surf_workstation_model->action_state_get(action->synchro.sleep) == SURF_ACTION_FAILED)
79 action->state = SIMIX_FAILED;
80 else if(surf_workstation_model->action_state_get(action->synchro.sleep) == SURF_ACTION_DONE)
81 action->state = SIMIX_SRC_TIMEOUT;
83 SIMIX_synchro_finish(action);
86 static void SIMIX_synchro_finish(smx_action_t action)
88 smx_req_t req = xbt_fifo_shift(action->request_list);
90 switch (action->state) {
92 case SIMIX_SRC_TIMEOUT:
94 THROW0(timeout_error, 0, "Synchro's wait timeout");
95 } CATCH(req->issuer->running_ctx->exception) {
96 req->issuer->doexception = 1;
102 THROW0(host_error, 0, "Host failed");
103 } CATCH(req->issuer->running_ctx->exception) {
104 req->issuer->doexception = 1;
113 SIMIX_synchro_stop_waiting(req->issuer, req);
114 SIMIX_synchro_destroy(action);
115 SIMIX_request_answer(req);
117 /*********************************** Mutex ************************************/
120 * \brief Initialize a mutex.
122 * Allocs and creates the data for the mutex.
125 smx_mutex_t SIMIX_mutex_init(void)
127 s_smx_process_t p; /* useful to initialize sleeping swag */
129 smx_mutex_t mutex = xbt_new0(s_smx_mutex_t, 1);
131 mutex->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
136 * \brief Handle mutex lock request
137 * \param req The request
139 void SIMIX_pre_mutex_lock(smx_req_t req)
141 /* FIXME: check where to validate the arguments */
142 smx_action_t sync_act = NULL;
143 smx_mutex_t mutex = req->mutex_lock.mutex;
144 smx_process_t process = req->issuer;
147 /* FIXME: check if the host is active ? */
148 /* Somebody using the mutex, use a synchro action to get host failures */
149 sync_act = SIMIX_synchro_wait(process->smx_host, -1);
150 xbt_fifo_push(sync_act->request_list, req);
151 req->issuer->waiting_action = sync_act;
152 xbt_swag_insert(req->issuer, mutex->sleeping);
156 mutex->owner = req->issuer;
157 SIMIX_request_answer(req);
162 * \brief Tries to lock a mutex.
164 * Tries to lock a mutex, return 1 if the mutex is unlocked, else 0.
165 * This function does not block and wait for the mutex to be unlocked.
166 * \param mutex The mutex
167 * \param issuer The process that tries to acquire the mutex
168 * \return 1 - mutex free, 0 - mutex used
170 int SIMIX_mutex_trylock(smx_mutex_t mutex, smx_process_t issuer)
176 mutex->owner = issuer;
181 * \brief Unlocks a mutex.
183 * Unlocks the mutex and gives it to a process waiting for it.
184 * If the unlocker is not the owner of the mutex nothing happens.
185 * If there are no process waiting, it sets the mutex as free.
186 * \param mutex The mutex
187 * \param issuer The process trying to unlock the mutex
189 void SIMIX_mutex_unlock(smx_mutex_t mutex, smx_process_t issuer)
191 smx_process_t p; /*process to wake up */
193 /* If the mutex is not owned by the issuer do nothing */
194 if (issuer != mutex->owner)
197 if (xbt_swag_size(mutex->sleeping) > 0) {
198 p = xbt_swag_extract(mutex->sleeping);
199 SIMIX_synchro_destroy(p->waiting_action);
200 p->waiting_action = NULL;
202 SIMIX_request_answer(&p->request);
204 /* nobody to wake up */
211 * \brief Destroys a mutex.
213 * Destroys and frees the mutex's memory.
214 * \param mutex A mutex
216 void SIMIX_mutex_destroy(smx_mutex_t mutex)
219 xbt_swag_free(mutex->sleeping);
224 /********************************* Condition **********************************/
227 * \brief Initialize a condition.
229 * Allocates and creates the data for the condition.
230 * It have to be called before the use of the condition.
231 * \return A condition
233 smx_cond_t SIMIX_cond_init()
236 smx_cond_t cond = xbt_new0(s_smx_cond_t, 1);
237 cond->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
243 * \brief Handle condition waiting requests without timeouts
246 void SIMIX_pre_cond_wait(smx_req_t req)
248 smx_process_t issuer = req->issuer;
249 smx_cond_t cond = req->cond_wait.cond;
250 smx_mutex_t mutex = req->cond_wait.mutex;
252 _SIMIX_cond_wait(cond, mutex, -1, issuer, req);
256 * \brief Handle condition waiting requests with timeouts
259 void SIMIX_pre_cond_wait_timeout(smx_req_t req)
261 smx_process_t issuer = req->issuer;
262 smx_cond_t cond = req->cond_wait_timeout.cond;
263 smx_mutex_t mutex = req->cond_wait_timeout.mutex;
264 double timeout = req->cond_wait_timeout.timeout;
266 _SIMIX_cond_wait(cond, mutex, timeout, issuer, req);
270 static void _SIMIX_cond_wait(smx_cond_t cond, smx_mutex_t mutex, double timeout,
271 smx_process_t issuer, smx_req_t req)
273 smx_action_t sync_act = NULL;
275 DEBUG1("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 sync_act = SIMIX_synchro_wait(issuer->smx_host, timeout);
285 xbt_fifo_unshift(sync_act->request_list, req);
286 issuer->waiting_action = sync_act;
287 xbt_swag_insert(req->issuer, cond->sleeping);
291 * \brief Signalizes a condition.
293 * Signalizes a condition and wakes up a sleeping process.
294 * If there are no process sleeping, no action is done.
295 * \param cond A condition
297 void SIMIX_cond_signal(smx_cond_t cond)
299 smx_process_t proc = NULL;
300 smx_mutex_t mutex = NULL;
301 smx_req_t req = NULL;
303 DEBUG1("Signal condition %p", cond);
305 /* If there are processes waiting for the condition choose one and try
306 to make it acquire the mutex */
307 if ((proc = xbt_swag_extract(cond->sleeping))) {
309 /* Destroy waiter's synchro action */
310 SIMIX_synchro_destroy(proc->waiting_action);
311 proc->waiting_action = NULL;
313 /* Now transform the cond wait request into a mutex lock one */
314 req = &proc->request;
315 if(req->call == REQ_COND_WAIT)
316 mutex = req->cond_wait.mutex;
318 mutex = req->cond_wait_timeout.mutex;
320 req->call = REQ_MUTEX_LOCK;
321 req->mutex_lock.mutex = mutex;
323 SIMIX_pre_mutex_lock(req);
328 * \brief Broadcasts a condition.
330 * Signal ALL processes waiting on a condition.
331 * If there are no process waiting, no action is done.
332 * \param cond A condition
334 void SIMIX_cond_broadcast(smx_cond_t cond)
336 DEBUG1("Broadcast condition %p", cond);
338 /* Signal the condition until nobody is waiting on it */
339 while (xbt_swag_size(cond->sleeping)) {
340 SIMIX_cond_signal(cond);
345 * \brief Destroys a contidion.
347 * Destroys and frees the condition's memory.
348 * \param cond A condition
350 void SIMIX_cond_destroy(smx_cond_t cond)
352 DEBUG1("Destroy condition %p", cond);
355 xbt_assert0(xbt_swag_size(cond->sleeping) == 0,
356 "Cannot destroy conditional since someone is still using it");
358 xbt_swag_free(cond->sleeping);
363 /******************************** Semaphores **********************************/
364 #define SMX_SEM_NOLIMIT 99999
365 /** @brief Initialize a semaphore */
366 smx_sem_t SIMIX_sem_init(unsigned int value)
370 smx_sem_t sem = xbt_new0(s_smx_sem_t, 1);
371 sem->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
376 /** @brief Destroys a semaphore */
377 void SIMIX_sem_destroy(smx_sem_t sem)
379 DEBUG1("Destroy semaphore %p", sem);
381 xbt_assert0(xbt_swag_size(sem->sleeping) == 0,
382 "Cannot destroy semaphore since someone is still using it");
383 xbt_swag_free(sem->sleeping);
388 /** @brief release the semaphore
390 * Unlock a process waiting on the semaphore.
391 * If no one was blocked, the semaphore capacity is increased by 1.
393 void SIMIX_sem_release(smx_sem_t sem)
397 DEBUG1("Sem release semaphore %p", sem);
398 if ((proc = xbt_swag_extract(sem->sleeping))) {
399 proc = xbt_swag_extract(sem->sleeping);
400 SIMIX_synchro_destroy(proc->waiting_action);
401 proc->waiting_action = NULL;
402 SIMIX_request_answer(&proc->request);
403 } else if (sem->value < SMX_SEM_NOLIMIT) {
408 /** @brief Returns true if acquiring this semaphore would block */
409 XBT_INLINE int SIMIX_sem_would_block(smx_sem_t sem)
411 return (sem->value <= 0);
414 /** @brief Returns the current capacity of the semaphore */
415 int SIMIX_sem_get_capacity(smx_sem_t sem)
420 static void _SIMIX_sem_wait(smx_sem_t sem, double timeout, smx_process_t issuer,
423 smx_action_t sync_act = NULL;
425 DEBUG2("Wait semaphore %p (timeout:%f)", sem, timeout);
426 if (sem->value <= 0) {
427 sync_act = SIMIX_synchro_wait(issuer->smx_host, timeout);
428 xbt_fifo_unshift(sync_act->request_list, req);
429 issuer->waiting_action = sync_act;
430 xbt_swag_insert(issuer, sem->sleeping);
433 SIMIX_request_answer(req);
438 * \brief Handle sem acquire requests without timeouts
440 void SIMIX_pre_sem_acquire(smx_req_t req)
442 _SIMIX_sem_wait(req->sem_acquire.sem, -1, req->issuer, req);
446 * \brief Handle sem acquire requests with timeouts
448 void SIMIX_pre_sem_acquire_timeout(smx_req_t req)
450 _SIMIX_sem_wait(req->sem_acquire_timeout.sem,
451 req->sem_acquire_timeout.timeout, req->issuer, req);