Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Implement SemaphoreImpl::acquire_async (timeouts are allowed)
[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
15 namespace simgrid {
16 namespace kernel {
17 namespace activity {
18
19 /** Semaphore Acquisition: the act / process of acquiring the semaphore.
20  *
21  * You can declare some interest on a semaphore without being blocked waiting if it's already empty.
22  * See the documentation of the MutexAcquisitionImpl for further details.
23  */
24 class XBT_PUBLIC SemAcquisitionImpl : public ActivityImpl_T<SemAcquisitionImpl> {
25   actor::ActorImpl* issuer_ = nullptr;
26   SemaphoreImpl* semaphore_ = nullptr;
27   bool granted_             = false;
28
29   friend SemaphoreImpl;
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 post() override;
39   void finish() 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> sleeping_; /* ongoing acquisitions */
50
51   friend SemAcquisitionImpl;
52
53 public:
54   explicit SemaphoreImpl(unsigned int value) : piface_(this), value_(value){};
55
56   SemaphoreImpl(SemaphoreImpl const&) = delete;
57   SemaphoreImpl& operator=(SemaphoreImpl const&) = delete;
58
59   SemAcquisitionImplPtr acquire_async(actor::ActorImpl* issuer);
60   void release();
61   bool would_block() const { return (value_ == 0); }
62
63   unsigned int get_capacity() const { return value_; }
64   bool is_used() const { return not sleeping_.empty(); }
65
66   friend void intrusive_ptr_add_ref(SemaphoreImpl* sem)
67   {
68     XBT_ATTRIB_UNUSED auto previous = sem->refcount_.fetch_add(1);
69     xbt_assert(previous != 0);
70   }
71   friend void intrusive_ptr_release(SemaphoreImpl* sem)
72   {
73     if (sem->refcount_.fetch_sub(1) == 1) {
74       xbt_assert(not sem->is_used(), "Cannot destroy semaphore since someone is still using it");
75       delete sem;
76     }
77   }
78
79   s4u::Semaphore& sem() { return piface_; }
80 };
81 } // namespace activity
82 } // namespace kernel
83 } // namespace simgrid
84
85 #endif /* SIMGRID_KERNEL_ACTIVITY_SEMAPHOREIMPL_HPP */