event/PollGroup: move event flags to a separate header

Reduce header dependencies for SocketEvent.hxx.
This commit is contained in:
Max Kellermann 2020-10-19 14:28:41 +02:00
parent 418ba96334
commit c18e00daa4
9 changed files with 150 additions and 32 deletions

View File

@ -0,0 +1,42 @@
/*
* Copyright 2003-2020 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 EVENT_BACKEND_EVENTS_HXX
#define EVENT_BACKEND_EVENTS_HXX
#include "event/Features.h"
#ifdef _WIN32
#include "WinSelectEvents.hxx"
using PollBackendEvents = WinSelectEvents;
#elif defined(USE_EPOLL)
#include "EpollEvents.hxx"
using PollBackendEvents = EpollEvents;
#else
#include "PollEvents.hxx"
using PollBackendEvents = PollEvents;
#endif
#endif

32
src/event/EpollEvents.hxx Normal file
View File

@ -0,0 +1,32 @@
/*
* Copyright 2003-2020 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 EVENT_EPOLL_EVENTS_HXX
#define EVENT_EPOLL_EVENTS_HXX
#include <sys/epoll.h>
struct EpollEvents {
static constexpr unsigned READ = EPOLLIN;
static constexpr unsigned WRITE = EPOLLOUT;
static constexpr unsigned ERROR = EPOLLERR;
static constexpr unsigned HANGUP = EPOLLHUP;
};
#endif

32
src/event/PollEvents.hxx Normal file
View File

@ -0,0 +1,32 @@
/*
* Copyright 2003-2020 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 EVENT_POLL_EVENTS_HXX
#define EVENT_POLL_EVENTS_HXX
#include <sys/poll.h>
struct PollEvents {
static constexpr unsigned READ = POLLIN;
static constexpr unsigned WRITE = POLLOUT;
static constexpr unsigned ERROR = POLLERR;
static constexpr unsigned HANGUP = POLLHUP;
};
#endif

View File

@ -54,11 +54,6 @@ class PollGroupEpoll
PollGroupEpoll(PollGroupEpoll &) = delete;
PollGroupEpoll &operator=(PollGroupEpoll &) = delete;
public:
static constexpr unsigned READ = EPOLLIN;
static constexpr unsigned WRITE = EPOLLOUT;
static constexpr unsigned ERROR = EPOLLERR;
static constexpr unsigned HANGUP = EPOLLHUP;
PollGroupEpoll() = default;
auto ReadEvents(int timeout_ms) noexcept {

View File

@ -42,11 +42,6 @@ class PollGroupPoll
PollGroupPoll(PollGroupPoll &) = delete;
PollGroupPoll &operator=(PollGroupPoll &) = delete;
public:
static constexpr unsigned READ = POLLIN;
static constexpr unsigned WRITE = POLLOUT;
static constexpr unsigned ERROR = POLLERR;
static constexpr unsigned HANGUP = POLLHUP;
PollGroupPoll() noexcept;
~PollGroupPoll() noexcept;

View File

@ -18,6 +18,7 @@
*/
#include "PollGroupWinSelect.hxx"
#include "WinSelectEvents.hxx"
static constexpr int EVENT_READ = 0;
static constexpr int EVENT_WRITE = 1;
@ -147,13 +148,13 @@ PollGroupWinSelect::ReadEvents(int timeout_ms) noexcept
return result;
for (const auto i : read_set)
items[i].events |= READ;
items[i].events |= WinSelectEvents::READ;
for (const auto i : write_set)
items[i].events |= WRITE;
items[i].events |= WinSelectEvents::WRITE;
for (const auto i : except_set)
items[i].events |= WRITE;
items[i].events |= WinSelectEvents::WRITE;
for (auto &i : items)
if (i.second.events != 0) {

View File

@ -26,15 +26,8 @@
#include <cassert>
#include <unordered_map>
#include <windows.h>
#include <winsock2.h>
/* ERROR is a WIN32 macro that poisons our namespace; this is a kludge
to allow us to use it anyway */
#ifdef ERROR
#undef ERROR
#endif
class SocketSet
{
fd_set set;
@ -114,11 +107,6 @@ class PollGroupWinSelect
PollGroupWinSelect(PollGroupWinSelect &) = delete;
PollGroupWinSelect &operator=(PollGroupWinSelect &) = delete;
public:
static constexpr unsigned READ = 1;
static constexpr unsigned WRITE = 2;
static constexpr unsigned ERROR = 0;
static constexpr unsigned HANGUP = 0;
PollGroupWinSelect() noexcept;
~PollGroupWinSelect() noexcept;

View File

@ -20,7 +20,7 @@
#ifndef MPD_SOCKET_EVENT_HXX
#define MPD_SOCKET_EVENT_HXX
#include "PollGroup.hxx"
#include "BackendEvents.hxx"
#include "net/SocketDescriptor.hxx"
#include "util/BindMethod.hxx"
#include "util/IntrusiveList.hxx"
@ -44,7 +44,7 @@ class EventLoop;
* thread that runs the #EventLoop, except where explicitly documented
* as thread-safe.
*/
class SocketEvent final : IntrusiveListHook {
class SocketEvent final : IntrusiveListHook, public PollBackendEvents {
friend class EventLoop;
friend class IntrusiveList<SocketEvent>;
@ -69,11 +69,6 @@ class SocketEvent final : IntrusiveListHook {
unsigned ready_flags = 0;
public:
static constexpr unsigned READ = PollGroup::READ;
static constexpr unsigned WRITE = PollGroup::WRITE;
static constexpr unsigned ERROR = PollGroup::ERROR;
static constexpr unsigned HANGUP = PollGroup::HANGUP;
/**
* These flags are always reported by epoll_wait() and don't
* need to be registered with epoll_ctl().

View File

@ -0,0 +1,38 @@
/*
* Copyright 2003-2020 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 EVENT_WINSELECT_EVENTS_HXX
#define EVENT_WINSELECT_EVENTS_HXX
#include <windows.h>
/* ERROR is a WIN32 macro that poisons our namespace; this is a kludge
to allow us to use it anyway */
#ifdef ERROR
#undef ERROR
#endif
struct WinSelectEvents {
static constexpr unsigned READ = 1;
static constexpr unsigned WRITE = 2;
static constexpr unsigned ERROR = 0;
static constexpr unsigned HANGUP = 0;
};
#endif