]> AND Public Git Repository - simgrid.git/blobdiff - src/kernel/activity/SemaphoreImpl.cpp
Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
kill some remains of the pre-C++ era
[simgrid.git] / src / kernel / activity / SemaphoreImpl.cpp
index 7f0b5661ba261cfa666a4f0bf81b23dc8810fb31..021ef85150946e74973a87725162435c86ccf717 100644 (file)
@@ -1,12 +1,14 @@
-/* Copyright (c) 2019-2021. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2019-2022. 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/kernel/activity/SemaphoreImpl.hpp"
-#include "src/kernel/activity/SynchroRaw.hpp"
+#include "src/kernel/activity/Synchro.hpp"
+#include "src/kernel/actor/SimcallObserver.hpp"
+#include <cmath> // std::isfinite
 
-XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_semaphore, simix_synchro, "Semaphore kernel-space implementation");
+XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ker_semaphore, ker_synchro, "Semaphore kernel-space implementation");
 
 namespace simgrid {
 namespace kernel {
@@ -15,8 +17,15 @@ namespace activity {
 void SemaphoreImpl::acquire(actor::ActorImpl* issuer, double timeout)
 {
   XBT_DEBUG("Wait semaphore %p (timeout:%f)", this, timeout);
+  xbt_assert(std::isfinite(timeout), "timeout is not finite!");
+
   if (value_ <= 0) {
-    RawImplPtr synchro(new RawImpl());
+    SynchroImplPtr synchro(new SynchroImpl([this, issuer]() {
+      this->remove_sleeping_actor(*issuer);
+      auto* observer = dynamic_cast<kernel::actor::SemAcquireSimcall*>(issuer->simcall_.observer_);
+      xbt_assert(observer != nullptr);
+      observer->set_result(true);
+    }));
     synchro->set_host(issuer->get_host()).set_timeout(timeout).start();
     synchro->register_simcall(&issuer->simcall_);
     sleeping_.push_back(*issuer);
@@ -42,21 +51,3 @@ void SemaphoreImpl::release()
 } // namespace activity
 } // namespace kernel
 } // namespace simgrid
-
-// Simcall handlers:
-/**
- * @brief Handles a sem acquire simcall without timeout.
- */
-void simcall_HANDLER_sem_acquire(smx_simcall_t simcall, smx_sem_t sem)
-{
-  sem->acquire(simcall->issuer_, -1);
-}
-
-/**
- * @brief Handles a sem acquire simcall with timeout.
- */
-void simcall_HANDLER_sem_acquire_timeout(smx_simcall_t simcall, smx_sem_t sem, double timeout)
-{
-  simcall_sem_acquire_timeout__set__result(simcall, 0); // default result, will be set to 1 on timeout
-  sem->acquire(simcall->issuer_, timeout);
-}