X-Git-Url: http://bilbo.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/361258ab0fbf44b907a966b0ad83c46fe7353925..6e80e277a2e65be7caefd0e04e4954c4fc26c2d8:/src/s4u/s4u_Semaphore.cpp diff --git a/src/s4u/s4u_Semaphore.cpp b/src/s4u/s4u_Semaphore.cpp index 191094aeb9..7144363c51 100644 --- a/src/s4u/s4u_Semaphore.cpp +++ b/src/s4u/s4u_Semaphore.cpp @@ -1,56 +1,122 @@ -/* 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/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 { +namespace simgrid::s4u { -Semaphore::Semaphore(unsigned int initial_capacity) +SemaphorePtr Semaphore::create(unsigned int initial_capacity) { - sem_ = simgrid::simix::simcall([initial_capacity] { return SIMIX_sem_init(initial_capacity); }); + auto* sem = new kernel::activity::SemaphoreImpl(initial_capacity); + return SemaphorePtr(&sem->sem(), false); } -Semaphore::~Semaphore() +void Semaphore::acquire() { - SIMIX_sem_destroy(sem_); + acquire_timeout(-1); } -SemaphorePtr Semaphore::create(unsigned int initial_capacity) +bool Semaphore::acquire_timeout(double timeout) { - return SemaphorePtr(new Semaphore(initial_capacity)); + 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::acquire() +void Semaphore::release() { - simcall_sem_acquire(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); } -void Semaphore::release() +int Semaphore::get_capacity() const { - simgrid::simix::simcall([this] { SIMIX_sem_release(sem_); }); + return pimpl_->get_capacity(); } -void intrusive_ptr_add_ref(Semaphore* sem) +bool Semaphore::would_block() const { - xbt_assert(sem); - sem->refcount_.fetch_add(1, std::memory_order_relaxed); + return pimpl_->would_block(); } -void intrusive_ptr_release(Semaphore* sem) +/* refcounting of the intrusive_ptr is delegated to the implementation object */ +void intrusive_ptr_add_ref(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_add_ref(sem->pimpl_); +} + +void intrusive_ptr_release(const Semaphore* 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 simgrid::s4u::Semaphore::create(initial_value).detach(); +} + +/** @brief locks on a semaphore object */ +void sg_sem_acquire(sg_sem_t sem) +{ + sem->acquire(); } +/** @brief locks on a semaphore object up until the provided timeout expires */ +int sg_sem_acquire_timeout(sg_sem_t sem, double timeout) +{ + return sem->acquire_timeout(timeout); +} + +/** @brief releases the semaphore object */ +void sg_sem_release(sg_sem_t sem) +{ + sem->release(); } + +int sg_sem_get_capacity(const_sg_sem_t sem) +{ + return sem->get_capacity(); +} + +void sg_sem_destroy(const_sg_sem_t sem) +{ + intrusive_ptr_release(sem); +} + +/** @brief returns a boolean indicating if this semaphore would block at this very specific time + * + * 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(const_sg_sem_t sem) +{ + return sem->would_block(); }