X-Git-Url: http://bilbo.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/cbd8dae6d524fc62b0fb5fcf7b2604e98f953615..a7cc502f224afb96f0203b94759cd0dabebf8de5:/src/s4u/s4u_Semaphore.cpp diff --git a/src/s4u/s4u_Semaphore.cpp b/src/s4u/s4u_Semaphore.cpp index a1df83b057..7144363c51 100644 --- a/src/s4u/s4u_Semaphore.cpp +++ b/src/s4u/s4u_Semaphore.cpp @@ -1,80 +1,86 @@ -/* Copyright (c) 2006-201. 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 "src/simix/smx_synchro_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_ = simgrid::simix::simcall([initial_capacity] { return SIMIX_sem_init(initial_capacity); }); -} - -Semaphore::~Semaphore() -{ - SIMIX_sem_destroy(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() { - simgrid::simix::simcall([this] { SIMIX_sem_release(sem_); }); + 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() +int Semaphore::get_capacity() const { - return simgrid::simix::simcall([this] { return SIMIX_sem_get_capacity(sem_); }); + return pimpl_->get_capacity(); } -int Semaphore::would_block() +bool Semaphore::would_block() const { - return simgrid::simix::simcall([this] { return SIMIX_sem_would_block(sem_); }); + 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 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 */ @@ -95,14 +101,14 @@ 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; + intrusive_ptr_release(sem); } /** @brief returns a boolean indicating if this semaphore would block at this very specific time @@ -110,7 +116,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(); }