Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[s4u] add condition variables
authoradfaure <adrien.faure2@gmail.com>
Fri, 17 Jun 2016 11:46:26 +0000 (13:46 +0200)
committeradfaure <adrien.faure2@gmail.com>
Fri, 17 Jun 2016 14:48:16 +0000 (16:48 +0200)
16 files changed:
examples/s4u/CMakeLists.txt
examples/s4u/actions-comm/s4u_actions-comm.cpp [new file with mode: 0644]
examples/s4u/actions-comm/s4u_actions-comm.tesh [new file with mode: 0644]
examples/s4u/actions-comm/s4u_actions-comm.txt [new file with mode: 0644]
examples/s4u/actions-comm/s4u_actions-comm_d.xml [new file with mode: 0644]
examples/s4u/actions-comm/s4u_actions-comm_split_d.xml [new file with mode: 0644]
examples/s4u/actions-comm/s4u_actions-comm_split_p0.txt [new file with mode: 0644]
examples/s4u/actions-comm/s4u_actions-comm_split_p1.txt [new file with mode: 0644]
include/simgrid/s4u.h
include/simgrid/s4u/conditionVariable.hpp [new file with mode: 0644]
include/simgrid/s4u/mutex.hpp
include/simgrid/simix.h
src/s4u/s4u_conditionVariable.cpp [new file with mode: 0644]
src/simix/smx_synchro.cpp
src/simix/smx_synchro_private.h
tools/cmake/DefinePackages.cmake

index e90125e..f710b29 100644 (file)
@@ -13,4 +13,17 @@ set(txt_files     ${txt_files}    ${CMAKE_CURRENT_SOURCE_DIR}/README  PARENT_SCO
 
 ADD_TESH_FACTORIES(s4u-basic "thread;ucontext;raw;boost" --setenv bindir=${CMAKE_BINARY_DIR}/examples/s4u/basic --cd ${CMAKE_HOME_DIRECTORY}/examples/s4u/basic s4u_basic.tesh)
 ADD_TESH_FACTORIES(s4u-io    "thread;ucontext;raw;boost" --setenv bindir=${CMAKE_BINARY_DIR}/examples/s4u/io    --cd ${CMAKE_HOME_DIRECTORY}/examples/s4u/io    s4u_io.tesh)
 
 ADD_TESH_FACTORIES(s4u-basic "thread;ucontext;raw;boost" --setenv bindir=${CMAKE_BINARY_DIR}/examples/s4u/basic --cd ${CMAKE_HOME_DIRECTORY}/examples/s4u/basic s4u_basic.tesh)
 ADD_TESH_FACTORIES(s4u-io    "thread;ucontext;raw;boost" --setenv bindir=${CMAKE_BINARY_DIR}/examples/s4u/io    --cd ${CMAKE_HOME_DIRECTORY}/examples/s4u/io    s4u_io.tesh)
-ADD_TESH_FACTORIES(s4u-mutex    "thread;ucontext;raw;boost" --setenv bindir=${CMAKE_BINARY_DIR}/examples/s4u/mutex    --cd ${CMAKE_HOME_DIRECTORY}/examples/s4u/mutex    s4u_mutex.tesh)
+ADD_TESH_FACTORIES(s4u-mutex "thread;ucontext;raw;boost" --setenv bindir=${CMAKE_BINARY_DIR}/examples/s4u/mutex    --cd ${CMAKE_HOME_DIRECTORY}/examples/s4u/mutex    s4u_mutex.tesh)
+
+foreach(x actions-comm)
+  add_executable       (s4u_${x}     ${x}/s4u_${x}.cpp)
+  target_link_libraries(s4u_${x}     simgrid)
+  set_target_properties(s4u_${x}  PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${x})
+  set(examples_src ${examples_src} ${CMAKE_CURRENT_SOURCE_DIR}/${x}/s4u_${x}.cpp)
+  set(tesh_files   ${tesh_files}   ${CMAKE_CURRENT_SOURCE_DIR}/${x}/s4u_${x}.tesh)
+endforeach()
+
+
+foreach(x actions-comm)
+  ADD_TESH_FACTORIES(s4u-${x} "thread;ucontext;raw;boost" --setenv bindir=${CMAKE_CURRENT_BINARY_DIR}/${x} --setenv srcdir=${CMAKE_HOME_DIRECTORY}/examples/platforms --cd ${CMAKE_HOME_DIRECTORY}/examples/s4u/${x} s4u_${x}.tesh)
+endforeach()
diff --git a/examples/s4u/actions-comm/s4u_actions-comm.cpp b/examples/s4u/actions-comm/s4u_actions-comm.cpp
new file mode 100644 (file)
index 0000000..cc77351
--- /dev/null
@@ -0,0 +1,317 @@
+/* Copyright (c) 2009-2016. The SimGrid Team. All rights reserved.          */
+
+/* This program is free software; you can redistribute it and/or modify it
+ * under the terms of the license (GNU LGPL) which comes with this package. */
+
+#include "simgrid/msg.h"
+#include "simgrid/simix.h"      /* semaphores for the barrier */
+#include <xbt/replay.h>
+#include "simgrid/s4u.h"
+
+XBT_LOG_NEW_DEFAULT_CATEGORY(actions, "Messages specific for this msg example");
+int communicator_size = 0;
+
+static void action_Isend(const char *const *action);
+
+typedef struct {
+  int last_Irecv_sender_id;
+  int bcast_counter;
+  xbt_dynar_t isends;           /* of msg_comm_t */
+  /* Used to implement irecv+wait */
+  xbt_dynar_t irecvs;           /* of msg_comm_t */
+  xbt_dynar_t tasks;            /* of msg_task_t */
+} s_process_globals_t, *process_globals_t;
+
+/* Helper function */
+static double parse_double(const char *string)
+{
+  double value;
+  char *endptr;
+
+  value = strtod(string, &endptr);
+  if (*endptr != '\0')
+    THROWF(unknown_error, 0, "%s is not a double", string);
+  return value;
+}
+
+#define ACT_DEBUG(...) \
+  if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose)) {  \
+    char *NAME = xbt_str_join_array(action, " ");              \
+    XBT_DEBUG(__VA_ARGS__);                                    \
+    xbt_free(NAME);                                            \
+  } else ((void)0)
+
+static void log_action(const char *const *action, double date)
+{
+  if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose)) {
+    char *name = xbt_str_join_array(action, " ");
+    XBT_VERB("%s %f", name, date);
+    xbt_free(name);
+  }
+}
+
+static void asynchronous_cleanup(void)
+{
+  process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
+
+  /* Destroy any isend which correspond to completed communications */
+  int found;
+  msg_comm_t comm;
+  while ((found = MSG_comm_testany(globals->isends)) != -1) {
+    xbt_dynar_remove_at(globals->isends, found, &comm);
+    MSG_comm_destroy(comm);
+  }
+}
+
+/* My actions */
+static void action_send(const char *const *action)
+{
+  char to[250];
+  const char *size_str = action[3];
+  double size = parse_double(size_str);
+  double clock = MSG_get_clock();
+
+  snprintf(to,249, "%s_%s", MSG_process_get_name(MSG_process_self()), action[2]);
+
+  ACT_DEBUG("Entering Send: %s (size: %g)", NAME, size);
+  if (size < 65536) {
+    action_Isend(action);
+  } else {
+    MSG_task_send(MSG_task_create(to, 0, size, NULL), to);
+  }
+
+  log_action(action, MSG_get_clock() - clock);
+  asynchronous_cleanup();
+}
+
+static void action_Isend(const char *const *action)
+{
+  char to[250];
+  const char *size = action[3];
+  double clock = MSG_get_clock();
+  process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
+
+  snprintf(to,249, "%s_%s", MSG_process_get_name(MSG_process_self()), action[2]);
+  msg_comm_t comm = MSG_task_isend(MSG_task_create(to, 0, parse_double(size), NULL), to);
+  xbt_dynar_push(globals->isends, &comm);
+
+  XBT_DEBUG("Isend on %s", MSG_process_get_name(MSG_process_self()));
+  log_action(action, MSG_get_clock() - clock);
+  asynchronous_cleanup();
+}
+
+static void action_recv(const char *const *action)
+{
+  char mailbox_name[250];
+  msg_task_t task = NULL;
+  double clock = MSG_get_clock();
+
+  snprintf(mailbox_name,249, "%s_%s", action[2], MSG_process_get_name(MSG_process_self()));
+
+  ACT_DEBUG("Receiving: %s", NAME);
+  msg_error_t res = MSG_task_receive(&task, mailbox_name);
+  log_action(action, MSG_get_clock() - clock);
+
+  if (res == MSG_OK) {
+    MSG_task_destroy(task);
+  }
+  asynchronous_cleanup();
+}
+
+static void action_Irecv(const char *const *action)
+{
+  char mailbox[250];
+  double clock = MSG_get_clock();
+  process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
+
+  XBT_DEBUG("Irecv on %s", MSG_process_get_name(MSG_process_self()));
+
+  snprintf(mailbox,249, "%s_%s", action[2], MSG_process_get_name(MSG_process_self()));
+  msg_task_t t = NULL;
+  xbt_dynar_push(globals->tasks, &t);
+  msg_comm_t c = MSG_task_irecv((msg_task**)xbt_dynar_get_ptr(globals->tasks, xbt_dynar_length(globals->tasks) - 1), mailbox);
+  xbt_dynar_push(globals->irecvs, &c);
+
+  log_action(action, MSG_get_clock() - clock);
+  asynchronous_cleanup();
+}
+
+static void action_wait(const char *const *action)
+{
+  msg_task_t task = NULL;
+  msg_comm_t comm;
+  double clock = MSG_get_clock();
+  process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
+
+  xbt_assert(xbt_dynar_length(globals->irecvs), "action wait not preceded by any irecv: %s",
+             xbt_str_join_array(action, " "));
+
+  ACT_DEBUG("Entering %s", NAME);
+  comm = xbt_dynar_pop_as(globals->irecvs, msg_comm_t);
+  MSG_comm_wait(comm, -1);
+  task = xbt_dynar_pop_as(globals->tasks, msg_task_t);
+  MSG_comm_destroy(comm);
+  MSG_task_destroy(task);
+
+  log_action(action, MSG_get_clock() - clock);
+}
+
+/* FIXME: that's a poor man's implementation: we should take the message exchanges into account */
+static void action_barrier(const char *const *action)
+{
+  // static smx_mutex_t mutex = NULL;
+  // static smx_cond_t cond = NULL;
+  static simgrid::s4u::Mutex *mutex = NULL;
+  static simgrid::s4u::ConditionVariable *cond = NULL;
+  static int processes_arrived_sofar = 0;
+  if (mutex == NULL) {          // first arriving on the barrier
+    mutex = new simgrid::s4u::Mutex();
+    cond = new simgrid::s4u::ConditionVariable();
+    processes_arrived_sofar = 0;
+  }
+  ACT_DEBUG("Entering barrier: %s (%d already there)", NAME, processes_arrived_sofar);
+  mutex->lock();
+  if (++processes_arrived_sofar == communicator_size) {
+    cond->notify_all();
+    mutex->unlock();
+  } else {
+    cond->wait(mutex);
+    mutex->unlock();
+  }
+
+  ACT_DEBUG("Exiting barrier: %s", NAME);
+
+  processes_arrived_sofar--;
+  if (processes_arrived_sofar<=0) {
+    delete cond;
+    delete mutex;
+    mutex = NULL;
+  }
+}
+
+static void action_bcast(const char *const *action)
+{
+  char mailbox[80];
+  double comm_size = parse_double(action[2]);
+  msg_task_t task = NULL;
+  double clock = MSG_get_clock();
+
+  process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
+
+  xbt_assert(communicator_size, "Size of Communicator is not defined, can't use collective operations");
+
+  const char * process_name = MSG_process_get_name(MSG_process_self());
+
+  char *bcast_identifier = bprintf("bcast_%d", counters->bcast_counter++);
+
+  if (!strcmp(process_name, "p0")) {
+    XBT_DEBUG("%s: %s is the Root", bcast_identifier, process_name);
+
+    msg_comm_t *comms = xbt_new0(msg_comm_t, communicator_size - 1);
+
+    for (int i = 1; i < communicator_size; i++) {
+      snprintf(mailbox,79, "%s_p0_p%d", bcast_identifier, i);
+      comms[i - 1] = MSG_task_isend(MSG_task_create(mailbox, 0, comm_size, NULL), mailbox);
+    }
+    MSG_comm_waitall(comms, communicator_size - 1, -1);
+    for (int i = 1; i < communicator_size; i++)
+      MSG_comm_destroy(comms[i - 1]);
+    xbt_free(comms);
+
+    XBT_DEBUG("%s: all messages sent by %s have been received", bcast_identifier, process_name);
+  } else {
+    snprintf(mailbox,79, "%s_p0_%s", bcast_identifier, process_name);
+    MSG_task_receive(&task, mailbox);
+    MSG_task_destroy(task);
+    XBT_DEBUG("%s: %s has received", bcast_identifier, process_name);
+  }
+
+  log_action(action, MSG_get_clock() - clock);
+  xbt_free(bcast_identifier);
+}
+
+static void action_comm_size(const char *const *action)
+{
+  const char *size = action[2];
+  double clock = MSG_get_clock();
+
+  communicator_size = parse_double(size);
+  log_action(action, MSG_get_clock() - clock);
+}
+
+static void action_compute(const char *const *action)
+{
+  const char *amount = action[2];
+  msg_task_t task = MSG_task_create("task", parse_double(amount), 0, NULL);
+  double clock = MSG_get_clock();
+
+  ACT_DEBUG("Entering %s", NAME);
+  MSG_task_execute(task);
+  MSG_task_destroy(task);
+  log_action(action, MSG_get_clock() - clock);
+}
+
+static void action_init(const char *const *action)
+{
+  XBT_DEBUG("Initialize the counters");
+  process_globals_t globals = (process_globals_t) calloc(1, sizeof(s_process_globals_t));
+  globals->isends = xbt_dynar_new(sizeof(msg_comm_t), NULL);
+  globals->irecvs = xbt_dynar_new(sizeof(msg_comm_t), NULL);
+  globals->tasks = xbt_dynar_new(sizeof(msg_task_t), NULL);
+  MSG_process_set_data(MSG_process_self(), globals);
+}
+
+static void action_finalize(const char *const *action)
+{
+  process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
+  if (globals) {
+    asynchronous_cleanup();
+    xbt_dynar_free_container(&(globals->isends));
+    xbt_dynar_free_container(&(globals->irecvs));
+    xbt_dynar_free_container(&(globals->tasks));
+    xbt_free(globals);
+  }
+}
+
+int main(int argc, char *argv[])
+{
+  msg_error_t res = MSG_OK;
+
+  /* Check the given arguments */
+  MSG_init(&argc, argv);
+  /* Explicit initialization of the action module is required now*/
+  MSG_action_init();
+
+  xbt_assert(argc > 2,
+       "Usage: %s platform_file deployment_file [action_files]\n"
+       "\t# if all actions are in the same file\n"
+       "\tExample: %s msg_platform.xml msg_deployment.xml actions\n"
+       "\t# if actions are in separate files, specified in deployment\n"
+       "\tExample: %s msg_platform.xml msg_deployment.xml ",
+       argv[0],argv[0],argv[0]);
+
+  MSG_create_environment(argv[1]);
+  MSG_launch_application(argv[2]);
+
+  /*   Action registration */
+  xbt_replay_action_register("init", action_init);
+  xbt_replay_action_register("finalize", action_finalize);
+  xbt_replay_action_register("comm_size", action_comm_size);
+  xbt_replay_action_register("send", action_send);
+  xbt_replay_action_register("Isend", action_Isend);
+  xbt_replay_action_register("recv", action_recv);
+  xbt_replay_action_register("Irecv", action_Irecv);
+  xbt_replay_action_register("wait", action_wait);
+  xbt_replay_action_register("barrier", action_barrier);
+  xbt_replay_action_register("bcast", action_bcast);
+  xbt_replay_action_register("compute", action_compute);
+
+  /* Actually do the simulation using MSG_action_trace_run */
+  res = MSG_action_trace_run(argv[3]);  // it's ok to pass a NULL argument here
+
+  XBT_INFO("Simulation time %g", MSG_get_clock());
+
+  MSG_action_exit(); /* Explicit finalization of the action module */
+
+  return res != MSG_OK;
+}
diff --git a/examples/s4u/actions-comm/s4u_actions-comm.tesh b/examples/s4u/actions-comm/s4u_actions-comm.tesh
new file mode 100644 (file)
index 0000000..c205737
--- /dev/null
@@ -0,0 +1,27 @@
+! output sort 19
+$ ${bindir:=.}/s4u_actions-comm --log=actions.thres=verbose ${srcdir:=.}/small_platform_fatpipe.xml s4u_actions-comm_split_d.xml "--log=root.fmt:[%10.6r]%e(%i:%P@%h)%e%m%n"
+> [ 20.703314] (1:p0@Tremblay) p0 recv p1 20.703314
+> [ 20.703314] (2:p1@Ruby) p1 send p0 1e10 20.703314
+> [ 30.897513] (0:maestro@) Simulation time 30.8975
+> [ 30.897513] (1:p0@Tremblay) p0 compute 1e9 10.194200
+> [ 30.897513] (2:p1@Ruby) p1 compute 1e9 10.194200
+
+! output sort 19
+$ ${bindir:=.}/s4u_actions-comm --log=actions.thres=verbose ${srcdir:=.}/small_platform_fatpipe.xml s4u_actions-comm_d.xml s4u_actions-comm.txt "--log=root.fmt:[%10.6r]%e(%i:%P@%h)%e%m%n"
+> [  0.000000] (1:p0@Tremblay) p0 comm_size 3 0.000000
+> [  1.037020] (1:p0@Tremblay) p0 bcast 5e8 1.037020
+> [  1.037020] (2:p1@Ruby) p1 bcast 5e8 1.037020
+> [  1.037020] (3:p2@Perl) p2 bcast 5e8 1.037020
+> [  1.082894] (1:p0@Tremblay) p0 compute 4.5E6 0.045874
+> [  1.123670] (1:p0@Tremblay) p0 compute 4E6 0.040777
+> [  1.149156] (1:p0@Tremblay) p0 compute 2.5E6 0.025485
+> [  1.149156] (2:p1@Ruby) p1 Irecv p0 0.000000
+> [  1.149156] (3:p2@Perl) p2 Irecv p1 0.000000
+> [  3.221244] (1:p0@Tremblay) p0 send p1 1e9 2.072088
+> [  6.246256] (3:p2@Perl) p2 compute 5e8 5.097100
+> [ 11.343355] (2:p1@Ruby) p1 compute 1e9 10.194200
+> [ 11.343355] (2:p1@Ruby) p1 wait 0.000000
+> [ 11.343355] (2:p1@Ruby) p1 Isend p2 1e9 0.000000
+> [ 13.415443] (0:maestro@) Simulation time 13.4154
+> [ 13.415443] (1:p0@Tremblay) p0 compute 1e9 10.194200
+> [ 13.415443] (3:p2@Perl) p2 wait 7.169187
diff --git a/examples/s4u/actions-comm/s4u_actions-comm.txt b/examples/s4u/actions-comm/s4u_actions-comm.txt
new file mode 100644 (file)
index 0000000..58c7e61
--- /dev/null
@@ -0,0 +1,37 @@
+# sample action file
+p0 init
+p1 init
+p2 init
+
+p0 comm_size 3
+p0 bcast 5e8
+p1 bcast 5e8
+p2 bcast 5e8
+
+p0 compute 4.5E6
+p0 compute 4E6
+p0 compute 2.5E6
+
+p0 barrier
+p1 barrier
+p2 barrier
+
+p0 send p1 1e9
+p0 compute 1e9
+
+p1 Irecv p0
+p1 compute 1e9
+p1 wait
+p1 Isend p2 1e9
+
+p2 Irecv p1
+p2 compute 5e8
+p2 wait
+
+p0 barrier
+p1 barrier
+p2 barrier
+
+p0 finalize
+p1 finalize
+p2 finalize
diff --git a/examples/s4u/actions-comm/s4u_actions-comm_d.xml b/examples/s4u/actions-comm/s4u_actions-comm_d.xml
new file mode 100644 (file)
index 0000000..8a5602e
--- /dev/null
@@ -0,0 +1,7 @@
+<?xml version='1.0'?>
+<!DOCTYPE platform SYSTEM "http://simgrid.gforge.inria.fr/simgrid/simgrid.dtd">
+<platform version="4">
+  <process host="Tremblay" function="p0"/>
+  <process host="Ruby" function="p1"/>
+  <process host="Perl" function="p2"/>
+</platform>
diff --git a/examples/s4u/actions-comm/s4u_actions-comm_split_d.xml b/examples/s4u/actions-comm/s4u_actions-comm_split_d.xml
new file mode 100644 (file)
index 0000000..64fc192
--- /dev/null
@@ -0,0 +1,10 @@
+<?xml version='1.0'?>
+<!DOCTYPE platform SYSTEM "http://simgrid.gforge.inria.fr/simgrid/simgrid.dtd">
+<platform version="4">
+<!-- Example file of how to use trace replay, with actions split in separate files, one per process.
+     Launch it like this:
+         ./actions-comm ../../platforms/platform.xml actions-comm_split_d.xml  -->
+
+  <process host="Tremblay" function="p0">    <argument value="s4u_actions-comm_split_p0.txt"/>  </process>
+  <process host="Ruby"     function="p1">    <argument value="s4u_actions-comm_split_p1.txt"/>  </process>
+</platform>
diff --git a/examples/s4u/actions-comm/s4u_actions-comm_split_p0.txt b/examples/s4u/actions-comm/s4u_actions-comm_split_p0.txt
new file mode 100644 (file)
index 0000000..dd16140
--- /dev/null
@@ -0,0 +1,6 @@
+# sample action file (with only the actions for p0, to be launched by deployment file)
+p0 init
+p0 recv p1
+p0 compute 1e9
+p0 finalize
+
diff --git a/examples/s4u/actions-comm/s4u_actions-comm_split_p1.txt b/examples/s4u/actions-comm/s4u_actions-comm_split_p1.txt
new file mode 100644 (file)
index 0000000..d7e9dfe
--- /dev/null
@@ -0,0 +1,5 @@
+# sample action file (with only the actions for p1, to be launched by deployment file)
+p1 init
+p1 send p0 1e10
+p1 compute 1e9
+p1 finalize
index dde3205..63b75a8 100644 (file)
@@ -12,6 +12,7 @@
 #include "s4u/host.hpp"
 
 #include "s4u/mutex.hpp"
 #include "s4u/host.hpp"
 
 #include "s4u/mutex.hpp"
+#include "s4u/conditionVariable.hpp"
 #include "s4u/Activity.hpp"
 #include "s4u/comm.hpp"
 
 #include "s4u/Activity.hpp"
 #include "s4u/comm.hpp"
 
diff --git a/include/simgrid/s4u/conditionVariable.hpp b/include/simgrid/s4u/conditionVariable.hpp
new file mode 100644 (file)
index 0000000..ec9823d
--- /dev/null
@@ -0,0 +1,65 @@
+/* Copyright (c) 2006-2016. The SimGrid Team. All rights reserved.          */
+
+/* This program is free software; you can redistribute it and/or modify it
+ * under the terms of the license (GNU LGPL) which comes with this package. */
+
+#ifndef SIMGRID_S4U_COND_VARIABLE_HPP
+#define SIMGRID_S4U_COND_VARIABLE_HPP
+
+#include <simgrid/s4u/mutex.hpp>
+
+
+namespace simgrid {
+namespace s4u {
+
+class Mutex;
+
+XBT_PUBLIC_CLASS ConditionVariable {
+  
+public:
+  ConditionVariable();
+
+  ConditionVariable(ConditionVariable* cond) : cond_(SIMIX_cond_ref(cond->cond_)) {}
+  ~ConditionVariable();
+
+  // Copy+move (with the copy-and-swap idiom):
+  ConditionVariable(ConditionVariable const& cond) : cond_(SIMIX_cond_ref(cond.cond_)) {}
+  friend void swap(ConditionVariable& first, ConditionVariable& second)
+  {
+    using std::swap;
+    swap(first.cond_, second.cond_);
+  }
+  ConditionVariable& operator=(ConditionVariable cond)
+  {
+    swap(*this, cond);
+    return *this;
+  }
+  ConditionVariable(ConditionVariable&& cond) : cond_(nullptr)
+  {
+    swap(*this, cond);
+  }
+
+  bool valid() const
+  {
+    return cond_ != nullptr;
+  }
+  
+  /**
+  * Wait functions
+  */
+  void wait(Mutex *mutex);
+  void wait_for(Mutex *mutex, double time);
+
+  /**
+  * Notify functions
+  */
+  void notify();
+  void notify_all();
+
+private:
+  smx_cond_t cond_;
+
+};
+}} // namespace simgrid::s4u
+
+#endif /* SIMGRID_S4U_COND_VARIABLE_HPP */
index 201f071..75b7d09 100644 (file)
 #include <boost/intrusive_ptr.hpp>
 #include <xbt/base.h>
 #include "simgrid/simix.h"
 #include <boost/intrusive_ptr.hpp>
 #include <xbt/base.h>
 #include "simgrid/simix.h"
+#include <simgrid/s4u/conditionVariable.hpp>
 
 namespace simgrid {
 namespace s4u {
 
 
 namespace simgrid {
 namespace s4u {
 
+class ConditionVariable;
 XBT_PUBLIC_CLASS Mutex {
 XBT_PUBLIC_CLASS Mutex {
-
+friend ConditionVariable;
 public:
   Mutex() :
     mutex_(simcall_mutex_init()) {}
 public:
   Mutex() :
     mutex_(simcall_mutex_init()) {}
index ebd2598..9beaaf8 100644 (file)
@@ -384,6 +384,7 @@ XBT_PUBLIC(void) simcall_mutex_unlock(smx_mutex_t mutex);
 
 XBT_PUBLIC(smx_cond_t) simcall_cond_init(void);
 XBT_PUBLIC(void) SIMIX_cond_unref(smx_cond_t cond);
 
 XBT_PUBLIC(smx_cond_t) simcall_cond_init(void);
 XBT_PUBLIC(void) SIMIX_cond_unref(smx_cond_t cond);
+XBT_PUBLIC(smx_cond_t) SIMIX_cond_ref(smx_cond_t cond);
 XBT_PUBLIC(void) simcall_cond_signal(smx_cond_t cond);
 XBT_PUBLIC(void) simcall_cond_wait(smx_cond_t cond, smx_mutex_t mutex);
 XBT_PUBLIC(void) simcall_cond_wait_timeout(smx_cond_t cond,
 XBT_PUBLIC(void) simcall_cond_signal(smx_cond_t cond);
 XBT_PUBLIC(void) simcall_cond_wait(smx_cond_t cond, smx_mutex_t mutex);
 XBT_PUBLIC(void) simcall_cond_wait_timeout(smx_cond_t cond,
diff --git a/src/s4u/s4u_conditionVariable.cpp b/src/s4u/s4u_conditionVariable.cpp
new file mode 100644 (file)
index 0000000..60e3365
--- /dev/null
@@ -0,0 +1,36 @@
+#include "simgrid/s4u/conditionVariable.hpp"
+#include "simgrid/simix.h"
+
+using namespace simgrid;
+
+s4u::ConditionVariable::ConditionVariable()  : cond_(simcall_cond_init()){
+    
+}
+
+s4u::ConditionVariable::~ConditionVariable() {
+  SIMIX_cond_unref(cond_);
+}
+
+/**
+ * Wait functions
+ */
+void s4u::ConditionVariable::wait(s4u::Mutex *mutex) {
+  simcall_cond_wait(cond_, mutex->mutex_);
+}
+  
+void s4u::ConditionVariable::wait_for(s4u::Mutex *mutex, double timeout) {
+  simcall_cond_wait_timeout(cond_, mutex->mutex_, timeout);
+}
+  
+/**
+ * Notify functions
+ */
+void s4u::ConditionVariable::notify() { 
+   simcall_cond_signal(cond_);
+}
+void s4u::ConditionVariable::notify_all() {
+  simcall_cond_broadcast(cond_);
+}
+
index 3817b17..c8ec7fe 100644 (file)
@@ -241,7 +241,6 @@ smx_cond_t SIMIX_cond_init(void)
   cond->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
   cond->mutex = nullptr;
   cond->refcount_ = 1;
   cond->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
   cond->mutex = nullptr;
   cond->refcount_ = 1;
-  intrusive_ptr_add_ref(cond);
   XBT_OUT();
   return cond;
 }
   XBT_OUT();
   return cond;
 }
@@ -352,12 +351,13 @@ void SIMIX_cond_broadcast(smx_cond_t cond)
   XBT_OUT();
 }
 
   XBT_OUT();
 }
 
-/**
- * \brief Destroys a condition.
- *
- * Destroys and frees the condition's memory. 
- * \param cond A condition
- */
+smx_cond_t SIMIX_cond_ref(smx_cond_t cond)
+{
+  if (cond != nullptr)
+    intrusive_ptr_add_ref(cond);
+  return cond;
+}
+
 void SIMIX_cond_unref(smx_cond_t cond)
 {
   XBT_IN("(%p)",cond);
 void SIMIX_cond_unref(smx_cond_t cond)
 {
   XBT_IN("(%p)",cond);
index 943e1ad..73f87c1 100644 (file)
@@ -66,6 +66,8 @@ typedef struct s_smx_sem {
   xbt_swag_t sleeping;          /* list of sleeping process */
 } s_smx_sem_t;
 
   xbt_swag_t sleeping;          /* list of sleeping process */
 } s_smx_sem_t;
 
+
+
 XBT_PRIVATE void SIMIX_post_synchro(smx_synchro_t synchro);
 XBT_PRIVATE void SIMIX_synchro_stop_waiting(smx_process_t process, smx_simcall_t simcall);
 XBT_PRIVATE void SIMIX_synchro_destroy(smx_synchro_t synchro);
 XBT_PRIVATE void SIMIX_post_synchro(smx_synchro_t synchro);
 XBT_PRIVATE void SIMIX_synchro_stop_waiting(smx_process_t process, smx_simcall_t simcall);
 XBT_PRIVATE void SIMIX_synchro_destroy(smx_synchro_t synchro);
@@ -74,12 +76,11 @@ XBT_PRIVATE void SIMIX_synchro_finish(smx_synchro_t synchro);
 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);
+XBT_PRIVATE void intrusive_ptr_add_ref(s_smx_cond_t *cond);
+XBT_PRIVATE void intrusive_ptr_release(s_smx_cond_t *cond);
 
 XBT_PRIVATE XBT_PRIVATE smx_sem_t SIMIX_sem_init(unsigned int value);
 XBT_PRIVATE void SIMIX_sem_release(smx_sem_t sem);
 XBT_PRIVATE int SIMIX_sem_would_block(smx_sem_t sem);
 XBT_PRIVATE int SIMIX_sem_get_capacity(smx_sem_t sem);
 
 XBT_PRIVATE XBT_PRIVATE smx_sem_t SIMIX_sem_init(unsigned int value);
 XBT_PRIVATE void SIMIX_sem_release(smx_sem_t sem);
 XBT_PRIVATE int SIMIX_sem_would_block(smx_sem_t sem);
 XBT_PRIVATE int SIMIX_sem_get_capacity(smx_sem_t sem);
-
-XBT_PRIVATE void intrusive_ptr_release(s_smx_cond_t *cond);
-XBT_PRIVATE void intrusive_ptr_add_ref(s_smx_cond_t *cond);
 #endif
 #endif
index 78f1271..a22af8f 100644 (file)
@@ -382,6 +382,7 @@ set(S4U_SRC
   src/s4u/s4u_host.cpp  
   src/s4u/s4u_mailbox.cpp
   src/s4u/s4u_mutex.cpp
   src/s4u/s4u_host.cpp  
   src/s4u/s4u_mailbox.cpp
   src/s4u/s4u_mutex.cpp
+  src/s4u/s4u_conditionVariable.cpp
   src/s4u/s4u_storage.cpp
 )
 
   src/s4u/s4u_storage.cpp
 )
 
@@ -648,6 +649,7 @@ set(headers_to_install
   include/simgrid/s4u/host.hpp  
   include/simgrid/s4u/mailbox.hpp  
   include/simgrid/s4u/mutex.hpp
   include/simgrid/s4u/host.hpp  
   include/simgrid/s4u/mailbox.hpp  
   include/simgrid/s4u/mutex.hpp
+  include/simgrid/s4u/conditionVariable.hpp
   include/simgrid/s4u/storage.hpp  
   include/simgrid/s4u.h
   include/simgrid/plugins/energy.h
   include/simgrid/s4u/storage.hpp  
   include/simgrid/s4u.h
   include/simgrid/plugins/energy.h