From: Gabriel Corona Date: Tue, 14 Jun 2016 08:47:38 +0000 (+0200) Subject: [simix] Make Mutex a C++ class (kind-of) X-Git-Tag: v3_14~987^2~6^2~9 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/def75190166f07cc72917cb15535b660aa976e7f [simix] Make Mutex a C++ class (kind-of) --- diff --git a/include/simgrid/simix.h b/include/simgrid/simix.h index bc07ffe3ee..4c0fb6c743 100644 --- a/include/simgrid/simix.h +++ b/include/simgrid/simix.h @@ -32,24 +32,26 @@ namespace simix { class Process; class Context; class ContextFactory; - + class Mutex; } } typedef simgrid::simix::Context *smx_context_t; typedef simgrid::simix::Process *smx_process_t; +/** + * \ingroup simix_synchro_management + */ +typedef simgrid::simix::Mutex *smx_mutex_t; + #else typedef struct s_smx_context *smx_context_t; typedef struct s_smx_process *smx_process_t; +typedef struct s_smx_mutex *smx_mutex_t; #endif - - -SG_BEGIN_DECL() - /**************************** Scalar Values **********************************/ typedef union u_smx_scalar u_smx_scalar_t; @@ -83,10 +85,7 @@ typedef enum { /** @} */ /* ******************************** Synchro ************************************ */ -/** - * \ingroup simix_synchro_management - */ -typedef struct s_smx_mutex *smx_mutex_t; + /** * \ingroup simix_synchro_management */ @@ -127,6 +126,8 @@ extern int smx_context_stack_size_was_set; extern int smx_context_guard_size; extern int smx_context_guard_size_was_set; +SG_BEGIN_DECL() + XBT_PUBLIC(xbt_dynar_t) SIMIX_process_get_runnable(void); XBT_PUBLIC(smx_process_t) SIMIX_process_from_PID(int PID); XBT_PUBLIC(xbt_dynar_t) SIMIX_processes_as_dynar(void); diff --git a/include/xbt/synchro_core.h b/include/xbt/synchro_core.h index 50684edca2..b859f1765f 100644 --- a/include/xbt/synchro_core.h +++ b/include/xbt/synchro_core.h @@ -13,6 +13,8 @@ #ifndef _XBT_THREAD_H #define _XBT_THREAD_H +#include + #include "xbt/misc.h" /* SG_BEGIN_DECL */ #include "xbt/function_types.h" diff --git a/src/mc/mc_base.cpp b/src/mc/mc_base.cpp index df068fa76f..7c350f7e7f 100644 --- a/src/mc/mc_base.cpp +++ b/src/mc/mc_base.cpp @@ -162,10 +162,10 @@ bool request_is_enabled(smx_simcall_t req) case SIMCALL_MUTEX_LOCK: { smx_mutex_t mutex = simcall_mutex_lock__get__mutex(req); #if HAVE_MC - s_smx_mutex_t temp_mutex; + simgrid::mc::Remote temp_mutex; if (mc_model_checker != nullptr) { - mc_model_checker->process().read(&temp_mutex, remote(mutex)); - mutex = &temp_mutex; + mc_model_checker->process().read(temp_mutex.getBuffer(), remote(mutex)); + mutex = temp_mutex.getBuffer(); } #endif diff --git a/src/mc/mc_request.cpp b/src/mc/mc_request.cpp index d89c89d767..cf8a8bfae4 100644 --- a/src/mc/mc_request.cpp +++ b/src/mc/mc_request.cpp @@ -387,8 +387,8 @@ std::string simgrid::mc::request_to_string(smx_simcall_t req, int value, simgrid else type = "Mutex TRYLOCK"; - s_smx_mutex_t mutex; - mc_model_checker->process().read_bytes(&mutex, sizeof(mutex), + simgrid::mc::Remote mutex; + mc_model_checker->process().read_bytes(mutex.getBuffer(), sizeof(mutex), remote( req->call == SIMCALL_MUTEX_LOCK ? simcall_mutex_lock__get__mutex(req) @@ -396,12 +396,12 @@ std::string simgrid::mc::request_to_string(smx_simcall_t req, int value, simgrid )); s_xbt_swag_t mutex_sleeping; mc_model_checker->process().read_bytes(&mutex_sleeping, sizeof(mutex_sleeping), - remote(mutex.sleeping)); + remote(mutex.getBuffer()->sleeping)); args = bprintf("locked = %d, owner = %d, sleeping = %d", - mutex.locked, - mutex.owner != nullptr ? (int) mc_model_checker->process().resolveProcess( - simgrid::mc::remote(mutex.owner))->pid : -1, + mutex.getBuffer()->locked, + mutex.getBuffer()->owner != nullptr ? (int) mc_model_checker->process().resolveProcess( + simgrid::mc::remote(mutex.getBuffer()->owner))->pid : -1, mutex_sleeping.count); break; } diff --git a/src/simix/smx_synchro.cpp b/src/simix/smx_synchro.cpp index 2797cb59cd..e7860fff32 100644 --- a/src/simix/smx_synchro.cpp +++ b/src/simix/smx_synchro.cpp @@ -1,3 +1,4 @@ + /* Copyright (c) 2007-2015. The SimGrid Team. * All rights reserved. */ @@ -93,130 +94,124 @@ void SIMIX_synchro_finish(smx_synchro_t synchro) } /*********************************** 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) +namespace simgrid { +namespace simix { + +Mutex::Mutex() { - XBT_IN("()"); - simgrid::simix::Process p; /* useful to initialize sleeping swag */ + XBT_IN("(%p)", this); + // Useful to initialize sleeping swag: + simgrid::simix::Process p; + this->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup)); + XBT_OUT(); +} - 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)); +Mutex::~Mutex() +{ + XBT_IN("(%p)", this); + xbt_swag_free(this->sleeping); 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) +void Mutex::lock(smx_process_t issuer) { - XBT_IN("(%p)",simcall); + XBT_IN("(%p; %p)", this, issuer); /* FIXME: check where to validate the arguments */ smx_synchro_t synchro = nullptr; - smx_process_t process = simcall->issuer; - if (mutex->locked) { + if (this->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); - synchro->simcalls.push_back(simcall); - simcall->issuer->waiting_synchro = synchro; - xbt_swag_insert(simcall->issuer, mutex->sleeping); + synchro = SIMIX_synchro_wait(issuer->host, -1); + synchro->simcalls.push_back(&issuer->simcall); + issuer->waiting_synchro = synchro; + xbt_swag_insert(issuer, this->sleeping); } else { /* mutex free */ - mutex->locked = 1; - mutex->owner = simcall->issuer; - SIMIX_simcall_answer(simcall); + this->locked = true; + this->owner = issuer; + SIMIX_simcall_answer(&issuer->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 the mutex for a process * - * 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 + * \param issuer the process that tries to acquire the mutex + * \return whether we managed to lock the mutex */ -int SIMIX_mutex_trylock(smx_mutex_t mutex, smx_process_t issuer) +bool Mutex::try_lock(smx_process_t issuer) { - XBT_IN("(%p, %p)",mutex,issuer); - if (mutex->locked){ + XBT_IN("(%p, %p)", this, issuer); + if (this->locked) { XBT_OUT(); - return 0; + return false; } - mutex->locked = 1; - mutex->owner = issuer; + this->locked = true; + this->owner = issuer; XBT_OUT(); - return 1; + return true; } -void simcall_HANDLER_mutex_unlock(smx_simcall_t simcall, smx_mutex_t mutex){ - SIMIX_mutex_unlock(mutex, simcall->issuer); -} -/** - * \brief Unlocks a mutex. +/** Unlock a mutex for a process * * 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) +void Mutex::unlock(smx_process_t issuer) { - XBT_IN("(%p, %p)",mutex,issuer); + XBT_IN("(%p, %p)", this, issuer); /* If the mutex is not owned by the issuer, that's not good */ - if (issuer != mutex->owner) + if (issuer != this->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)); + SIMIX_process_get_name(this->owner),SIMIX_process_get_PID(this->owner)); - if (xbt_swag_size(mutex->sleeping) > 0) { + if (xbt_swag_size(this->sleeping) > 0) { /*process to wake up */ - smx_process_t p = (smx_process_t) xbt_swag_extract(mutex->sleeping); + smx_process_t p = (smx_process_t) xbt_swag_extract(this->sleeping); delete p->waiting_synchro; p->waiting_synchro = nullptr; - mutex->owner = p; + this->owner = p; SIMIX_simcall_answer(&p->simcall); } else { /* nobody to wake up */ - mutex->locked = 0; - mutex->owner = nullptr; + this->locked = false; + this->owner = nullptr; } 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(); + delete mutex; +} + +smx_mutex_t simcall_HANDLER_mutex_init(smx_simcall_t simcall) +{ + return new simgrid::simix::Mutex(); +} + +// Simcall handlers: + +void simcall_HANDLER_mutex_lock(smx_simcall_t simcall, smx_mutex_t mutex) +{ + mutex->lock(simcall->issuer); +} + +int simcall_HANDLER_mutex_trylock(smx_simcall_t simcall, smx_mutex_t mutex) +{ + return mutex->try_lock(simcall->issuer); +} + +void simcall_HANDLER_mutex_unlock(smx_simcall_t simcall, smx_mutex_t mutex) +{ + mutex->unlock(simcall->issuer); } /********************************* Condition **********************************/ @@ -279,7 +274,7 @@ static void _SIMIX_cond_wait(smx_cond_t cond, smx_mutex_t mutex, double timeout, /* FIXME: what happens if the issuer is not the owner of the mutex? */ if (mutex != nullptr) { cond->mutex = mutex; - SIMIX_mutex_unlock(mutex, issuer); + mutex->unlock(issuer); } synchro = SIMIX_synchro_wait(issuer->host, timeout); diff --git a/src/simix/smx_synchro_private.h b/src/simix/smx_synchro_private.h index c8ea1f64f4..4b45f95ba5 100644 --- a/src/simix/smx_synchro_private.h +++ b/src/simix/smx_synchro_private.h @@ -12,11 +12,28 @@ #include "xbt/xbt_os_thread.h" #include "src/simix/popping_private.h" -typedef struct s_smx_mutex { - unsigned int locked; - smx_process_t owner; - xbt_swag_t sleeping; /* list of sleeping process */ -} s_smx_mutex_t; +namespace simgrid { +namespace simix { + +class XBT_PUBLIC() Mutex { +public: + Mutex(); + ~Mutex(); + Mutex(Mutex const&) = delete; + Mutex& operator=(Mutex const&) = delete; + + void lock(smx_process_t issuer); + bool try_lock(smx_process_t issuer); + void unlock(smx_process_t issuer); + + bool locked = false; + smx_process_t owner = nullptr; + // List of sleeping processes: + xbt_swag_t sleeping = nullptr; +}; + +} +} typedef struct s_smx_cond { smx_mutex_t mutex; @@ -33,10 +50,6 @@ XBT_PRIVATE void SIMIX_synchro_stop_waiting(smx_process_t process, smx_simcall_t XBT_PRIVATE void SIMIX_synchro_destroy(smx_synchro_t synchro); XBT_PRIVATE void SIMIX_synchro_finish(smx_synchro_t synchro); -XBT_PRIVATE smx_mutex_t SIMIX_mutex_init(void); -XBT_PRIVATE int SIMIX_mutex_trylock(smx_mutex_t mutex, smx_process_t issuer); -XBT_PRIVATE void SIMIX_mutex_unlock(smx_mutex_t mutex, smx_process_t issuer); - XBT_PRIVATE smx_cond_t SIMIX_cond_init(void); XBT_PRIVATE void SIMIX_cond_broadcast(smx_cond_t cond); XBT_PRIVATE void SIMIX_cond_signal(smx_cond_t cond);