Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
untested C interface to Condition Variables
authorFrederic Suter <frederic.suter@cc.in2p3.fr>
Wed, 10 Jul 2019 10:20:20 +0000 (12:20 +0200)
committerFrederic Suter <frederic.suter@cc.in2p3.fr>
Wed, 10 Jul 2019 10:20:20 +0000 (12:20 +0200)
MANIFEST.in
include/simgrid/cond.h [new file with mode: 0644]
include/simgrid/forward.h
include/simgrid/s4u/ConditionVariable.hpp
src/s4u/s4u_ConditionVariable.cpp
tools/cmake/DefinePackages.cmake

index 516d08f..b58b894 100644 (file)
@@ -1896,6 +1896,7 @@ include include/simgrid/Exception.hpp
 include include/simgrid/actor.h
 include include/simgrid/barrier.h
 include include/simgrid/chrono.hpp
+include include/simgrid/cond.h
 include include/simgrid/config.h.in
 include include/simgrid/engine.h
 include include/simgrid/forward.h
@@ -1925,6 +1926,7 @@ include include/simgrid/link.h
 include include/simgrid/mailbox.h
 include include/simgrid/modelchecker.h
 include include/simgrid/msg.h
+include include/simgrid/mutex.h
 include include/simgrid/plugins/dvfs.h
 include include/simgrid/plugins/energy.h
 include include/simgrid/plugins/file_system.h
diff --git a/include/simgrid/cond.h b/include/simgrid/cond.h
new file mode 100644 (file)
index 0000000..08c3318
--- /dev/null
@@ -0,0 +1,30 @@
+/* Copyright (c) 2019. 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 INCLUDE_SIMGRID_COND_H_
+#define INCLUDE_SIMGRID_COND_H_
+
+#include <simgrid/forward.h>
+
+/* C interface */
+SG_BEGIN_DECL()
+/** @brief Creates a condition variable */
+XBT_PUBLIC sg_cond_t sg_cond_init();
+
+/** @brief Blocks onto the given condition variable */
+XBT_PUBLIC void sg_cond_wait(sg_cond_t cond, sg_mutex_t mutex);
+/** @brief Blocks onto the given condition variable, but only for the given amount of time.
+ *  @return 0 on success, 1 on timeout */
+XBT_PUBLIC int sg_cond_wait_for(sg_cond_t cond, sg_mutex_t mutex, double delay);
+/** @brief Signals the given mutex variable */
+XBT_PUBLIC void sg_cond_notify_one(sg_cond_t cond);
+/** @brief Broadcasts the given mutex variable */
+XBT_PUBLIC void sg_cond_notify_all(sg_cond_t cond);
+/** @brief Destroys the given mutex variable */
+XBT_PUBLIC void sg_cond_destroy(sg_cond_t cond);
+
+SG_END_DECL()
+
+#endif /* INCLUDE_SIMGRID_COND_H_ */
index 65b416c..8d4d26c 100644 (file)
@@ -179,6 +179,7 @@ typedef simgrid::s4u::Barrier s4u_Barrier;
 typedef simgrid::s4u::Host s4u_Host;
 typedef simgrid::s4u::Link s4u_Link;
 typedef simgrid::s4u::File s4u_File;
+typedef simgrid::s4u::ConditionVariable s4u_ConditionVariable;
 typedef simgrid::s4u::Mutex s4u_Mutex;
 typedef simgrid::s4u::Semaphore s4u_Semaphore;
 typedef simgrid::s4u::Storage s4u_Storage;
@@ -200,6 +201,7 @@ typedef struct s4u_Barrier s4u_Barrier;
 typedef struct s4u_Host s4u_Host;
 typedef struct s4u_Link s4u_Link;
 typedef struct s4u_File s4u_File;
+typedef struct s4u_ConditionVariable s4u_ConditionVariable;
 typedef struct s4u_Mutex s4u_Mutex;
 typedef struct s4u_Semaphore s4u_Semaphore;
 typedef struct s4u_Storage s4u_Storage;
@@ -217,6 +219,7 @@ typedef struct s_smx_sem* smx_sem_t;
 #endif
 
 typedef s4u_Barrier* sg_bar_t;
+typedef s4u_ConditionVariable* sg_cond_t;
 typedef s4u_Mutex* sg_mutex_t;
 typedef s4u_Semaphore* sg_sem_t;
 typedef s4u_NetZone* sg_netzone_t;
index 523024d..f11df5c 100644 (file)
@@ -28,9 +28,8 @@ private:
   friend kernel::activity::ConditionVariableImpl;
   kernel::activity::ConditionVariableImpl* const cond_;
 
-  explicit ConditionVariable(kernel::activity::ConditionVariableImpl* cond) : cond_(cond) {}
-
 public:
+  explicit ConditionVariable(kernel::activity::ConditionVariableImpl* cond) : cond_(cond) {}
   ConditionVariable(ConditionVariable const&) = delete;
   ConditionVariable& operator=(ConditionVariable const&) = delete;
 
index e9f2c45..0e3713d 100644 (file)
@@ -3,6 +3,8 @@
 /* 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/cond.h"
+#include "simgrid/forward.h"
 #include "simgrid/s4u/ConditionVariable.hpp"
 #include "simgrid/simix.h"
 #include "src/kernel/activity/ConditionVariableImpl.hpp"
@@ -85,3 +87,38 @@ void intrusive_ptr_release(ConditionVariable* cond)
 
 } // namespace s4u
 } // namespace simgrid
+
+/* **************************** Public C interface *************************** */
+sg_cond_t sg_cond_init()
+{
+  simgrid::kernel::activity::ConditionVariableImpl* cond =
+      simgrid::simix::simcall([] { return new simgrid::kernel::activity::ConditionVariableImpl(); });
+
+  return new simgrid::s4u::ConditionVariable(cond);
+}
+
+void sg_cond_wait(sg_cond_t cond, sg_mutex_t mutex)
+{
+  cond->wait(mutex);
+}
+
+int sg_cond_wait_for(sg_cond_t cond, sg_mutex_t mutex, double delay)
+{
+  std::unique_lock<simgrid::s4u::Mutex> lock(*mutex);
+  return static_cast<int>(cond->wait_for(lock, delay));
+}
+
+void sg_cond_notify_one(sg_cond_t cond)
+{
+  cond->notify_one();
+}
+
+void sg_cond_notify_all(sg_cond_t cond)
+{
+  cond->notify_all();
+}
+
+void sg_cond_destroy(sg_cond_t cond)
+{
+  delete cond;
+}
index b05a4a8..8185799 100644 (file)
@@ -693,6 +693,7 @@ set(headers_to_install
   include/simgrid/kernel/future.hpp
   include/simgrid/host.h
   include/simgrid/link.h
+  include/simgrid/cond.h
   include/simgrid/mutex.h
   include/simgrid/semaphore.h
   include/simgrid/storage.h