DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
CallbackGate.hpp
Go to the documentation of this file.
1
28#ifndef DPDKLIBS_INCLUDE_DPDKLIBS_SENDER_CALLBACKGATE_HPP_
29#define DPDKLIBS_INCLUDE_DPDKLIBS_SENDER_CALLBACKGATE_HPP_
30
31#include <mutex>
32#include <shared_mutex>
33#include <utility>
34
36
37template<typename Owner>
39{
40public:
41 CallbackGate() = default;
42 CallbackGate(const CallbackGate&) = delete;
46
47 void arm(Owner* owner)
48 {
49 std::unique_lock lock(m_mutex);
50 m_owner = owner;
51 }
52
53 // Blocks until every in-flight dispatch() has returned, then disarms.
54 void tombstone()
55 {
56 std::unique_lock lock(m_mutex);
57 m_owner = nullptr;
58 }
59
60 bool armed() const
61 {
62 std::shared_lock lock(m_mutex);
63 return m_owner != nullptr;
64 }
65
66 // Invoke fn(*owner) under a shared lock if armed. Returns true if fn ran.
67 // The shared lock guarantees the owner outlives the call with respect to
68 // tombstone(). It does not serialize concurrent dispatches.
69 template<typename Fn>
70 bool dispatch(Fn&& fn)
71 {
72 std::shared_lock lock(m_mutex);
73 if (m_owner == nullptr) {
74 return false;
75 }
76 std::forward<Fn>(fn)(*m_owner);
77 return true;
78 }
79
80private:
81 mutable std::shared_mutex m_mutex;
82 Owner* m_owner = nullptr;
83};
84
85} // namespace dunedaq::dpdklibs::sender
86
87#endif // DPDKLIBS_INCLUDE_DPDKLIBS_SENDER_CALLBACKGATE_HPP_
CallbackGate & operator=(const CallbackGate &)=delete
CallbackGate & operator=(CallbackGate &&)=delete
CallbackGate(CallbackGate &&)=delete
CallbackGate(const CallbackGate &)=delete