event/DeferredMonitor: new class wrapping g_idle_add()
This commit is contained in:
parent
123dd5fe2d
commit
355dd5cb24
@ -297,6 +297,7 @@ libevent_a_SOURCES = \
|
|||||||
src/event/WakeFD.hxx \
|
src/event/WakeFD.hxx \
|
||||||
src/event/SignalMonitor.hxx src/event/SignalMonitor.cxx \
|
src/event/SignalMonitor.hxx src/event/SignalMonitor.cxx \
|
||||||
src/event/TimeoutMonitor.hxx src/event/TimeoutMonitor.cxx \
|
src/event/TimeoutMonitor.hxx src/event/TimeoutMonitor.cxx \
|
||||||
|
src/event/DeferredMonitor.hxx src/event/DeferredMonitor.cxx \
|
||||||
src/event/SocketMonitor.cxx src/event/SocketMonitor.hxx \
|
src/event/SocketMonitor.cxx src/event/SocketMonitor.hxx \
|
||||||
src/event/BufferedSocket.cxx src/event/BufferedSocket.hxx \
|
src/event/BufferedSocket.cxx src/event/BufferedSocket.hxx \
|
||||||
src/event/FullyBufferedSocket.cxx src/event/FullyBufferedSocket.hxx \
|
src/event/FullyBufferedSocket.cxx src/event/FullyBufferedSocket.hxx \
|
||||||
|
@ -19,6 +19,8 @@
|
|||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "GlobalEvents.hxx"
|
#include "GlobalEvents.hxx"
|
||||||
|
#include "util/Manual.hxx"
|
||||||
|
#include "event/DeferredMonitor.hxx"
|
||||||
#include "gcc.h"
|
#include "gcc.h"
|
||||||
|
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
@ -30,7 +32,13 @@
|
|||||||
#define G_LOG_DOMAIN "global_events"
|
#define G_LOG_DOMAIN "global_events"
|
||||||
|
|
||||||
namespace GlobalEvents {
|
namespace GlobalEvents {
|
||||||
static guint source_id;
|
class Monitor final : public DeferredMonitor {
|
||||||
|
protected:
|
||||||
|
virtual void Run() override;
|
||||||
|
};
|
||||||
|
|
||||||
|
static Manual<Monitor> monitor;
|
||||||
|
|
||||||
static std::atomic_uint flags;
|
static std::atomic_uint flags;
|
||||||
static Handler handlers[MAX];
|
static Handler handlers[MAX];
|
||||||
}
|
}
|
||||||
@ -47,29 +55,27 @@ InvokeGlobalEvent(GlobalEvents::Event event)
|
|||||||
GlobalEvents::handlers[event]();
|
GlobalEvents::handlers[event]();
|
||||||
}
|
}
|
||||||
|
|
||||||
static gboolean
|
void
|
||||||
GlobalEventCallback(gcc_unused gpointer data)
|
GlobalEvents::Monitor::Run()
|
||||||
{
|
{
|
||||||
const unsigned flags = GlobalEvents::flags.exchange(0);
|
const unsigned f = flags.exchange(0);
|
||||||
|
|
||||||
for (unsigned i = 0; i < GlobalEvents::MAX; ++i)
|
for (unsigned i = 0; i < MAX; ++i)
|
||||||
if (flags & (1u << i))
|
if (f & (1u << i))
|
||||||
/* invoke the event handler */
|
/* invoke the event handler */
|
||||||
InvokeGlobalEvent(GlobalEvents::Event(i));
|
InvokeGlobalEvent(Event(i));
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
GlobalEvents::Initialize()
|
GlobalEvents::Initialize()
|
||||||
{
|
{
|
||||||
|
monitor.Construct();
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
GlobalEvents::Deinitialize()
|
GlobalEvents::Deinitialize()
|
||||||
{
|
{
|
||||||
if (source_id != 0)
|
monitor.Destruct();
|
||||||
g_source_remove(source_id);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@ -88,5 +94,5 @@ GlobalEvents::Emit(Event event)
|
|||||||
|
|
||||||
const unsigned mask = 1u << unsigned(event);
|
const unsigned mask = 1u << unsigned(event);
|
||||||
if (GlobalEvents::flags.fetch_or(mask) == 0)
|
if (GlobalEvents::flags.fetch_or(mask) == 0)
|
||||||
source_id = g_idle_add(GlobalEventCallback, nullptr);
|
monitor->Schedule();
|
||||||
}
|
}
|
||||||
|
55
src/event/DeferredMonitor.cxx
Normal file
55
src/event/DeferredMonitor.cxx
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2003-2013 The Music Player Daemon Project
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "config.h"
|
||||||
|
#include "DeferredMonitor.hxx"
|
||||||
|
#include "Loop.hxx"
|
||||||
|
|
||||||
|
void
|
||||||
|
DeferredMonitor::Cancel()
|
||||||
|
{
|
||||||
|
const auto id = source_id.exchange(0);
|
||||||
|
if (id != 0)
|
||||||
|
g_source_remove(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
DeferredMonitor::Schedule()
|
||||||
|
{
|
||||||
|
const unsigned id = g_idle_add(Callback, this);
|
||||||
|
const auto old_id = source_id.exchange(id);
|
||||||
|
if (old_id != 0)
|
||||||
|
g_source_remove(old_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
DeferredMonitor::DoRun()
|
||||||
|
{
|
||||||
|
const auto id = source_id.exchange(0);
|
||||||
|
if (id != 0)
|
||||||
|
Run();
|
||||||
|
}
|
||||||
|
|
||||||
|
gboolean
|
||||||
|
DeferredMonitor::Callback(gpointer data)
|
||||||
|
{
|
||||||
|
DeferredMonitor &monitor = *(DeferredMonitor *)data;
|
||||||
|
monitor.Run();
|
||||||
|
return false;
|
||||||
|
}
|
54
src/event/DeferredMonitor.hxx
Normal file
54
src/event/DeferredMonitor.hxx
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2003-2013 The Music Player Daemon Project
|
||||||
|
* 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_SOCKET_DEFERRED_MONITOR_HXX
|
||||||
|
#define MPD_SOCKET_DEFERRED_MONITOR_HXX
|
||||||
|
|
||||||
|
#include "check.h"
|
||||||
|
|
||||||
|
#include <glib.h>
|
||||||
|
|
||||||
|
#include <atomic>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Defer execution of an event into an #EventLoop.
|
||||||
|
*/
|
||||||
|
class DeferredMonitor {
|
||||||
|
std::atomic<guint> source_id;
|
||||||
|
|
||||||
|
public:
|
||||||
|
DeferredMonitor()
|
||||||
|
:source_id(0) {}
|
||||||
|
|
||||||
|
~DeferredMonitor() {
|
||||||
|
Cancel();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Schedule();
|
||||||
|
void Cancel();
|
||||||
|
|
||||||
|
protected:
|
||||||
|
virtual void Run() = 0;
|
||||||
|
|
||||||
|
private:
|
||||||
|
void DoRun();
|
||||||
|
static gboolean Callback(gpointer data);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif /* MAIN_NOTIFY_H */
|
Loading…
Reference in New Issue
Block a user