MaskMonitor.hxx 1.66 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2019 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifndef MPD_EVENT_MASK_MONITOR_HXX
#define MPD_EVENT_MASK_MONITOR_HXX

23
#include "DeferEvent.hxx"
24
#include "util/BindMethod.hxx"
25 26 27 28 29 30 31 32 33

#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.
 */
34 35 36
class MaskMonitor final {
	DeferEvent defer;

Max Kellermann's avatar
Max Kellermann committed
37
	typedef BoundMethod<void(unsigned) noexcept> Callback;
38 39
	const Callback callback;

40 41 42
	std::atomic_uint pending_mask;

public:
43
	MaskMonitor(EventLoop &_loop, Callback _callback) noexcept
44 45 46
		:defer(_loop, BIND_THIS_METHOD(RunDeferred)),
		 callback(_callback), pending_mask(0) {}

47
	auto &GetEventLoop() const noexcept {
48 49
		return defer.GetEventLoop();
	}
50

51
	void Cancel() noexcept {
52 53
		defer.Cancel();
	}
54

55
	void OrMask(unsigned new_mask) noexcept;
56 57

protected:
58 59
	/* DeferEvent callback */
	void RunDeferred() noexcept;
60 61 62
};

#endif