Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
uniformization: call post that calls finish
[simgrid.git] / src / kernel / activity / BarrierImpl.hpp
1 /* Copyright (c) 2012-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_BARRIER_HPP
7 #define SIMGRID_KERNEL_ACTIVITY_BARRIER_HPP
8
9 #include "simgrid/s4u/Barrier.hpp"
10 #include "src/kernel/activity/ActivityImpl.hpp"
11 #include "src/kernel/actor/ActorImpl.hpp"
12 #include "src/kernel/actor/SynchroObserver.hpp"
13 #include "xbt/string.hpp"
14
15 namespace simgrid::kernel::activity {
16 /** Barrier Acquisition: the act / process of acquiring the barrier.
17  *
18  * This is the asynchronous activity associated to Barriers. See the doc of MutexImpl for more details on the rationnal.
19  */
20 class XBT_PUBLIC BarrierAcquisitionImpl : public ActivityImpl_T<BarrierAcquisitionImpl> {
21   actor::ActorImpl* issuer_ = nullptr;
22   BarrierImpl* barrier_     = nullptr;
23   bool granted_             = false;
24
25   friend actor::BarrierObserver;
26   friend BarrierImpl;
27
28 public:
29   BarrierAcquisitionImpl(actor::ActorImpl* issuer, BarrierImpl* bar) : issuer_(issuer), barrier_(bar) {}
30   BarrierImplPtr get_barrier() { return barrier_; }
31   actor::ActorImpl* get_issuer() { return issuer_; }
32
33   bool test(actor::ActorImpl* issuer = nullptr) override;
34   void wait_for(actor::ActorImpl* issuer, double timeout) override;
35   void post() override;
36   void finish() override;
37   void set_exception(actor::ActorImpl* issuer) override
38   { /* nothing to do */
39   }
40 };
41
42 class XBT_PUBLIC BarrierImpl {
43   std::atomic_int_fast32_t refcount_{1};
44   s4u::Barrier piface_;
45   unsigned int expected_actors_;
46   std::deque<BarrierAcquisitionImplPtr> ongoing_acquisitions_;
47   static unsigned next_id_;
48   unsigned id_ = next_id_++;
49
50   friend BarrierAcquisitionImpl;
51   friend s4u::Barrier;
52
53 public:
54   explicit BarrierImpl(int expected_actors) : piface_(this), expected_actors_(expected_actors) {}
55   BarrierImpl(BarrierImpl const&) = delete;
56   BarrierImpl& operator=(BarrierImpl const&) = delete;
57
58   BarrierAcquisitionImplPtr acquire_async(actor::ActorImpl* issuer);
59   unsigned get_id() const { return id_; }
60
61   friend void intrusive_ptr_add_ref(BarrierImpl* barrier)
62   {
63     XBT_ATTRIB_UNUSED auto previous = barrier->refcount_.fetch_add(1);
64     xbt_assert(previous != 0);
65   }
66
67   friend void intrusive_ptr_release(BarrierImpl* barrier)
68   {
69     if (barrier->refcount_.fetch_sub(1) == 1)
70       delete barrier;
71   }
72
73   s4u::Barrier& get_iface() { return piface_; }
74
75   std::string to_string() const
76   {
77     return xbt::string_printf("Barrier %u: %zu of %u", id_, ongoing_acquisitions_.size(), expected_actors_);
78   }
79 };
80 } // namespace simgrid::kernel::activity
81 #endif