Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[s4u] implement semaphore as direct SIMIX wrapper
authorMillian Poquet <millian.poquet@inria.fr>
Mon, 1 Oct 2018 17:27:03 +0000 (19:27 +0200)
committerMillian Poquet <millian.poquet@inria.fr>
Mon, 1 Oct 2018 17:27:03 +0000 (19:27 +0200)
include/simgrid/forward.h
include/simgrid/s4u.hpp
include/simgrid/s4u/Semaphore.hpp [new file with mode: 0644]
src/s4u/s4u_Semaphore.cpp [new file with mode: 0644]
tools/cmake/DefinePackages.cmake

index 86a28bd..fd0ab86 100644 (file)
@@ -74,6 +74,13 @@ typedef boost::intrusive_ptr<Mutex> MutexPtr;
 class NetZone;
 class VirtualMachine;
 class File;
 class NetZone;
 class VirtualMachine;
 class File;
+
+class Semaphore;
+/** Smart pointer to a simgrid::s4u::Semaphore */
+typedef boost::intrusive_ptr<Semaphore> SemaphorePtr;
+XBT_PUBLIC void intrusive_ptr_release(Semaphore* m);
+XBT_PUBLIC void intrusive_ptr_add_ref(Semaphore* m);
+
 class Storage;
 } // namespace s4u
 
 class Storage;
 } // namespace s4u
 
index 485ed2d..d3182a5 100644 (file)
@@ -18,6 +18,7 @@
 #include <simgrid/s4u/Mailbox.hpp>
 #include <simgrid/s4u/Mutex.hpp>
 #include <simgrid/s4u/NetZone.hpp>
 #include <simgrid/s4u/Mailbox.hpp>
 #include <simgrid/s4u/Mutex.hpp>
 #include <simgrid/s4u/NetZone.hpp>
+#include <simgrid/s4u/Semaphore.hpp>
 #include <simgrid/s4u/Storage.hpp>
 
 #include <simgrid/Exception.hpp>
 #include <simgrid/s4u/Storage.hpp>
 
 #include <simgrid/Exception.hpp>
diff --git a/include/simgrid/s4u/Semaphore.hpp b/include/simgrid/s4u/Semaphore.hpp
new file mode 100644 (file)
index 0000000..442b147
--- /dev/null
@@ -0,0 +1,54 @@
+/* Copyright (c) 2006-2018. 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_SEMAPHORE_HPP
+#define SIMGRID_S4U_SEMAPHORE_HPP
+
+#include <simgrid/forward.h>
+#include <simgrid/simix.h>
+
+namespace simgrid {
+namespace s4u {
+
+/** @brief A classical semaphore, but blocking in the simulation world
+ *  @ingroup s4u_api
+ *
+ * It is strictly impossible to use a real semaphore, such as
+ * <a href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/sem_init.html">sem_init</a>,
+ * because it would block the whole simulation.
+ * Instead, you should use the present class, that offers a very similar interface.
+ *
+ * As for any S4U object, Semaphores are using the @ref s4u_raii "RAII idiom" for memory management.
+ * Use createSemaphore() to get a ::SemaphorePtr to a newly created semaphore
+ * and only manipulate ::SemaphorePtr.
+ *
+ */
+class XBT_PUBLIC Semaphore {
+  smx_sem_t sem_;
+  std::atomic_int_fast32_t refcount_{0};
+
+  explicit Semaphore(unsigned int initial_capacity);
+  ~Semaphore();
+
+  friend void intrusive_ptr_add_ref(Semaphore* sem);
+  friend void intrusive_ptr_release(Semaphore* sem);
+
+public:
+  // No copy:
+  /** You cannot create a new semaphore by copying an existing one. Use SemaphorePtr instead */
+  Semaphore(Semaphore const&) = delete;
+  /** You cannot create a new semaphore by value assignment either. Use SemaphorePtr instead */
+  Semaphore& operator=(Semaphore const&) = delete;
+
+  /** Constructs a new semaphore */
+  static SemaphorePtr create(unsigned int initial_capacity);
+
+  void acquire();
+  void release();
+};
+
+}} // namespace simgrid::s4u
+
+#endif /* SIMGRID_S4U_SEMAPHORE_HPP */
diff --git a/src/s4u/s4u_Semaphore.cpp b/src/s4u/s4u_Semaphore.cpp
new file mode 100644 (file)
index 0000000..191094a
--- /dev/null
@@ -0,0 +1,56 @@
+/* Copyright (c) 2006-201. 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 "src/msg/msg_private.hpp"
+#include "src/simix/smx_synchro_private.hpp"
+#include "xbt/log.h"
+
+#include "simgrid/s4u/Semaphore.hpp"
+
+namespace simgrid {
+namespace s4u {
+
+Semaphore::Semaphore(unsigned int initial_capacity)
+{
+    sem_ = simgrid::simix::simcall([initial_capacity] { return SIMIX_sem_init(initial_capacity); });
+}
+
+Semaphore::~Semaphore()
+{
+    SIMIX_sem_destroy(sem_);
+}
+
+SemaphorePtr Semaphore::create(unsigned int initial_capacity)
+{
+    return SemaphorePtr(new Semaphore(initial_capacity));
+}
+
+void Semaphore::acquire()
+{
+    simcall_sem_acquire(sem_);
+}
+
+void Semaphore::release()
+{
+    simgrid::simix::simcall([this] { SIMIX_sem_release(sem_); });
+}
+
+void intrusive_ptr_add_ref(Semaphore* sem)
+{
+  xbt_assert(sem);
+  sem->refcount_.fetch_add(1, std::memory_order_relaxed);
+}
+
+void intrusive_ptr_release(Semaphore* sem)
+{
+  xbt_assert(sem);
+  if (sem->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
+    std::atomic_thread_fence(std::memory_order_acquire);
+    delete sem;
+  }
+}
+
+}
+}
index f580465..3cbd633 100644 (file)
@@ -440,6 +440,7 @@ set(S4U_SRC
   src/s4u/s4u_Mailbox.cpp
   src/s4u/s4u_Mutex.cpp
   src/s4u/s4u_Netzone.cpp
   src/s4u/s4u_Mailbox.cpp
   src/s4u/s4u_Mutex.cpp
   src/s4u/s4u_Netzone.cpp
+  src/s4u/s4u_Semaphore.cpp
   src/s4u/s4u_Storage.cpp
 )
 
   src/s4u/s4u_Storage.cpp
 )
 
@@ -707,6 +708,7 @@ set(headers_to_install
   include/simgrid/s4u/Mailbox.hpp
   include/simgrid/s4u/Mutex.hpp
   include/simgrid/s4u/NetZone.hpp
   include/simgrid/s4u/Mailbox.hpp
   include/simgrid/s4u/Mutex.hpp
   include/simgrid/s4u/NetZone.hpp
+  include/simgrid/s4u/Semaphore.hpp
   include/simgrid/s4u/Storage.hpp
   include/simgrid/s4u/VirtualMachine.hpp
   include/simgrid/s4u.hpp
   include/simgrid/s4u/Storage.hpp
   include/simgrid/s4u/VirtualMachine.hpp
   include/simgrid/s4u.hpp