X-Git-Url: http://bilbo.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/554255fe3f311a54df171d31a4a27f4004840f59..8052f28d5b5a9d88b6724dff2e5e81dee10065d7:/src/s4u/s4u_Semaphore.cpp?ds=sidebyside diff --git a/src/s4u/s4u_Semaphore.cpp b/src/s4u/s4u_Semaphore.cpp index 56a3bf1514..7144363c51 100644 --- a/src/s4u/s4u_Semaphore.cpp +++ b/src/s4u/s4u_Semaphore.cpp @@ -1,18 +1,17 @@ -/* Copyright (c) 2018-2021. The SimGrid Team. All rights reserved. */ +/* Copyright (c) 2018-2023. 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 "xbt/log.h" +#include +#include +#include -#include "simgrid/forward.h" -#include "simgrid/s4u/Semaphore.hpp" #include "src/kernel/activity/SemaphoreImpl.hpp" -#include "src/kernel/actor/SimcallObserver.hpp" +#include "src/kernel/actor/SynchroObserver.hpp" +#include "src/mc/mc_replay.hpp" -namespace simgrid { -namespace s4u { +namespace simgrid::s4u { SemaphorePtr Semaphore::create(unsigned int initial_capacity) { @@ -22,49 +21,60 @@ SemaphorePtr Semaphore::create(unsigned int initial_capacity) void Semaphore::acquire() { - kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self(); - kernel::actor::SemAcquireSimcall observer{issuer, pimpl_}; - kernel::actor::simcall_blocking([&observer] { observer.get_sem()->acquire(observer.get_issuer(), -1.0); }, &observer); + acquire_timeout(-1); } bool Semaphore::acquire_timeout(double timeout) { kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self(); - kernel::actor::SemAcquireSimcall observer{issuer, pimpl_, timeout}; - return kernel::actor::simcall_blocking( - [&observer] { observer.get_sem()->acquire(observer.get_issuer(), observer.get_timeout()); }, &observer); + + if (MC_is_active() || MC_record_replay_is_active()) { // Split in 2 simcalls for transition persistency + kernel::actor::SemaphoreObserver lock_observer{issuer, mc::Transition::Type::SEM_ASYNC_LOCK, pimpl_}; + auto acquisition = + kernel::actor::simcall_answered([issuer, this] { return pimpl_->acquire_async(issuer); }, &lock_observer); + + kernel::actor::SemaphoreAcquisitionObserver wait_observer{issuer, mc::Transition::Type::SEM_WAIT, acquisition.get(), + timeout}; + return kernel::actor::simcall_blocking([issuer, acquisition, timeout] { acquisition->wait_for(issuer, timeout); }, + &wait_observer); + + } else { // Do it in one simcall only and without observer + kernel::actor::SemaphoreAcquisitionObserver observer{issuer, mc::Transition::Type::SEM_WAIT, nullptr, timeout}; + return kernel::actor::simcall_blocking( + [this, issuer, timeout] { pimpl_->acquire_async(issuer)->wait_for(issuer, timeout); }, &observer); + } } void Semaphore::release() { - kernel::actor::simcall([this] { pimpl_->release(); }); + kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self(); + kernel::actor::SemaphoreObserver observer{issuer, mc::Transition::Type::SEM_UNLOCK, pimpl_}; + + kernel::actor::simcall_answered([this] { pimpl_->release(); }, &observer); } int Semaphore::get_capacity() const { - return kernel::actor::simcall([this] { return pimpl_->get_capacity(); }); + return pimpl_->get_capacity(); } bool Semaphore::would_block() const { - return kernel::actor::simcall([this] { return pimpl_->would_block(); }); + return pimpl_->would_block(); } /* refcounting of the intrusive_ptr is delegated to the implementation object */ void intrusive_ptr_add_ref(const Semaphore* sem) { - xbt_assert(sem); - sem->pimpl_->ref(); + intrusive_ptr_add_ref(sem->pimpl_); } void intrusive_ptr_release(const Semaphore* sem) { - xbt_assert(sem); - sem->pimpl_->unref(); + intrusive_ptr_release(sem->pimpl_); } -} // namespace s4u -} // namespace simgrid +} // namespace simgrid::s4u /* **************************** Public C interface *************************** */ /** @brief creates a semaphore object of the given initial capacity */