Logo AND Algorithmique Numérique Distribuée

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