Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[simix] Make Mutex a C++ class (kind-of)
authorGabriel Corona <gabriel.corona@loria.fr>
Tue, 14 Jun 2016 08:47:38 +0000 (10:47 +0200)
committerGabriel Corona <gabriel.corona@loria.fr>
Tue, 14 Jun 2016 13:20:21 +0000 (15:20 +0200)
include/simgrid/simix.h
include/xbt/synchro_core.h
src/mc/mc_base.cpp
src/mc/mc_request.cpp
src/simix/smx_synchro.cpp
src/simix/smx_synchro_private.h

index bc07ffe..4c0fb6c 100644 (file)
@@ -32,24 +32,26 @@ namespace simix {
   class Process;
   class Context;
   class ContextFactory;
   class Process;
   class Context;
   class ContextFactory;
-
+  class Mutex;
 }
 }
 
 typedef simgrid::simix::Context *smx_context_t;
 typedef simgrid::simix::Process *smx_process_t;
 
 }
 }
 
 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;
 #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
 
 
 #endif
 
-
-
-SG_BEGIN_DECL()
-
 /**************************** Scalar Values **********************************/
 
 typedef union u_smx_scalar u_smx_scalar_t;
 /**************************** Scalar Values **********************************/
 
 typedef union u_smx_scalar u_smx_scalar_t;
@@ -83,10 +85,7 @@ typedef enum {
 /** @} */
 
 /* ******************************** Synchro ************************************ */
 /** @} */
 
 /* ******************************** Synchro ************************************ */
-/**
- * \ingroup simix_synchro_management
- */
-typedef struct s_smx_mutex *smx_mutex_t;
+
 /**
  * \ingroup simix_synchro_management
  */
 /**
  * \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;
 
 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);
 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);
index 50684ed..b859f17 100644 (file)
@@ -13,6 +13,8 @@
 #ifndef _XBT_THREAD_H
 #define _XBT_THREAD_H
 
 #ifndef _XBT_THREAD_H
 #define _XBT_THREAD_H
 
+#include <simgrid/simix.h>
+
 #include "xbt/misc.h"           /* SG_BEGIN_DECL */
 #include "xbt/function_types.h"
 
 #include "xbt/misc.h"           /* SG_BEGIN_DECL */
 #include "xbt/function_types.h"
 
index df068fa..7c350f7 100644 (file)
@@ -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
   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<simgrid::simix::Mutex> temp_mutex;
     if (mc_model_checker != nullptr) {
     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
 
     }
 #endif
 
index d89c89d..cf8a8bf 100644 (file)
@@ -387,8 +387,8 @@ std::string simgrid::mc::request_to_string(smx_simcall_t req, int value, simgrid
     else
       type = "Mutex TRYLOCK";
 
     else
       type = "Mutex TRYLOCK";
 
-    s_smx_mutex_t mutex;
-    mc_model_checker->process().read_bytes(&mutex, sizeof(mutex),
+    simgrid::mc::Remote<simgrid::simix::Mutex> mutex;
+    mc_model_checker->process().read_bytes(mutex.getBuffer(), sizeof(mutex),
       remote(
         req->call == SIMCALL_MUTEX_LOCK
         ? simcall_mutex_lock__get__mutex(req)
       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),
       ));
     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",
 
     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;
   }
       mutex_sleeping.count);
     break;
   }
index 2797cb5..e7860ff 100644 (file)
@@ -1,3 +1,4 @@
+
 /* Copyright (c) 2007-2015. The SimGrid Team.
  * All rights reserved.                                                     */
 
 /* Copyright (c) 2007-2015. The SimGrid Team.
  * All rights reserved.                                                     */
 
@@ -93,130 +94,124 @@ void SIMIX_synchro_finish(smx_synchro_t synchro)
 }
 /*********************************** Mutex ************************************/
 
 }
 /*********************************** 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();
   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;
   /* 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 */
     /* 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 */
   } 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();
 }
 
   }
   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();
     XBT_OUT();
-    return 0;
+    return false;
   }
 
   }
 
-  mutex->locked = 1;
-  mutex->owner = issuer;
+  this->locked = true;
+  this->owner = issuer;
   XBT_OUT();
   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.
  *
  * 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 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.",
     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 */
     /*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;
     delete p->waiting_synchro;
     p->waiting_synchro = nullptr;
-    mutex->owner = p;
+    this->owner = p;
     SIMIX_simcall_answer(&p->simcall);
   } else {
     /* nobody to wake up */
     SIMIX_simcall_answer(&p->simcall);
   } else {
     /* nobody to wake up */
-    mutex->locked = 0;
-    mutex->owner = nullptr;
+    this->locked = false;
+    this->owner = nullptr;
   }
   XBT_OUT();
 }
 
   }
   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)
 {
 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 **********************************/
 }
 
 /********************************* 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;
   /* 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);
   }
 
   synchro = SIMIX_synchro_wait(issuer->host, timeout);
index c8ea1f6..4b45f95 100644 (file)
 #include "xbt/xbt_os_thread.h"
 #include "src/simix/popping_private.h"
 
 #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;
 
 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 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);
 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);