8#include "unfair_recursive_mutex.hpp"
27template<
typename Result,
typename... Args>
30 static_assert(std::is_same_v<Result,void>,
"Result of a notifier must be void.");
44 auto lock = std::scoped_lock(_mutex);
46 ttlet i =
std::find_if(_callbacks.cbegin(), _callbacks.cend(), [&callback_ptr](ttlet &item) {
47 return item.lock() == callback_ptr;
50 if (i == _callbacks.cend()) {
51 _callbacks.emplace_back(callback_ptr);
63 template<
typename Callback>
66 auto callback_ptr = std::make_shared<callback_type>(
std::forward<
decltype(callback)>(callback));
68 auto lock = std::scoped_lock(_mutex);
69 _callbacks.emplace_back(callback_ptr);
78 auto lock = std::scoped_lock(_mutex);
80 ttlet new_end =
std::remove_if(_callbacks.begin(), _callbacks.end(), [&callback_ptr](ttlet &item) {
81 return item.expired() || item.lock() == callback_ptr;
83 _callbacks.erase(new_end, _callbacks.cend());
91 auto lock = std::scoped_lock(_mutex);
92 tt_assert(!_executing_callbacks);
93 _executing_callbacks =
true;
95 auto i = _callbacks.begin();
96 while (i != _callbacks.end()) {
97 if (
auto callback = i->lock()) {
102 i = _callbacks.erase(i);
105 _executing_callbacks =
false;
110 mutable bool _executing_callbacks =
false;
Definition notifier.hpp:17
void unsubscribe(callback_ptr_type const &callback_ptr) noexcept
Remove a callback from the notifier.
Definition notifier.hpp:76
void operator()(Args const &...args) const noexcept
Call the subscribed callbacks with the given arguments.
Definition notifier.hpp:89
void subscribe_ptr(callback_ptr_type const &callback_ptr) noexcept
Add a callback to the notifier.
Definition notifier.hpp:42
callback_ptr_type subscribe(Callback &&callback) noexcept
Add a callback to the notifier.
Definition notifier.hpp:64
An unfair recursive-mutex This is a fast implementation of a recursive-mutex which does not fairly ar...
Definition unfair_recursive_mutex.hpp:32