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 action->synchro.sleep->model_type->action_unref(action->synchro.sleep);
85 SIMIX_synchro_finish(action);
88 static void SIMIX_synchro_finish(smx_action_t action)
90 smx_req_t req = xbt_fifo_shift(action->request_list);
92 switch (action->state) {
94 case SIMIX_SRC_TIMEOUT:
96 THROW0(timeout_error, 0, "Synchro's wait timeout");
97 } CATCH(req->issuer->running_ctx->exception) {
98 req->issuer->doexception = 1;
104 THROW0(host_error, 0, "Host failed");
105 } CATCH(req->issuer->running_ctx->exception) {
106 req->issuer->doexception = 1;
115 SIMIX_synchro_stop_waiting(req->issuer, req);
116 SIMIX_synchro_destroy(action);
117 SIMIX_request_answer(req);
119 /*********************************** Mutex ************************************/
122 * \brief Initialize a mutex.
124 * Allocs and creates the data for the mutex.
127 smx_mutex_t SIMIX_mutex_init(void)
129 s_smx_process_t p; /* useful to initialize sleeping swag */
131 smx_mutex_t mutex = xbt_new0(s_smx_mutex_t, 1);
133 mutex->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
138 * \brief Handle mutex lock request
139 * \param req The request
141 void SIMIX_pre_mutex_lock(smx_req_t req)
143 /* FIXME: check where to validate the arguments */
144 smx_action_t sync_act = NULL;
145 smx_mutex_t mutex = req->mutex_lock.mutex;
146 smx_process_t process = req->issuer;
149 /* FIXME: check if the host is active ? */
150 /* Somebody using the mutex, use a synchro action to get host failures */
151 sync_act = SIMIX_synchro_wait(process->smx_host, -1);
152 xbt_fifo_push(sync_act->request_list, req);
153 req->issuer->waiting_action = sync_act;
154 xbt_swag_insert(req->issuer, mutex->sleeping);
158 mutex->owner = req->issuer;
159 SIMIX_request_answer(req);
164 * \brief Tries to lock a mutex.
166 * Tries to lock a mutex, return 1 if the mutex is unlocked, else 0.
167 * This function does not block and wait for the mutex to be unlocked.
168 * \param mutex The mutex
169 * \param issuer The process that tries to acquire the mutex
170 * \return 1 - mutex free, 0 - mutex used
172 int SIMIX_mutex_trylock(smx_mutex_t mutex, smx_process_t issuer)
178 mutex->owner = issuer;
183 * \brief Unlocks a mutex.
185 * Unlocks the mutex and gives it to a process waiting for it.
186 * If the unlocker is not the owner of the mutex nothing happens.
187 * If there are no process waiting, it sets the mutex as free.
188 * \param mutex The mutex
189 * \param issuer The process trying to unlock the mutex
191 void SIMIX_mutex_unlock(smx_mutex_t mutex, smx_process_t issuer)
193 smx_process_t p; /*process to wake up */
195 /* If the mutex is not owned by the issuer do nothing */
196 if (issuer != mutex->owner)
199 if (xbt_swag_size(mutex->sleeping) > 0) {
200 p = xbt_swag_extract(mutex->sleeping);
201 SIMIX_synchro_destroy(p->waiting_action);
202 p->waiting_action = NULL;
204 SIMIX_request_answer(&p->request);
206 /* nobody to wake up */
213 * \brief Destroys a mutex.
215 * Destroys and frees the mutex's memory.
216 * \param mutex A mutex
218 void SIMIX_mutex_destroy(smx_mutex_t mutex)
221 xbt_swag_free(mutex->sleeping);
226 /********************************* Condition **********************************/
229 * \brief Initialize a condition.
231 * Allocates and creates the data for the condition.
232 * It have to be called before the use of the condition.
233 * \return A condition
235 smx_cond_t SIMIX_cond_init()
238 smx_cond_t cond = xbt_new0(s_smx_cond_t, 1);
239 cond->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
245 * \brief Handle condition waiting requests without timeouts
248 void SIMIX_pre_cond_wait(smx_req_t req)
250 smx_process_t issuer = req->issuer;
251 smx_cond_t cond = req->cond_wait.cond;
252 smx_mutex_t mutex = req->cond_wait.mutex;
254 _SIMIX_cond_wait(cond, mutex, -1, issuer, req);
258 * \brief Handle condition waiting requests with timeouts
261 void SIMIX_pre_cond_wait_timeout(smx_req_t req)
263 smx_process_t issuer = req->issuer;
264 smx_cond_t cond = req->cond_wait_timeout.cond;
265 smx_mutex_t mutex = req->cond_wait_timeout.mutex;
266 double timeout = req->cond_wait_timeout.timeout;
268 _SIMIX_cond_wait(cond, mutex, timeout, issuer, req);
272 static void _SIMIX_cond_wait(smx_cond_t cond, smx_mutex_t mutex, double timeout,
273 smx_process_t issuer, smx_req_t req)
275 smx_action_t sync_act = NULL;
277 DEBUG1("Wait condition %p", cond);
279 /* If there is a mutex unlock it */
280 /* FIXME: what happens if the issuer is not the owner of the mutex? */
283 SIMIX_mutex_unlock(mutex, issuer);
286 sync_act = SIMIX_synchro_wait(issuer->smx_host, timeout);
287 xbt_fifo_unshift(sync_act->request_list, req);
288 issuer->waiting_action = sync_act;
289 xbt_swag_insert(req->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)
301 smx_process_t proc = NULL;
302 smx_mutex_t mutex = NULL;
303 smx_req_t req = NULL;
305 DEBUG1("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 = xbt_swag_extract(cond->sleeping))) {
311 /* Destroy waiter's synchro action */
312 SIMIX_synchro_destroy(proc->waiting_action);
313 proc->waiting_action = NULL;
315 /* Now transform the cond wait request into a mutex lock one */
316 req = &proc->request;
317 if(req->call == REQ_COND_WAIT)
318 mutex = req->cond_wait.mutex;
320 mutex = req->cond_wait_timeout.mutex;
322 req->call = REQ_MUTEX_LOCK;
323 req->mutex_lock.mutex = mutex;
325 SIMIX_pre_mutex_lock(req);
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)
338 DEBUG1("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);
347 * \brief Destroys a contidion.
349 * Destroys and frees the condition's memory.
350 * \param cond A condition
352 void SIMIX_cond_destroy(smx_cond_t cond)
354 DEBUG1("Destroy condition %p", cond);
357 xbt_assert0(xbt_swag_size(cond->sleeping) == 0,
358 "Cannot destroy conditional since someone is still using it");
360 xbt_swag_free(cond->sleeping);
365 /******************************** Semaphores **********************************/
366 #define SMX_SEM_NOLIMIT 99999
367 /** @brief Initialize a semaphore */
368 smx_sem_t SIMIX_sem_init(unsigned int value)
372 smx_sem_t sem = xbt_new0(s_smx_sem_t, 1);
373 sem->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
378 /** @brief Destroys a semaphore */
379 void SIMIX_sem_destroy(smx_sem_t sem)
381 DEBUG1("Destroy semaphore %p", sem);
383 xbt_assert0(xbt_swag_size(sem->sleeping) == 0,
384 "Cannot destroy semaphore since someone is still using it");
385 xbt_swag_free(sem->sleeping);
390 /** @brief release the semaphore
392 * Unlock a process waiting on the semaphore.
393 * If no one was blocked, the semaphore capacity is increased by 1.
395 void SIMIX_sem_release(smx_sem_t sem)
399 DEBUG1("Sem release semaphore %p", sem);
400 if ((proc = xbt_swag_extract(sem->sleeping))) {
401 proc = xbt_swag_extract(sem->sleeping);
402 SIMIX_synchro_destroy(proc->waiting_action);
403 proc->waiting_action = NULL;
404 SIMIX_request_answer(&proc->request);
405 } else if (sem->value < SMX_SEM_NOLIMIT) {
410 /** @brief Returns true if acquiring this semaphore would block */
411 XBT_INLINE int SIMIX_sem_would_block(smx_sem_t sem)
413 return (sem->value <= 0);
416 /** @brief Returns the current capacity of the semaphore */
417 int SIMIX_sem_get_capacity(smx_sem_t sem)
422 static void _SIMIX_sem_wait(smx_sem_t sem, double timeout, smx_process_t issuer,
425 smx_action_t sync_act = NULL;
427 DEBUG2("Wait semaphore %p (timeout:%f)", sem, timeout);
428 if (sem->value <= 0) {
429 sync_act = SIMIX_synchro_wait(issuer->smx_host, timeout);
430 xbt_fifo_unshift(sync_act->request_list, req);
431 issuer->waiting_action = sync_act;
432 xbt_swag_insert(issuer, sem->sleeping);
435 SIMIX_request_answer(req);
440 * \brief Handle sem acquire requests without timeouts
442 void SIMIX_pre_sem_acquire(smx_req_t req)
444 _SIMIX_sem_wait(req->sem_acquire.sem, -1, req->issuer, req);
448 * \brief Handle sem acquire requests with timeouts
450 void SIMIX_pre_sem_acquire_timeout(smx_req_t req)
452 _SIMIX_sem_wait(req->sem_acquire_timeout.sem,
453 req->sem_acquire_timeout.timeout, req->issuer, req);