From cfdd5edc499601430bb93a8b593fab0edec34f08 Mon Sep 17 00:00:00 2001
From: Denis Krjuchkov <denis@crazydev.net>
Date: Fri, 29 Nov 2013 14:38:48 +0600
Subject: [PATCH] event: implement PollGroup based on Windows select

---
 Makefile.am                      |   2 +
 configure.ac                     |  14 ++-
 src/event/PollGroup.hxx          |   6 ++
 src/event/PollGroupWinSelect.cxx | 158 +++++++++++++++++++++++++++++++
 src/event/PollGroupWinSelect.hxx | 109 +++++++++++++++++++++
 src/event/PollResultGeneric.hxx  |  57 +++++++++++
 6 files changed, 343 insertions(+), 3 deletions(-)
 create mode 100644 src/event/PollGroupWinSelect.cxx
 create mode 100644 src/event/PollGroupWinSelect.hxx
 create mode 100644 src/event/PollResultGeneric.hxx

diff --git a/Makefile.am b/Makefile.am
index f92f45d75..3d66de8e5 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -308,6 +308,8 @@ libevent_a_SOURCES = \
 	src/event/WakeFD.hxx \
 	src/event/PollGroup.hxx \
 	src/event/PollGroupEPoll.hxx \
+	src/event/PollGroupWinSelect.hxx src/event/PollGroupWinSelect.cxx \
+	src/event/PollResultGeneric.hxx \
 	src/event/SignalMonitor.hxx src/event/SignalMonitor.cxx \
 	src/event/TimeoutMonitor.hxx src/event/TimeoutMonitor.cxx \
 	src/event/IdleMonitor.hxx src/event/IdleMonitor.cxx \
diff --git a/configure.ac b/configure.ac
index db35fc0bf..23998269b 100644
--- a/configure.ac
+++ b/configure.ac
@@ -170,13 +170,15 @@ AC_ARG_WITH(eventloop,
 
 AC_ARG_WITH(pollmethod,
 	AS_HELP_STRING(
-		[--with-pollmethod=@<:@epoll|auto@:>@],
+		[--with-pollmethod=@<:@epoll|winselect|auto@:>@],
 		[specify poll method for internal event loop (default=auto)]),,
 	[with_pollmethod=auto])
 
 if test "x$with_eventloop" = xauto; then
-	if test "x$enable_epoll" = xyes; then
-		with_eventloop=internal
+	if
+		test "x$enable_epoll" = xyes ||
+		test "x$host_is_windows" = xyes; then
+			with_eventloop=internal
 	else
 		with_eventloop=glib
 	fi
@@ -200,6 +202,8 @@ if test "x$with_eventloop" = xinternal; then
 	if test "x$with_pollmethod" = xauto; then
 		if test "x$enable_epoll" = xyes; then
 			with_pollmethod=epoll
+		elif test "x$host_is_windows" = xyes; then
+			with_pollmethod=winselect
 		else
 			AC_MSG_ERROR([no poll method is available for your platform])
 		fi
@@ -208,6 +212,10 @@ if test "x$with_eventloop" = xinternal; then
 	epoll)
 		AC_DEFINE(USE_EPOLL, 1, [Define to poll sockets with epoll])
 		;;
+	winselect)
+		AC_DEFINE(USE_WINSELECT, 1,
+			[Define to poll sockets with Windows select])
+		;;
 	*)
 		AC_MSG_ERROR([unknown pollmethod option: $with_pollmethod])
 	esac
diff --git a/src/event/PollGroup.hxx b/src/event/PollGroup.hxx
index 824a6ecfe..1e34f1423 100644
--- a/src/event/PollGroup.hxx
+++ b/src/event/PollGroup.hxx
@@ -26,4 +26,10 @@ typedef PollResultEPoll PollResult;
 typedef PollGroupEPoll  PollGroup;
 #endif
 
+#ifdef USE_WINSELECT
+#include "PollGroupWinSelect.hxx"
+typedef PollResultGeneric  PollResult;
+typedef PollGroupWinSelect PollGroup;
+#endif
+
 #endif
diff --git a/src/event/PollGroupWinSelect.cxx b/src/event/PollGroupWinSelect.cxx
new file mode 100644
index 000000000..1050ffa8a
--- /dev/null
+++ b/src/event/PollGroupWinSelect.cxx
@@ -0,0 +1,158 @@
+/*
+ * 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"
+
+#ifdef USE_WINSELECT
+
+#include "PollGroupWinSelect.hxx"
+
+constexpr int EVENT_READ = 0;
+constexpr int EVENT_WRITE = 1;
+
+static inline bool HasEvent(unsigned events, int event_id)
+{
+	return (events & (1 << event_id)) != 0;
+}
+
+bool PollGroupWinSelect::CanModify(PollGroupWinSelect::Item &item,
+				   unsigned events, int event_id)
+{
+	if (item.index[event_id] < 0 && HasEvent(events, event_id))
+		return !event_set[event_id].IsFull();
+	return true;
+}
+
+void PollGroupWinSelect::Modify(PollGroupWinSelect::Item &item, int fd,
+				unsigned events, int event_id)
+{
+	int index = item.index[event_id];
+	auto &set = event_set[event_id];
+
+	if (index < 0 && HasEvent(events, event_id))
+		item.index[event_id] = set.Add(fd);
+	else if (index >= 0 && !HasEvent(events, event_id)) {
+		if (index != set.Size() - 1) {
+			set.MoveToEnd(index);
+			items[set[index]].index[event_id] = index;
+		}
+		set.RemoveLast();
+		item.index[event_id] = -1;
+	}
+}
+
+bool PollGroupWinSelect::Add(int fd, unsigned events, void *obj)
+{
+	assert(items.find(fd) == items.end());
+	auto &item = items[fd];
+
+	item.index[EVENT_READ] = -1;
+	item.index[EVENT_WRITE] = -1;
+	item.obj = obj;
+	item.events = 0;
+
+	if (!CanModify(item, events, EVENT_READ)) {
+		items.erase(fd);
+		return false;
+	}
+	if (!CanModify(item, events, EVENT_WRITE)) {
+		items.erase(fd);
+		return false;
+	}
+
+	Modify(item, fd, events, EVENT_READ);
+	Modify(item, fd, events, EVENT_WRITE);
+	return true;
+}
+
+bool PollGroupWinSelect::Modify(int fd, unsigned events, void *obj)
+{
+	auto item_iter = items.find(fd);
+	assert(item_iter != items.end());
+	auto &item = item_iter->second;
+
+	if (!CanModify(item, events, EVENT_READ))
+		return false;
+	if (!CanModify(item, events, EVENT_WRITE))
+		return false;
+
+	item.obj = obj;
+	Modify(item, fd, events, EVENT_READ);
+	Modify(item, fd, events, EVENT_WRITE);
+	return true;
+}
+
+bool PollGroupWinSelect::Remove(int fd)
+{
+	auto item_iter = items.find(fd);
+	assert(item_iter != items.end());
+	auto &item = item_iter->second;
+
+	Modify(item, fd, 0, EVENT_READ);
+	Modify(item, fd, 0, EVENT_WRITE);
+	items.erase(item_iter);
+	return true;
+}
+
+void PollGroupWinSelect::ReadEvents(PollResultGeneric &result, int timeout_ms)
+{
+	bool use_sleep = event_set[EVENT_READ].IsEmpty() &&
+			 event_set[EVENT_WRITE].IsEmpty();
+
+	if (use_sleep) {
+		Sleep(timeout_ms < 0 ? INFINITE : (DWORD) timeout_ms);
+		return;
+	}
+
+	SocketSet read_set(event_set[EVENT_READ]);
+	SocketSet write_set(event_set[EVENT_WRITE]);
+	SocketSet except_set(event_set[EVENT_WRITE]);
+
+	timeval tv;
+	if (timeout_ms >= 0) {
+		tv.tv_sec = timeout_ms / 1000;
+		tv.tv_usec = (timeout_ms % 1000) * 1000;
+	}
+
+	int ret = select(0,
+			 read_set.IsEmpty() ? nullptr : read_set.GetPtr(),
+			 write_set.IsEmpty() ? nullptr : write_set.GetPtr(),
+			 except_set.IsEmpty() ? nullptr : except_set.GetPtr(),
+			 timeout_ms < 0 ? nullptr : &tv);
+
+	if (ret == 0 || ret == SOCKET_ERROR)
+		return;
+
+	for (int i = 0; i < read_set.Size(); ++i)
+		items[read_set[i]].events |= READ;
+
+	for (int i = 0; i < write_set.Size(); ++i)
+		items[write_set[i]].events |= WRITE;
+
+	for (int i = 0; i < except_set.Size(); ++i)
+		items[except_set[i]].events |= WRITE;
+
+	for (auto i = items.begin(); i != items.end(); ++i)
+		if (i->second.events != 0) {
+			result.Add(i->second.events, i->second.obj);
+			i->second.events = 0;
+		}
+}
+
+#endif
diff --git a/src/event/PollGroupWinSelect.hxx b/src/event/PollGroupWinSelect.hxx
new file mode 100644
index 000000000..722d06f52
--- /dev/null
+++ b/src/event/PollGroupWinSelect.hxx
@@ -0,0 +1,109 @@
+/*
+ * 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_EVENT_POLLGROUP_WINSELECT_HXX
+#define MPD_EVENT_POLLGROUP_WINSELECT_HXX
+
+#include "check.h"
+
+#include "PollResultGeneric.hxx"
+
+#include <assert.h>
+#include <string.h>
+
+#include <unordered_map>
+
+#include <windows.h>
+#include <winsock2.h>
+
+#ifdef ERROR
+#undef ERROR
+#endif
+
+class SocketSet
+{
+	fd_set set;
+public:
+	SocketSet() { set.fd_count = 0; }
+	SocketSet(SocketSet &other) {
+		set.fd_count = other.set.fd_count;
+		memcpy(set.fd_array,
+		       other.set.fd_array,
+		       sizeof (SOCKET) * set.fd_count);
+	}
+
+	fd_set *GetPtr() { return &set; }
+	int Size() { return set.fd_count; }
+	bool IsEmpty() { return set.fd_count == 0; }
+	bool IsFull() { return set.fd_count == FD_SETSIZE; }
+
+	int operator[](int index) {
+		assert(index >= 0 && (u_int)index < set.fd_count);
+		return set.fd_array[index];
+	}
+
+	int Add(int fd) {
+		assert(!IsFull());
+		set.fd_array[set.fd_count] = fd;
+		return set.fd_count++;
+	}
+
+	void MoveToEnd(int index) {
+		assert(index >= 0 && (u_int)index < set.fd_count);
+		std::swap(set.fd_array[index], set.fd_array[set.fd_count - 1]);
+	}
+
+	void RemoveLast() {
+		assert(!IsEmpty());
+		--set.fd_count;
+	}
+};
+
+class PollGroupWinSelect
+{
+	struct Item
+	{
+		int index[2];
+		void *obj;
+		unsigned events;
+	};
+
+	SocketSet event_set[2];
+	std::unordered_map<int, Item> items;
+
+	bool CanModify(Item &item, unsigned events, int event_id);
+	void Modify(Item &item, int fd, unsigned events, int event_id);
+
+public:
+	static constexpr unsigned READ = 1;
+	static constexpr unsigned WRITE = 2;
+	static constexpr unsigned ERROR = 0;
+	static constexpr unsigned HANGUP = 0;
+
+	PollGroupWinSelect() { }
+	~PollGroupWinSelect() { }
+
+	void ReadEvents(PollResultGeneric &result, int timeout_ms);
+	bool Add(int fd, unsigned events, void *obj);
+	bool Modify(int fd, unsigned events, void *obj);
+	bool Remove(int fd);
+	bool Abandon(int fd) { return Remove(fd); }
+};
+
+#endif
diff --git a/src/event/PollResultGeneric.hxx b/src/event/PollResultGeneric.hxx
new file mode 100644
index 000000000..1c2c0d00b
--- /dev/null
+++ b/src/event/PollResultGeneric.hxx
@@ -0,0 +1,57 @@
+/*
+ * 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_EVENT_POLLRESULT_GENERIC_HXX
+#define MPD_EVENT_POLLRESULT_GENERIC_HXX
+
+#include "check.h"
+
+#include <vector>
+
+class PollResultGeneric
+{
+	struct Item
+	{
+		unsigned events;
+		void *obj;
+
+		Item() = default;
+		Item(unsigned _events, void *_obj)
+			: events(_events), obj(_obj) { }
+	};
+
+	std::vector<Item> items;
+public:
+	int GetSize() const { return items.size(); }
+	unsigned GetEvents(int i) const { return items[i].events; }
+	void *GetObject(int i) const { return items[i].obj; }
+	void Reset() { items.clear(); }
+
+	void Clear(void *obj) {
+		for (auto i = items.begin(); i != items.end(); ++i)
+			if (i->obj == obj)
+				i->events = 0;
+	}
+
+	void Add(unsigned events, void *obj) {
+		items.emplace_back(events, obj);
+	}
+};
+
+#endif