Logo AND Algorithmique Numérique Distribuée

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