2023-03-06 14:42:04 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
// Copyright The Music Player Daemon Project
|
2016-03-05 18:49:42 +01:00
|
|
|
|
|
|
|
#ifndef MPD_EVENT_MASK_MONITOR_HXX
|
|
|
|
#define MPD_EVENT_MASK_MONITOR_HXX
|
|
|
|
|
2020-12-01 16:25:11 +01:00
|
|
|
#include "InjectEvent.hxx"
|
2016-06-17 17:00:05 +02:00
|
|
|
#include "util/BindMethod.hxx"
|
2016-03-05 18:49:42 +01:00
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Manage a bit mask of events that have occurred. Every time the
|
|
|
|
* mask becomes non-zero, OnMask() is called in #EventLoop's thread.
|
|
|
|
*
|
|
|
|
* This class is thread-safe.
|
|
|
|
*/
|
2017-11-10 21:09:54 +01:00
|
|
|
class MaskMonitor final {
|
2020-12-01 16:25:11 +01:00
|
|
|
InjectEvent event;
|
2017-11-10 21:09:54 +01:00
|
|
|
|
2021-02-05 17:53:11 +01:00
|
|
|
using Callback = BoundMethod<void(unsigned) noexcept>;
|
2016-06-17 17:00:05 +02:00
|
|
|
const Callback callback;
|
|
|
|
|
2016-03-05 18:49:42 +01:00
|
|
|
std::atomic_uint pending_mask;
|
|
|
|
|
|
|
|
public:
|
2019-04-04 19:49:00 +02:00
|
|
|
MaskMonitor(EventLoop &_loop, Callback _callback) noexcept
|
2020-12-01 16:25:11 +01:00
|
|
|
:event(_loop, BIND_THIS_METHOD(RunDeferred)),
|
2017-11-10 21:09:54 +01:00
|
|
|
callback(_callback), pending_mask(0) {}
|
|
|
|
|
2019-04-04 19:48:28 +02:00
|
|
|
auto &GetEventLoop() const noexcept {
|
2020-12-01 16:25:11 +01:00
|
|
|
return event.GetEventLoop();
|
2017-11-10 21:09:54 +01:00
|
|
|
}
|
2016-03-05 18:49:42 +01:00
|
|
|
|
2019-04-04 19:49:00 +02:00
|
|
|
void Cancel() noexcept {
|
2020-12-01 16:25:11 +01:00
|
|
|
event.Cancel();
|
2017-11-10 21:09:54 +01:00
|
|
|
}
|
2016-03-05 18:49:42 +01:00
|
|
|
|
2019-04-04 19:49:00 +02:00
|
|
|
void OrMask(unsigned new_mask) noexcept;
|
2016-03-05 18:49:42 +01:00
|
|
|
|
|
|
|
protected:
|
2020-12-01 16:25:11 +01:00
|
|
|
/* InjectEvent callback */
|
2017-11-10 21:09:54 +01:00
|
|
|
void RunDeferred() noexcept;
|
2016-03-05 18:49:42 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|