distortos  v0.6.0
object-oriented C++ RTOS for microcontrollers
callOnce.hpp
Go to the documentation of this file.
1 
12 #ifndef INCLUDE_DISTORTOS_CALLONCE_HPP_
13 #define INCLUDE_DISTORTOS_CALLONCE_HPP_
14 
16 
17 #include "distortos/OnceFlag.hpp"
18 
19 #include "estd/invoke.hpp"
20 
21 #include <mutex>
22 
23 namespace distortos
24 {
25 
44 template<typename Function, typename... Args>
45 void callOnce(OnceFlag& onceFlag, Function&& function, Args&&... args)
46 {
48 
49  if (onceFlag.done_ == true)
50  return;
51 
52  const std::lock_guard<distortos::Mutex> lockGuard {onceFlag.mutex_};
53 
54  if (onceFlag.done_ == true)
55  return;
56 
57  estd::invoke(std::forward<Function>(function), std::forward<Args>(args)...);
58  onceFlag.done_ = true;
59 }
60 
61 } // namespace distortos
62 
63 #endif // INCLUDE_DISTORTOS_CALLONCE_HPP_
CHECK_FUNCTION_CONTEXT() macro.
Mutex mutex_
internal mutex used by callOnce()
Definition: OnceFlag.hpp:50
OnceFlag class header.
void callOnce(OnceFlag &onceFlag, Function &&function, Args &&... args)
Executes the callable object exactly once, even if called from multiple threads.
Definition: callOnce.hpp:45
Top-level namespace of distortos project.
#define CHECK_FUNCTION_CONTEXT()
Macro used to check whether the function is executed from thread context.
Definition: CHECK_FUNCTION_CONTEXT.hpp:29
volatile bool done_
tells whether any function was already called for this object (true) or not (false) ...
Definition: OnceFlag.hpp:53
auto invoke(Function &&function, Args &&... args) -> decltype(internal::invoke(std::forward< Function >(function), std::forward< Args >(args)...))
Invokes callable object in an appropriate way.
Definition: invoke.hpp:139
OnceFlag is a helper class for callOnce().
Definition: OnceFlag.hpp:29
invoke() header