aboutsummaryrefslogtreecommitdiff
path: root/src/event
diff options
context:
space:
mode:
authorDenis Krjuchkov <denis@crazydev.net>2013-01-13 15:30:53 +0600
committerDenis Krjuchkov <denis@crazydev.net>2013-01-13 15:30:53 +0600
commit90ab65f8c732e83bd99a1621e38a4e690c71e95d (patch)
tree277fbb184b0d366a987927bda65e819b11923813 /src/event
parentabb0fcb2035fa96b1ec09f9480838c476527ccd4 (diff)
WakeFD.cxx: use two sockets instead of pipe on Win32
Pipes aren't really pollable on Windows. GLib emulates polling on pipes using separate reader/writer threads. This adds significant overhead and keeps a strong dependency on GLib. socketpair() function is absent on Windows as well. We implement it here in a loose way.
Diffstat (limited to 'src/event')
-rw-r--r--src/event/WakeFD.cxx131
1 files changed, 127 insertions, 4 deletions
diff --git a/src/event/WakeFD.cxx b/src/event/WakeFD.cxx
index a09f1bf7..1a84f564 100644
--- a/src/event/WakeFD.cxx
+++ b/src/event/WakeFD.cxx
@@ -24,16 +24,29 @@
#include <unistd.h>
+#ifdef WIN32
+#include <ws2tcpip.h>
+#include <winsock2.h>
+#include <cstring> /* for memset() */
+#endif
+
#ifdef HAVE_EVENTFD
#include <sys/eventfd.h>
#endif
+#ifdef WIN32
+static bool PoorSocketPair(int fd[2]);
+#endif
+
bool
WakeFD::Create()
{
assert(fds[0] == -1);
assert(fds[1] == -1);
+#ifdef WIN32
+ return PoorSocketPair(fds);
+#else
#ifdef HAVE_EVENTFD
fds[0] = eventfd_cloexec_nonblock(0, 0);
if (fds[0] >= 0) {
@@ -41,21 +54,23 @@ WakeFD::Create()
return true;
}
#endif
-
return pipe_cloexec_nonblock(fds) >= 0;
+#endif
}
void
WakeFD::Destroy()
{
-#ifndef WIN32
- /* By some strange reason this call hangs on Win32 */
+#ifdef WIN32
+ closesocket(fds[0]);
+ closesocket(fds[1]);
+#else
close(fds[0]);
-#endif
#ifdef HAVE_EVENTFD
if (!IsEventFD())
#endif
close(fds[1]);
+#endif
#ifndef NDEBUG
fds[0] = -1;
@@ -68,6 +83,12 @@ WakeFD::Read()
{
assert(fds[0] >= 0);
+#ifdef WIN32
+ assert(fds[1] >= 0);
+ char buffer[256];
+ return recv(fds[0], buffer, sizeof(buffer), 0) > 0;
+#else
+
#ifdef HAVE_EVENTFD
if (IsEventFD()) {
eventfd_t value;
@@ -80,6 +101,7 @@ WakeFD::Read()
char buffer[256];
return read(fds[0], buffer, sizeof(buffer)) > 0;
+#endif
}
void
@@ -87,6 +109,12 @@ WakeFD::Write()
{
assert(fds[0] >= 0);
+#ifdef WIN32
+ assert(fds[1] >= 0);
+
+ send(fds[1], "", 1, 0);
+#else
+
#ifdef HAVE_EVENTFD
if (IsEventFD()) {
static constexpr eventfd_t value = 1;
@@ -99,4 +127,99 @@ WakeFD::Write()
assert(fds[1] >= 0);
gcc_unused ssize_t nbytes = write(fds[1], "", 1);
+#endif
+}
+
+#ifdef WIN32
+
+static void SafeCloseSocket(SOCKET s)
+{
+ int error = WSAGetLastError();
+ closesocket(s);
+ WSASetLastError(error);
+}
+
+/* Our poor man's socketpair() implementation
+ * Due to limited protocol/address family support and primitive error handling
+ * it's better to keep this as a private implementation detail of WakeFD
+ * rather than wide-available API.
+ */
+static bool PoorSocketPair(int fd[2])
+{
+ assert (fd != nullptr);
+
+ SOCKET listen_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
+ if (listen_socket == INVALID_SOCKET)
+ return false;
+
+ sockaddr_in address;
+ std::memset(&address, 0, sizeof(address));
+ address.sin_family = AF_INET;
+ address.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+
+ int ret = bind(listen_socket,
+ reinterpret_cast<sockaddr*>(&address),
+ sizeof(address));
+
+ if (ret < 0) {
+ SafeCloseSocket(listen_socket);
+ return false;
+ }
+
+ ret = listen(listen_socket, 1);
+
+ if (ret < 0) {
+ SafeCloseSocket(listen_socket);
+ return false;
+ }
+
+ int address_len = sizeof(address);
+ ret = getsockname(listen_socket,
+ reinterpret_cast<sockaddr*>(&address),
+ &address_len);
+
+ if (ret < 0) {
+ SafeCloseSocket(listen_socket);
+ return false;
+ }
+
+ SOCKET socket0 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
+ if (socket0 == INVALID_SOCKET) {
+ SafeCloseSocket(listen_socket);
+ return false;
+ }
+
+ ret = connect(socket0,
+ reinterpret_cast<sockaddr*>(&address),
+ sizeof(address));
+
+ if (ret < 0) {
+ SafeCloseSocket(listen_socket);
+ SafeCloseSocket(socket0);
+ return false;
+ }
+
+ SOCKET socket1 = accept(listen_socket, nullptr, nullptr);
+ if (socket1 == INVALID_SOCKET) {
+ SafeCloseSocket(listen_socket);
+ SafeCloseSocket(socket0);
+ return false;
+ }
+
+ SafeCloseSocket(listen_socket);
+
+ u_long non_block = 1;
+ if (ioctlsocket(socket0, FIONBIO, &non_block) < 0
+ || ioctlsocket(socket1, FIONBIO, &non_block) < 0) {
+ SafeCloseSocket(socket0);
+ SafeCloseSocket(socket1);
+ return false;
+ }
+
+ fd[0] = static_cast<int>(socket0);
+ fd[1] = static_cast<int>(socket1);
+
+ return true;
}
+
+#endif