Lumiera  0.pre.03
»edit your freedom«
sync-barrier.hpp
Go to the documentation of this file.
1 /*
2  SYNC-BARRIER.hpp - N-fold synchronisation point with yield-wait
3 
4  Copyright (C)
5  2023, Hermann Vosseler <Ichthyostega@web.de>
6 
7   **Lumiera** is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published by the
9   Free Software Foundation; either version 2 of the License, or (at your
10   option) any later version. See the file COPYING for further details.
11 
12 */
13 
14 
40 #ifndef LIB_SYNC_BARRIER_H
41 #define LIB_SYNC_BARRIER_H
42 
43 
44 #include "lib/error.hpp"
45 #include "lib/nocopy.hpp"
46 
47 #include <thread>
48 #include <atomic>
49 
50 
51 namespace lib {
52 
53 
66  {
67  std::atomic_int latch_;
68 
69  public:
71  explicit
72  SyncBarrier (uint nFold =2)
73  : latch_{int(nFold)}
74  {
75  REQUIRE (nFold >= 2, "Pointless to sync less than two participants.");
76  ENSURE (nFold < 100'000, "Danger territory.... sync 100k Threads??");
77  }
78 
79  void
80  sync()
81  {
82  size_t level = latch_.fetch_add(-1, std::memory_order_acq_rel);
83  if (1 < level)
84  do std::this_thread::yield();
85  while (0 < latch_.load (std::memory_order_relaxed));
86  else
87  latch_.store (0, std::memory_order_relaxed);
88  } // prevent spurious calls from wrapping
89  };
90 
91 
92 
93 } // namespace lib
94 #endif /*LIB_SYNC_BARRIER_H*/
Any copy and copy construction prohibited.
Definition: nocopy.hpp:37
Implementation namespace for support and library code.
Mix-Ins to allow or prohibit various degrees of copying and cloning.
Lumiera error handling (C++ interface).
A one time N-fold mutual synchronisation barrier.
SyncBarrier(uint nFold=2)