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 57a6283..d16abce 100644 (file)
@@ -1,29 +1,24 @@
-/* Copyright (c) 2018-2019. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2018-2021. 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/forward.h"
 #include "simgrid/s4u/Semaphore.hpp"
+#include "src/kernel/activity/SemaphoreImpl.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(unsigned int initial_capacity) : pimpl_(new kernel::activity::SemaphoreImpl(initial_capacity)) {}
 
 Semaphore::~Semaphore()
 {
-  if (sem_ != nullptr) {
-    xbt_assert(sem_->sleeping.empty(), "Cannot destroy semaphore since someone is still using it");
-    delete sem_;
-  }
+  xbt_assert(not pimpl_->is_used(), "Cannot destroy semaphore since someone is still using it");
+  delete pimpl_;
 }
 
 SemaphorePtr Semaphore::create(unsigned int initial_capacity)
@@ -33,27 +28,27 @@ SemaphorePtr Semaphore::create(unsigned int initial_capacity)
 
 void Semaphore::acquire()
 {
-  simcall_sem_acquire(sem_);
+  simcall_sem_acquire(pimpl_);
 }
 
 int Semaphore::acquire_timeout(double timeout)
 {
-  return simcall_sem_acquire_timeout(sem_, timeout);
+  return simcall_sem_acquire_timeout(pimpl_, timeout);
 }
 
 void Semaphore::release()
 {
-  simgrid::simix::simcall([this] { SIMIX_sem_release(sem_); });
+  kernel::actor::simcall([this] { pimpl_->release(); });
 }
 
-int Semaphore::get_capacity()
+int Semaphore::get_capacity() const
 {
-  return simgrid::simix::simcall([this] { return SIMIX_sem_get_capacity(sem_); });
+  return kernel::actor::simcall([this] { return pimpl_->get_capacity(); });
 }
 
-int Semaphore::would_block()
+int Semaphore::would_block() const
 {
-  return simgrid::simix::simcall([this] { return SIMIX_sem_would_block(sem_); });
+  return kernel::actor::simcall([this] { return pimpl_->would_block(); });
 }
 
 void intrusive_ptr_add_ref(Semaphore* sem)
@@ -71,8 +66,9 @@ void intrusive_ptr_release(Semaphore* sem)
   }
 }
 
-}
-}
+} // namespace s4u
+} // namespace simgrid
+
 /* **************************** Public C interface *************************** */
 /** @brief creates a semaphore object of the given initial capacity */
 sg_sem_t sg_sem_init(int initial_value)
@@ -98,12 +94,12 @@ void sg_sem_release(sg_sem_t sem)
   sem->release();
 }
 
-int sg_sem_get_capacity(sg_sem_t sem)
+int sg_sem_get_capacity(const_sg_sem_t sem)
 {
   return sem->get_capacity();
 }
 
-void sg_sem_destroy(sg_sem_t sem)
+void sg_sem_destroy(const_sg_sem_t sem)
 {
   delete sem;
 }
@@ -113,7 +109,7 @@ void sg_sem_destroy(sg_sem_t sem)
  * 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)
+int sg_sem_would_block(const_sg_sem_t sem)
 {
   return sem->would_block();
 }