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_mallocator_get(simix_global->action_mallocator);
28 action->type = SIMIX_ACTION_SYNCHRO;
29 action->name = xbt_strdup("synchro");
30 action->synchro.sleep =
31 surf_workstation_model->extension.workstation.sleep(smx_host->host, timeout);
33 surf_workstation_model->action_data_set(action->synchro.sleep, action);
37 void SIMIX_synchro_stop_waiting(smx_process_t process, smx_req_t req)
42 xbt_swag_remove(process, req->mutex_lock.mutex->sleeping);
46 xbt_swag_remove(process, req->cond_wait.cond->sleeping);
49 case REQ_COND_WAIT_TIMEOUT:
50 xbt_swag_remove(process, req->cond_wait_timeout.cond->sleeping);
54 xbt_swag_remove(process, req->sem_acquire.sem->sleeping);
57 case REQ_SEM_ACQUIRE_TIMEOUT:
58 xbt_swag_remove(process, req->sem_acquire_timeout.sem->sleeping);
66 void SIMIX_synchro_destroy(smx_action_t action)
68 XBT_DEBUG("Destroying synchro %p", action);
69 action->synchro.sleep->model_type->action_unref(action->synchro.sleep);
70 xbt_free(action->name);
71 xbt_mallocator_release(simix_global->action_mallocator, action);
74 void SIMIX_post_synchro(smx_action_t action)
76 if (surf_workstation_model->action_state_get(action->synchro.sleep) == SURF_ACTION_FAILED)
77 action->state = SIMIX_FAILED;
78 else if(surf_workstation_model->action_state_get(action->synchro.sleep) == SURF_ACTION_DONE)
79 action->state = SIMIX_SRC_TIMEOUT;
81 SIMIX_synchro_finish(action);
84 static void SIMIX_synchro_finish(smx_action_t action)
86 smx_req_t req = xbt_fifo_shift(action->request_list);
88 switch (action->state) {
90 case SIMIX_SRC_TIMEOUT:
92 THROWF(timeout_error, 0, "Synchro's wait timeout");
93 } CATCH(req->issuer->running_ctx->exception) {
94 req->issuer->doexception = 1;
100 THROWF(host_error, 0, "Host failed");
101 } CATCH(req->issuer->running_ctx->exception) {
102 req->issuer->doexception = 1;
111 SIMIX_synchro_stop_waiting(req->issuer, req);
112 SIMIX_synchro_destroy(action);
113 SIMIX_request_answer(req);
115 /*********************************** Mutex ************************************/
118 * \brief Initialize a mutex.
120 * Allocs and creates the data for the mutex.
123 smx_mutex_t SIMIX_mutex_init(void)
125 s_smx_process_t p; /* useful to initialize sleeping swag */
127 smx_mutex_t mutex = xbt_new0(s_smx_mutex_t, 1);
129 mutex->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
134 * \brief Handle mutex lock request
135 * \param req The request
137 void SIMIX_pre_mutex_lock(smx_req_t req)
139 /* FIXME: check where to validate the arguments */
140 smx_action_t sync_act = NULL;
141 smx_mutex_t mutex = req->mutex_lock.mutex;
142 smx_process_t process = req->issuer;
145 /* FIXME: check if the host is active ? */
146 /* Somebody using the mutex, use a synchro action to get host failures */
147 sync_act = SIMIX_synchro_wait(process->smx_host, -1);
148 xbt_fifo_push(sync_act->request_list, req);
149 req->issuer->waiting_action = sync_act;
150 xbt_swag_insert(req->issuer, mutex->sleeping);
154 mutex->owner = req->issuer;
155 SIMIX_request_answer(req);
160 * \brief Tries to lock a mutex.
162 * Tries to lock a mutex, return 1 if the mutex is unlocked, else 0.
163 * This function does not block and wait for the mutex to be unlocked.
164 * \param mutex The mutex
165 * \param issuer The process that tries to acquire the mutex
166 * \return 1 - mutex free, 0 - mutex used
168 int SIMIX_mutex_trylock(smx_mutex_t mutex, smx_process_t issuer)
174 mutex->owner = issuer;
179 * \brief Unlocks a mutex.
181 * Unlocks the mutex and gives it to a process waiting for it.
182 * If the unlocker is not the owner of the mutex nothing happens.
183 * If there are no process waiting, it sets the mutex as free.
184 * \param mutex The mutex
185 * \param issuer The process trying to unlock the mutex
187 void SIMIX_mutex_unlock(smx_mutex_t mutex, smx_process_t issuer)
189 smx_process_t p; /*process to wake up */
191 /* If the mutex is not owned by the issuer do nothing */
192 if (issuer != mutex->owner)
195 if (xbt_swag_size(mutex->sleeping) > 0) {
196 p = xbt_swag_extract(mutex->sleeping);
197 SIMIX_synchro_destroy(p->waiting_action);
198 p->waiting_action = NULL;
200 SIMIX_request_answer(&p->request);
202 /* nobody to wake up */
209 * \brief Destroys a mutex.
211 * Destroys and frees the mutex's memory.
212 * \param mutex A mutex
214 void SIMIX_mutex_destroy(smx_mutex_t mutex)
217 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()
234 smx_cond_t cond = xbt_new0(s_smx_cond_t, 1);
235 cond->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
241 * \brief Handle condition waiting requests without timeouts
244 void SIMIX_pre_cond_wait(smx_req_t req)
246 smx_process_t issuer = req->issuer;
247 smx_cond_t cond = req->cond_wait.cond;
248 smx_mutex_t mutex = req->cond_wait.mutex;
250 _SIMIX_cond_wait(cond, mutex, -1, issuer, req);
254 * \brief Handle condition waiting requests with timeouts
257 void SIMIX_pre_cond_wait_timeout(smx_req_t req)
259 smx_process_t issuer = req->issuer;
260 smx_cond_t cond = req->cond_wait_timeout.cond;
261 smx_mutex_t mutex = req->cond_wait_timeout.mutex;
262 double timeout = req->cond_wait_timeout.timeout;
264 _SIMIX_cond_wait(cond, mutex, timeout, issuer, req);
268 static void _SIMIX_cond_wait(smx_cond_t cond, smx_mutex_t mutex, double timeout,
269 smx_process_t issuer, smx_req_t req)
271 smx_action_t sync_act = NULL;
273 XBT_DEBUG("Wait condition %p", cond);
275 /* If there is a mutex unlock it */
276 /* FIXME: what happens if the issuer is not the owner of the mutex? */
279 SIMIX_mutex_unlock(mutex, issuer);
282 sync_act = SIMIX_synchro_wait(issuer->smx_host, timeout);
283 xbt_fifo_unshift(sync_act->request_list, req);
284 issuer->waiting_action = sync_act;
285 xbt_swag_insert(req->issuer, cond->sleeping);
289 * \brief Signalizes a condition.
291 * Signalizes a condition and wakes up a sleeping process.
292 * If there are no process sleeping, no action is done.
293 * \param cond A condition
295 void SIMIX_cond_signal(smx_cond_t cond)
297 smx_process_t proc = NULL;
298 smx_mutex_t mutex = NULL;
299 smx_req_t req = NULL;
301 XBT_DEBUG("Signal condition %p", cond);
303 /* If there are processes waiting for the condition choose one and try
304 to make it acquire the mutex */
305 if ((proc = xbt_swag_extract(cond->sleeping))) {
307 /* Destroy waiter's synchro action */
308 SIMIX_synchro_destroy(proc->waiting_action);
309 proc->waiting_action = NULL;
311 /* Now transform the cond wait request into a mutex lock one */
312 req = &proc->request;
313 if(req->call == REQ_COND_WAIT)
314 mutex = req->cond_wait.mutex;
316 mutex = req->cond_wait_timeout.mutex;
318 req->call = REQ_MUTEX_LOCK;
319 req->mutex_lock.mutex = mutex;
321 SIMIX_pre_mutex_lock(req);
326 * \brief Broadcasts a condition.
328 * Signal ALL processes waiting on a condition.
329 * If there are no process waiting, no action is done.
330 * \param cond A condition
332 void SIMIX_cond_broadcast(smx_cond_t cond)
334 XBT_DEBUG("Broadcast condition %p", cond);
336 /* Signal the condition until nobody is waiting on it */
337 while (xbt_swag_size(cond->sleeping)) {
338 SIMIX_cond_signal(cond);
343 * \brief Destroys a contidion.
345 * Destroys and frees the condition's memory.
346 * \param cond A condition
348 void SIMIX_cond_destroy(smx_cond_t cond)
350 XBT_DEBUG("Destroy condition %p", cond);
353 xbt_assert(xbt_swag_size(cond->sleeping) == 0,
354 "Cannot destroy conditional since someone is still using it");
356 xbt_swag_free(cond->sleeping);
361 /******************************** Semaphores **********************************/
362 #define SMX_SEM_NOLIMIT 99999
363 /** @brief Initialize a semaphore */
364 smx_sem_t SIMIX_sem_init(unsigned int value)
368 smx_sem_t sem = xbt_new0(s_smx_sem_t, 1);
369 sem->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
374 /** @brief Destroys a semaphore */
375 void SIMIX_sem_destroy(smx_sem_t sem)
377 XBT_DEBUG("Destroy semaphore %p", sem);
379 xbt_assert(xbt_swag_size(sem->sleeping) == 0,
380 "Cannot destroy semaphore since someone is still using it");
381 xbt_swag_free(sem->sleeping);
386 /** @brief release the semaphore
388 * Unlock a process waiting on the semaphore.
389 * If no one was blocked, the semaphore capacity is increased by 1.
391 void SIMIX_sem_release(smx_sem_t sem)
395 XBT_DEBUG("Sem release semaphore %p", sem);
396 if ((proc = xbt_swag_extract(sem->sleeping))) {
397 proc = xbt_swag_extract(sem->sleeping);
398 SIMIX_synchro_destroy(proc->waiting_action);
399 proc->waiting_action = NULL;
400 SIMIX_request_answer(&proc->request);
401 } else if (sem->value < SMX_SEM_NOLIMIT) {
406 /** @brief Returns true if acquiring this semaphore would block */
407 XBT_INLINE int SIMIX_sem_would_block(smx_sem_t sem)
409 return (sem->value <= 0);
412 /** @brief Returns the current capacity of the semaphore */
413 int SIMIX_sem_get_capacity(smx_sem_t sem)
418 static void _SIMIX_sem_wait(smx_sem_t sem, double timeout, smx_process_t issuer,
421 smx_action_t sync_act = NULL;
423 XBT_DEBUG("Wait semaphore %p (timeout:%f)", sem, timeout);
424 if (sem->value <= 0) {
425 sync_act = SIMIX_synchro_wait(issuer->smx_host, timeout);
426 xbt_fifo_unshift(sync_act->request_list, req);
427 issuer->waiting_action = sync_act;
428 xbt_swag_insert(issuer, sem->sleeping);
431 SIMIX_request_answer(req);
436 * \brief Handle sem acquire requests without timeouts
438 void SIMIX_pre_sem_acquire(smx_req_t req)
440 _SIMIX_sem_wait(req->sem_acquire.sem, -1, req->issuer, req);
444 * \brief Handle sem acquire requests with timeouts
446 void SIMIX_pre_sem_acquire_timeout(smx_req_t req)
448 _SIMIX_sem_wait(req->sem_acquire_timeout.sem,
449 req->sem_acquire_timeout.timeout, req->issuer, req);