SingleTimeExecutor.hpp
Go to the documentation of this file.
1 #include <utility>
2 
3 // @formatter:off
4 //
5 // Balau core C++ library
6 //
7 // Copyright (C) 2008 Bora Software (contact@borasoftware.com)
8 //
9 // Licensed under the Boost Software License - Version 1.0 - August 17th, 2003.
10 // See the LICENSE file for the full license text.
11 //
12 
18 
19 #ifndef COM_BORA_SOFTWARE__BALAU_CONCURRENT__SINGLE_TIME_EXECUTOR
20 #define COM_BORA_SOFTWARE__BALAU_CONCURRENT__SINGLE_TIME_EXECUTOR
21 
22 #include <atomic>
23 #include <functional>
24 #include <mutex>
25 
26 namespace Balau::Concurrent {
27 
38  public: explicit SingleTimeExecutor(std::function<void ()> code_)
39  : code(std::move(code_))
40  , destructorCode() {}
41 
49  public: SingleTimeExecutor(std::function<void ()> code_, std::function<void ()> destructorCode_)
50  : code(std::move(code_))
51  , destructorCode(std::move(destructorCode_)) {}
52 
56  public: ~SingleTimeExecutor() {
57  bool isExecuted = executed.load(std::memory_order_relaxed);
58  std::atomic_thread_fence(std::memory_order_acquire);
59 
60  if (isExecuted) {
61  std::lock_guard<std::mutex> lock(mutex);
62  isExecuted = executed.load(std::memory_order_relaxed);
63 
64  if (isExecuted && destructorCode) {
65  destructorCode();
66  }
67  }
68  }
69 
73  public: void operator () () {
74  bool isExecuted = executed.load(std::memory_order_relaxed);
75  std::atomic_thread_fence(std::memory_order_acquire);
76 
77  if (!isExecuted) {
78  std::lock_guard<std::mutex> lock(mutex);
79  isExecuted = executed.load(std::memory_order_relaxed);
80 
81  if (!isExecuted && code) {
82  code();
83  std::atomic_thread_fence(std::memory_order_release);
84  executed.store(true, std::memory_order_relaxed);
85  }
86  }
87  }
88 
99  public: bool hasExecuted() const {
100  return executed;
101  }
102 
108  public: std::mutex & getMutex() {
109  return mutex;
110  }
111 
113 
114  private: std::function<void ()> code;
115  private: std::function<void ()> destructorCode;
116  private: std::atomic<bool> executed;
117  private: std::mutex mutex;
118 };
119 
120 } // namespace Balau::Concurrent
121 
122 #endif // COM_BORA_SOFTWARE__BALAU_CONCURRENT__SINGLE_TIME_EXECUTOR
SingleTimeExecutor(std::function< void()> code_, std::function< void()> destructorCode_)
Create a single time executor with a code block and a destructor code block.
Definition: SingleTimeExecutor.hpp:49
STL namespace.
void operator()()
Execute the code a single time.
Definition: SingleTimeExecutor.hpp:73
Concurrency control classes.
Definition: CyclicBarrier.hpp:26
bool hasExecuted() const
Returns true if the code has been executed.
Definition: SingleTimeExecutor.hpp:99
Executes the supplied code block a single time.
Definition: SingleTimeExecutor.hpp:34
~SingleTimeExecutor()
Destroy the single time executor.
Definition: SingleTimeExecutor.hpp:56
std::mutex & getMutex()
Get the internal mutex in order to synchronise on external code.
Definition: SingleTimeExecutor.hpp:108
SingleTimeExecutor(std::function< void()> code_)
Create a single time executor with a code block.
Definition: SingleTimeExecutor.hpp:38