Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Make sure that the ID of a semaphore does not change over time
[simgrid.git] / src / kernel / activity / SemaphoreImpl.hpp
1 /* Copyright (c) 2019-2023. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #ifndef SIMGRID_KERNEL_ACTIVITY_SEMAPHOREIMPL_HPP
7 #define SIMGRID_KERNEL_ACTIVITY_SEMAPHOREIMPL_HPP
8
9 #include "simgrid/s4u/Semaphore.hpp"
10 #include "src/kernel/actor/ActorImpl.hpp"
11 #include "src/kernel/actor/SynchroObserver.hpp"
12
13 #include <atomic>
14 #include <boost/intrusive/list.hpp>
15
16 namespace simgrid::kernel::activity {
17
18 /** Semaphore Acquisition: the act / process of acquiring the semaphore.
19  *
20  * You can declare some interest on a semaphore without being blocked waiting if it's already empty.
21  * See the documentation of the MutexAcquisitionImpl for further details.
22  */
23 class XBT_PUBLIC SemAcquisitionImpl : public ActivityImpl_T<SemAcquisitionImpl> {
24   actor::ActorImpl* issuer_ = nullptr;
25   SemaphoreImpl* semaphore_ = nullptr;
26   bool granted_             = false;
27
28   friend SemaphoreImpl;
29   friend actor::SemaphoreAcquisitionObserver;
30
31 public:
32   SemAcquisitionImpl(actor::ActorImpl* issuer, SemaphoreImpl* sem) : issuer_(issuer), semaphore_(sem) {}
33   SemaphoreImplPtr get_semaphore() { return semaphore_; }
34   actor::ActorImpl* get_issuer() { return issuer_; }
35
36   bool test(actor::ActorImpl* issuer = nullptr) override { return granted_; }
37   void wait_for(actor::ActorImpl* issuer, double timeout) override;
38   void finish() override;
39   void cancel() override;
40   void set_exception(actor::ActorImpl* issuer) override
41   { /* nothing to do */
42   }
43 };
44
45 class XBT_PUBLIC SemaphoreImpl {
46   std::atomic_int_fast32_t refcount_{1};
47   s4u::Semaphore piface_;
48   unsigned int value_;
49   std::deque<SemAcquisitionImplPtr> ongoing_acquisitions_;
50
51   static unsigned next_id_;
52   const unsigned id_ = next_id_++;
53
54   friend SemAcquisitionImpl;
55   friend actor::SemaphoreObserver;
56
57 public:
58   explicit SemaphoreImpl(unsigned int value) : piface_(this), value_(value){};
59
60   SemaphoreImpl(SemaphoreImpl const&) = delete;
61   SemaphoreImpl& operator=(SemaphoreImpl const&) = delete;
62
63   SemAcquisitionImplPtr acquire_async(actor::ActorImpl* issuer);
64   void release();
65   bool would_block() const { return (value_ == 0); }
66
67   unsigned int get_capacity() const { return value_; }
68   bool is_used() const { return not ongoing_acquisitions_.empty(); }
69
70   friend void intrusive_ptr_add_ref(SemaphoreImpl* sem)
71   {
72     XBT_ATTRIB_UNUSED auto previous = sem->refcount_.fetch_add(1);
73     xbt_assert(previous != 0);
74   }
75   friend void intrusive_ptr_release(SemaphoreImpl* sem)
76   {
77     if (sem->refcount_.fetch_sub(1) == 1) {
78       xbt_assert(not sem->is_used(), "Cannot destroy semaphore since someone is still using it");
79       delete sem;
80     }
81   }
82   unsigned get_id() const { return id_; }
83   s4u::Semaphore& sem() { return piface_; }
84 };
85 } // namespace simgrid::kernel::activity
86
87 #endif /* SIMGRID_KERNEL_ACTIVITY_SEMAPHOREIMPL_HPP */