Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of framagit.org:simgrid/simgrid
[simgrid.git] / include / simgrid / s4u / Barrier.hpp
1 /* Copyright (c) 2018-2019. 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_S4U_BARRIER_HPP
7 #define SIMGRID_S4U_BARRIER_HPP
8
9 #include <simgrid/barrier.h>
10 #include <simgrid/chrono.hpp>
11 #include <simgrid/forward.h>
12 #include <simgrid/s4u/ConditionVariable.hpp>
13 #include <simgrid/s4u/Mutex.hpp>
14
15 #include <atomic>
16 #include <future>
17
18 namespace simgrid {
19 namespace s4u {
20
21 class XBT_PUBLIC Barrier {
22 private:
23   MutexPtr mutex_            = Mutex::create();
24   ConditionVariablePtr cond_ = ConditionVariable::create();
25   unsigned int expected_actors_;
26   unsigned int arrived_actors_ = 0;
27
28   /* refcounting */
29   std::atomic_int_fast32_t refcount_{0};
30
31 public:
32   /** Creates a barrier for the given amount of actors */
33   explicit Barrier(unsigned int expected_processes) : expected_actors_(expected_processes) {}
34 #ifndef DOXYGEN
35   ~Barrier()              = default;
36   Barrier(Barrier const&) = delete;
37   Barrier& operator=(Barrier const&) = delete;
38 #endif
39
40   /** Creates a barrier for the given amount of actors */
41   static BarrierPtr create(unsigned int expected_actors);
42   /** Blocks into the barrier. Every waiting actors will be unlocked once the expected amount of actors reaches the barrier */
43   int wait();
44
45 #ifndef DOXYGEN
46   /* refcounting */
47   friend XBT_PUBLIC void intrusive_ptr_add_ref(Barrier* barrier);
48   friend XBT_PUBLIC void intrusive_ptr_release(Barrier* barrier);
49 #endif
50 };
51 } // namespace s4u
52 } // namespace simgrid
53
54 #endif