Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'add_remaining_comm_sync_bindings' into 'master'
[simgrid.git] / src / kernel / activity / SemaphoreImpl.hpp
1 /* Copyright (c) 2019-2022. 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 <atomic>
10 #include <boost/intrusive/list.hpp>
11
12 #include "simgrid/s4u/Semaphore.hpp"
13 #include "src/kernel/actor/ActorImpl.hpp"
14 #include "src/kernel/actor/SynchroObserver.hpp"
15
16 namespace simgrid {
17 namespace kernel {
18 namespace activity {
19
20 /** Semaphore Acquisition: the act / process of acquiring the semaphore.
21  *
22  * You can declare some interest on a semaphore without being blocked waiting if it's already empty.
23  * See the documentation of the MutexAcquisitionImpl for further details.
24  */
25 class XBT_PUBLIC SemAcquisitionImpl : public ActivityImpl_T<SemAcquisitionImpl> {
26   actor::ActorImpl* issuer_ = nullptr;
27   SemaphoreImpl* semaphore_ = nullptr;
28   bool granted_             = false;
29
30   friend SemaphoreImpl;
31   friend actor::SemaphoreAcquisitionObserver;
32
33 public:
34   SemAcquisitionImpl(actor::ActorImpl* issuer, SemaphoreImpl* sem) : issuer_(issuer), semaphore_(sem) {}
35   SemaphoreImplPtr get_semaphore() { return semaphore_; }
36   actor::ActorImpl* get_issuer() { return issuer_; }
37
38   bool test(actor::ActorImpl* issuer = nullptr) override { return granted_; }
39   void wait_for(actor::ActorImpl* issuer, double timeout) override;
40   void post() override;
41   void finish() override;
42   void cancel() override;
43   void set_exception(actor::ActorImpl* issuer) override
44   { /* nothing to do */
45   }
46 };
47
48 class XBT_PUBLIC SemaphoreImpl {
49   std::atomic_int_fast32_t refcount_{1};
50   s4u::Semaphore piface_;
51   unsigned int value_;
52   std::deque<SemAcquisitionImplPtr> ongoing_acquisitions_;
53
54   static unsigned next_id_;
55   unsigned id_ = next_id_++;
56
57   friend SemAcquisitionImpl;
58   friend actor::SemaphoreObserver;
59
60 public:
61   explicit SemaphoreImpl(unsigned int value) : piface_(this), value_(value){};
62
63   SemaphoreImpl(SemaphoreImpl const&) = delete;
64   SemaphoreImpl& operator=(SemaphoreImpl const&) = delete;
65
66   SemAcquisitionImplPtr acquire_async(actor::ActorImpl* issuer);
67   void release();
68   bool would_block() const { return (value_ == 0); }
69
70   unsigned int get_capacity() const { return value_; }
71   bool is_used() const { return not ongoing_acquisitions_.empty(); }
72
73   friend void intrusive_ptr_add_ref(SemaphoreImpl* sem)
74   {
75     XBT_ATTRIB_UNUSED auto previous = sem->refcount_.fetch_add(1);
76     xbt_assert(previous != 0);
77   }
78   friend void intrusive_ptr_release(SemaphoreImpl* sem)
79   {
80     if (sem->refcount_.fetch_sub(1) == 1) {
81       xbt_assert(not sem->is_used(), "Cannot destroy semaphore since someone is still using it");
82       delete sem;
83     }
84   }
85   unsigned get_id() const { return id_; }
86   s4u::Semaphore& sem() { return piface_; }
87 };
88 } // namespace activity
89 } // namespace kernel
90 } // namespace simgrid
91
92 #endif /* SIMGRID_KERNEL_ACTIVITY_SEMAPHOREIMPL_HPP */