util-event: Ensure thread safety

This commit is contained in:
Michael Fabian 'Xaymar' Dirks 2020-02-14 07:28:57 +01:00
parent 5d38ca8a74
commit fb5041a69d

View file

@ -20,11 +20,13 @@
#pragma once #pragma once
#include <functional> #include <functional>
#include <list> #include <list>
#include <mutex>
namespace util { namespace util {
template<typename... _args> template<typename... _args>
class event { class event {
std::list<std::function<void(_args...)>> listeners; std::list<std::function<void(_args...)>> listeners;
std::recursive_mutex lock;
std::function<void()> listen_cb; std::function<void()> listen_cb;
std::function<void()> silence_cb; std::function<void()> silence_cb;
@ -34,12 +36,14 @@ namespace util {
// Destructor // Destructor
virtual ~event() virtual ~event()
{ {
std::lock_guard<std::recursive_mutex> lock;
this->clear(); this->clear();
} }
// Add new listener. // Add new listener.
inline void add(std::function<void(_args...)> listener) inline void add(std::function<void(_args...)> listener)
{ {
std::lock_guard<std::recursive_mutex> lock;
if (listeners.size() == 0) { if (listeners.size() == 0) {
if (listen_cb) { if (listen_cb) {
listen_cb(); listen_cb();
@ -51,6 +55,7 @@ namespace util {
// Remove existing listener. // Remove existing listener.
inline void remove(std::function<void(_args...)> listener) inline void remove(std::function<void(_args...)> listener)
{ {
std::lock_guard<std::recursive_mutex> lock;
listeners.remove(listener); listeners.remove(listener);
if (listeners.size() == 0) { if (listeners.size() == 0) {
if (silence_cb) { if (silence_cb) {
@ -62,12 +67,14 @@ namespace util {
// Check if empty / no listeners. // Check if empty / no listeners.
inline bool empty() inline bool empty()
{ {
std::lock_guard<std::recursive_mutex> lock;
return listeners.empty(); return listeners.empty();
} }
// Remove all listeners. // Remove all listeners.
inline void clear() inline void clear()
{ {
std::lock_guard<std::recursive_mutex> lock;
listeners.clear(); listeners.clear();
if (silence_cb) { if (silence_cb) {
silence_cb(); silence_cb();
@ -80,6 +87,7 @@ namespace util {
template<typename... _largs> template<typename... _largs>
inline void operator()(_args... args) inline void operator()(_args... args)
{ {
std::lock_guard<std::recursive_mutex> lock;
for (auto& l : listeners) { for (auto& l : listeners) {
l(args...); l(args...);
} }
@ -108,11 +116,13 @@ namespace util {
public /* events */: public /* events */:
void set_listen_callback(std::function<void()> cb) void set_listen_callback(std::function<void()> cb)
{ {
std::lock_guard<std::recursive_mutex> lock;
this->listen_cb = cb; this->listen_cb = cb;
} }
void set_silence_callback(std::function<void()> cb) void set_silence_callback(std::function<void()> cb)
{ {
std::lock_guard<std::recursive_mutex> lock;
this->silence_cb = cb; this->silence_cb = cb;
} }
}; };