-/*********************************** Mutex ************************************/
-
-smx_mutex_t simcall_HANDLER_mutex_init(smx_simcall_t simcall){
- return SIMIX_mutex_init();
-}
-/**
- * \brief Initialize a mutex.
- *
- * Allocs and creates the data for the mutex.
- * \return A mutex
- */
-smx_mutex_t SIMIX_mutex_init(void)
-{
- XBT_IN("()");
- simgrid::simix::Process p; /* useful to initialize sleeping swag */
-
- smx_mutex_t mutex = xbt_new0(s_smx_mutex_t, 1);
- mutex->locked = 0;
- mutex->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
- XBT_OUT();
- return mutex;
-}
-
-/**
- * \brief Handles a mutex lock simcall.
- * \param simcall the simcall
- */
-void simcall_HANDLER_mutex_lock(smx_simcall_t simcall, smx_mutex_t mutex)
-{
- XBT_IN("(%p)",simcall);
- /* FIXME: check where to validate the arguments */
- smx_synchro_t synchro = NULL;
- smx_process_t process = simcall->issuer;
-
- if (mutex->locked) {
- /* FIXME: check if the host is active ? */
- /* Somebody using the mutex, use a synchronization to get host failures */
- synchro = SIMIX_synchro_wait(process->host, -1);
- xbt_fifo_push(synchro->simcalls, simcall);
- simcall->issuer->waiting_synchro = synchro;
- xbt_swag_insert(simcall->issuer, mutex->sleeping);
- } else {
- /* mutex free */
- mutex->locked = 1;
- mutex->owner = simcall->issuer;
- SIMIX_simcall_answer(simcall);
- }
- XBT_OUT();
-}
-
-int simcall_HANDLER_mutex_trylock(smx_simcall_t simcall, smx_mutex_t mutex){
- return SIMIX_mutex_trylock(mutex, simcall->issuer);
-}
-/**
- * \brief Tries to lock a mutex.
- *
- * Tries to lock a mutex, return 1 if the mutex is unlocked, else 0.
- * This function does not block and wait for the mutex to be unlocked.
- * \param mutex The mutex
- * \param issuer The process that tries to acquire the mutex
- * \return 1 - mutex free, 0 - mutex used
- */
-int SIMIX_mutex_trylock(smx_mutex_t mutex, smx_process_t issuer)
-{
- XBT_IN("(%p, %p)",mutex,issuer);
- if (mutex->locked){
- XBT_OUT();
- return 0;
- }
-
- mutex->locked = 1;
- mutex->owner = issuer;
- XBT_OUT();
- return 1;
-}
-
-void simcall_HANDLER_mutex_unlock(smx_simcall_t simcall, smx_mutex_t mutex){
- SIMIX_mutex_unlock(mutex, simcall->issuer);
-}
-/**
- * \brief Unlocks a mutex.
- *
- * Unlocks the mutex and gives it to a process waiting for it.
- * If the unlocker is not the owner of the mutex nothing happens.
- * If there are no process waiting, it sets the mutex as free.
- * \param mutex The mutex
- * \param issuer The process trying to unlock the mutex
- */
-void SIMIX_mutex_unlock(smx_mutex_t mutex, smx_process_t issuer)
-{
- XBT_IN("(%p, %p)",mutex,issuer);
-
- /* If the mutex is not owned by the issuer, that's not good */
- if (issuer != mutex->owner)
- THROWF(mismatch_error, 0, "Cannot release that mutex: it was locked by %s (pid:%d), not by you.",
- SIMIX_process_get_name(mutex->owner),SIMIX_process_get_PID(mutex->owner));
-
- if (xbt_swag_size(mutex->sleeping) > 0) {
- /*process to wake up */
- smx_process_t p = (smx_process_t) xbt_swag_extract(mutex->sleeping);
- delete p->waiting_synchro;
- p->waiting_synchro = NULL;
- mutex->owner = p;
- SIMIX_simcall_answer(&p->simcall);
- } else {
- /* nobody to wake up */
- mutex->locked = 0;
- mutex->owner = NULL;
- }
- XBT_OUT();
-}
-
-/**
- * \brief Destroys a mutex.
- *
- * Destroys and frees the mutex's memory.
- * \param mutex A mutex
- */
-void SIMIX_mutex_destroy(smx_mutex_t mutex)
-{
- XBT_IN("(%p)",mutex);
- if (mutex){
- xbt_swag_free(mutex->sleeping);
- xbt_free(mutex);
- }
- XBT_OUT();
-}