X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/45ab34376c032c03f722da50077725c60a6b69b9..63f8062313404439d1fc84eaa80c182888825a0e:/src/kernel/activity/SemaphoreImpl.hpp?ds=sidebyside diff --git a/src/kernel/activity/SemaphoreImpl.hpp b/src/kernel/activity/SemaphoreImpl.hpp index e6dc57d6bb..e7b21d2b0c 100644 --- a/src/kernel/activity/SemaphoreImpl.hpp +++ b/src/kernel/activity/SemaphoreImpl.hpp @@ -1,39 +1,71 @@ -/* Copyright (c) 2019. The SimGrid Team. All rights reserved. */ +/* Copyright (c) 2019-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. */ -#ifndef SIMGRID_KERNEL_ACTIVITY_SEMAPHOREIMPL_HPP_ -#define SIMGRID_KERNEL_ACTIVITY_SEMAPHOREIMPL_HPP_ +#ifndef SIMGRID_KERNEL_ACTIVITY_SEMAPHOREIMPL_HPP +#define SIMGRID_KERNEL_ACTIVITY_SEMAPHOREIMPL_HPP + +#include "simgrid/s4u/Semaphore.hpp" +#include "src/kernel/actor/ActorImpl.hpp" +#include "src/kernel/actor/SynchroObserver.hpp" #include #include -#include "simgrid/s4u/Semaphore.hpp" -#include "src/kernel/actor/ActorImpl.hpp" +namespace simgrid::kernel::activity { + +/** Semaphore Acquisition: the act / process of acquiring the semaphore. + * + * You can declare some interest on a semaphore without being blocked waiting if it's already empty. + * See the documentation of the MutexAcquisitionImpl for further details. + */ +class XBT_PUBLIC SemAcquisitionImpl : public ActivityImpl_T { + actor::ActorImpl* issuer_ = nullptr; + SemaphoreImpl* semaphore_ = nullptr; + bool granted_ = false; + + friend SemaphoreImpl; + friend actor::SemaphoreAcquisitionObserver; -namespace simgrid { -namespace kernel { -namespace activity { +public: + SemAcquisitionImpl(actor::ActorImpl* issuer, SemaphoreImpl* sem) : issuer_(issuer), semaphore_(sem) {} + SemaphoreImplPtr get_semaphore() { return semaphore_; } + actor::ActorImpl* get_issuer() { return issuer_; } + + bool test(actor::ActorImpl* issuer = nullptr) override { return granted_; } + void wait_for(actor::ActorImpl* issuer, double timeout) override; + void finish() override; + void cancel() override; + void set_exception(actor::ActorImpl* issuer) override + { /* nothing to do */ + } +}; class XBT_PUBLIC SemaphoreImpl { std::atomic_int_fast32_t refcount_{1}; + s4u::Semaphore piface_; unsigned int value_; + std::deque ongoing_acquisitions_; -public: - actor::SynchroList sleeping_; /* list of sleeping actors*/ + static unsigned next_id_; + const unsigned id_ = next_id_++; + + friend SemAcquisitionImpl; + friend actor::SemaphoreObserver; - explicit SemaphoreImpl(unsigned int value) : value_(value){}; - ~SemaphoreImpl() = default; +public: + explicit SemaphoreImpl(unsigned int value) : piface_(this), value_(value){}; SemaphoreImpl(SemaphoreImpl const&) = delete; SemaphoreImpl& operator=(SemaphoreImpl const&) = delete; - void acquire(actor::ActorImpl* issuer, double timeout); + SemAcquisitionImplPtr acquire_async(actor::ActorImpl* issuer); void release(); - bool would_block() { return (value_ == 0); } + bool would_block() const { return (value_ == 0); } - unsigned int get_capacity() { return value_; } + unsigned int get_capacity() const { return value_; } + bool is_used() const { return not ongoing_acquisitions_.empty(); } friend void intrusive_ptr_add_ref(SemaphoreImpl* sem) { @@ -42,12 +74,14 @@ public: } friend void intrusive_ptr_release(SemaphoreImpl* sem) { - if (sem->refcount_.fetch_sub(1) == 1) + if (sem->refcount_.fetch_sub(1) == 1) { + xbt_assert(not sem->is_used(), "Cannot destroy semaphore since someone is still using it"); delete sem; + } } + unsigned get_id() const { return id_; } + s4u::Semaphore& sem() { return piface_; } }; -} // namespace activity -} // namespace kernel -} // namespace simgrid +} // namespace simgrid::kernel::activity -#endif /* SIMGRID_KERNEL_ACTIVITY_SEMAPHOREIMPL_HPP_ */ +#endif /* SIMGRID_KERNEL_ACTIVITY_SEMAPHOREIMPL_HPP */