X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/39c935d6d5ee86d153f6f7e6a10d723ae7c57f6f..7672ae43c2b49a7dcdc0976cc89a05cc87ae534c:/src/s4u/s4u_Semaphore.cpp diff --git a/src/s4u/s4u_Semaphore.cpp b/src/s4u/s4u_Semaphore.cpp index 7143d4faf5..d8f64714f3 100644 --- a/src/s4u/s4u_Semaphore.cpp +++ b/src/s4u/s4u_Semaphore.cpp @@ -1,84 +1,86 @@ -/* Copyright (c) 2018-2021. The SimGrid Team. All rights reserved. */ +/* Copyright (c) 2018-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/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/SynchroObserver.hpp" +#include "src/mc/mc_replay.hpp" -namespace simgrid { -namespace s4u { - -Semaphore::Semaphore(unsigned int initial_capacity) -{ - sem_ = kernel::actor::simcall([initial_capacity] { return new kernel::activity::SemaphoreImpl(initial_capacity); }); -} - -Semaphore::~Semaphore() -{ - if (sem_ != nullptr) { - xbt_assert(not sem_->is_used(), "Cannot destroy semaphore since someone is still using it"); - delete sem_; - } -} +namespace simgrid::s4u { SemaphorePtr Semaphore::create(unsigned int initial_capacity) { - return SemaphorePtr(new Semaphore(initial_capacity)); + auto* sem = new kernel::activity::SemaphoreImpl(initial_capacity); + return SemaphorePtr(&sem->sem(), false); } void Semaphore::acquire() { - simcall_sem_acquire(sem_); + acquire_timeout(-1); } -int Semaphore::acquire_timeout(double timeout) +bool Semaphore::acquire_timeout(double timeout) { - return simcall_sem_acquire_timeout(sem_, timeout); + kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self(); + + 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] { sem_->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 sem_->get_capacity(); }); + return pimpl_->get_capacity(); } -int Semaphore::would_block() const +bool Semaphore::would_block() const { - return kernel::actor::simcall([this] { return sem_->would_block(); }); + return pimpl_->would_block(); } -void intrusive_ptr_add_ref(Semaphore* sem) +/* refcounting of the intrusive_ptr is delegated to the implementation object */ +void intrusive_ptr_add_ref(const Semaphore* sem) { - xbt_assert(sem); - sem->refcount_.fetch_add(1, std::memory_order_relaxed); + intrusive_ptr_add_ref(sem->pimpl_); } -void intrusive_ptr_release(Semaphore* sem) +void intrusive_ptr_release(const 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; - } + 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 */ sg_sem_t sg_sem_init(int initial_value) { - return new simgrid::s4u::Semaphore(initial_value); + return simgrid::s4u::Semaphore::create(initial_value).detach(); } /** @brief locks on a semaphore object */ @@ -106,7 +108,7 @@ int sg_sem_get_capacity(const_sg_sem_t sem) void sg_sem_destroy(const_sg_sem_t sem) { - delete sem; + intrusive_ptr_release(sem); } /** @brief returns a boolean indicating if this semaphore would block at this very specific time