2016-06-17 18:20:11 +02:00
|
|
|
/*
|
2022-07-14 17:58:12 +02:00
|
|
|
* Copyright 2003-2022 The Music Player Daemon Project
|
2016-06-17 18:20:11 +02:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2017-09-21 22:40:25 +02:00
|
|
|
#ifndef MPD_DEFER_EVENT_HXX
|
|
|
|
#define MPD_DEFER_EVENT_HXX
|
2016-06-17 18:20:11 +02:00
|
|
|
|
|
|
|
#include "util/BindMethod.hxx"
|
2020-12-01 16:56:58 +01:00
|
|
|
#include "util/IntrusiveList.hxx"
|
2017-11-12 17:34:06 +01:00
|
|
|
|
|
|
|
class EventLoop;
|
|
|
|
|
2016-06-17 18:20:11 +02:00
|
|
|
/**
|
2020-12-01 16:25:11 +01:00
|
|
|
* Defer execution until the next event loop iteration. Use this to
|
|
|
|
* move calls out of the current stack frame, to avoid surprising side
|
|
|
|
* effects for callers up in the call chain.
|
2016-06-17 18:20:11 +02:00
|
|
|
*
|
2020-12-01 16:25:11 +01:00
|
|
|
* This class is not thread-safe, all methods must be called from the
|
|
|
|
* thread that runs the #EventLoop.
|
2016-06-17 18:20:11 +02:00
|
|
|
*/
|
2020-12-01 16:56:58 +01:00
|
|
|
class DeferEvent final : AutoUnlinkIntrusiveListHook
|
2020-10-14 14:02:11 +02:00
|
|
|
{
|
2017-11-12 17:34:06 +01:00
|
|
|
friend class EventLoop;
|
2022-06-08 10:31:09 +02:00
|
|
|
friend struct IntrusiveListBaseHookTraits<DeferEvent>;
|
2017-11-12 17:34:06 +01:00
|
|
|
|
|
|
|
EventLoop &loop;
|
|
|
|
|
2020-12-01 16:25:54 +01:00
|
|
|
using Callback = BoundMethod<void() noexcept>;
|
2016-06-17 18:20:11 +02:00
|
|
|
const Callback callback;
|
|
|
|
|
|
|
|
public:
|
2017-11-12 17:34:06 +01:00
|
|
|
DeferEvent(EventLoop &_loop, Callback _callback) noexcept
|
|
|
|
:loop(_loop), callback(_callback) {}
|
2016-06-17 18:20:11 +02:00
|
|
|
|
2020-12-01 16:26:30 +01:00
|
|
|
DeferEvent(const DeferEvent &) = delete;
|
|
|
|
DeferEvent &operator=(const DeferEvent &) = delete;
|
|
|
|
|
2020-12-01 16:56:58 +01:00
|
|
|
auto &GetEventLoop() const noexcept {
|
|
|
|
return loop;
|
2017-11-12 17:34:06 +01:00
|
|
|
}
|
|
|
|
|
2020-12-01 16:56:58 +01:00
|
|
|
bool IsPending() const noexcept {
|
|
|
|
return is_linked();
|
2017-11-12 17:34:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Schedule() noexcept;
|
2016-06-17 18:20:11 +02:00
|
|
|
|
2020-12-01 17:04:14 +01:00
|
|
|
/**
|
|
|
|
* Schedule this event, but only after the #EventLoop is idle,
|
|
|
|
* i.e. before going to sleep.
|
|
|
|
*/
|
|
|
|
void ScheduleIdle() noexcept;
|
|
|
|
|
2020-12-01 16:56:58 +01:00
|
|
|
void Cancel() noexcept {
|
|
|
|
if (IsPending())
|
|
|
|
unlink();
|
2017-11-12 17:34:06 +01:00
|
|
|
}
|
|
|
|
|
2020-12-01 16:56:58 +01:00
|
|
|
private:
|
|
|
|
void Run() noexcept {
|
2016-06-17 18:20:11 +02:00
|
|
|
callback();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|