Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of framagit.org:simgrid/simgrid
[simgrid.git] / src / s4u / s4u_Semaphore.cpp
index 191094a..b407059 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2006-201. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2018-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. */
@@ -7,6 +7,7 @@
 #include "src/simix/smx_synchro_private.hpp"
 #include "xbt/log.h"
 
+#include "simgrid/forward.h"
 #include "simgrid/s4u/Semaphore.hpp"
 
 namespace simgrid {
@@ -32,11 +33,26 @@ void Semaphore::acquire()
     simcall_sem_acquire(sem_);
 }
 
+int Semaphore::acquire_timeout(double timeout)
+{
+  return simcall_sem_acquire_timeout(sem_, timeout);
+}
+
 void Semaphore::release()
 {
     simgrid::simix::simcall([this] { SIMIX_sem_release(sem_); });
 }
 
+int Semaphore::get_capacity()
+{
+  return simgrid::simix::simcall([this] { return SIMIX_sem_get_capacity(sem_); });
+}
+
+int Semaphore::would_block()
+{
+  return simgrid::simix::simcall([this] { return SIMIX_sem_would_block(sem_); });
+}
+
 void intrusive_ptr_add_ref(Semaphore* sem)
 {
   xbt_assert(sem);
@@ -54,3 +70,47 @@ void intrusive_ptr_release(Semaphore* sem)
 
 }
 }
+/* **************************** Public C interface *************************** */
+/** @brief creates a semaphore object of the given initial capacity */
+sg_sem_t sg_sem_init(int initial_value)
+{
+  return new simgrid::s4u::Semaphore(initial_value);
+}
+
+/** @brief locks on a semaphore object */
+void sg_sem_acquire(sg_sem_t sem)
+{
+  sem->acquire();
+}
+
+/** @brief locks on a semaphore object up until the provided timeout expires */
+int sg_sem_acquire_timeout(sg_sem_t sem, double timeout)
+{
+  return sem->acquire_timeout(timeout);
+}
+
+/** @brief releases the semaphore object */
+void sg_sem_release(sg_sem_t sem)
+{
+  sem->release();
+}
+
+int sg_sem_get_capacity(sg_sem_t sem)
+{
+  return sem->get_capacity();
+}
+
+void sg_sem_destroy(sg_sem_t sem)
+{
+  delete sem;
+}
+
+/** @brief returns a boolean indicating if this semaphore would block at this very specific time
+ *
+ * Note that the returned value may be wrong right after the function call, when you try to use it...
+ * But that's a classical semaphore issue, and SimGrid's semaphore are not different to usual ones here.
+ */
+int sg_sem_would_block(sg_sem_t sem)
+{
+  return sem->would_block();
+}