Skip to content

Commit

Permalink
Enable wepoll on Windows (tests off) (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
resetius authored Nov 24, 2024
1 parent 3c2084d commit e93b170
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 24 deletions.
23 changes: 23 additions & 0 deletions coroio/all.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,34 @@
#include "poll.hpp"

#ifdef __linux__

#ifndef HAVE_EPOLL
#define HAVE_EPOLL
#endif

#ifndef HAVE_URING
#define HAVE_URING
#endif

#include "epoll.hpp"
#include "uring.hpp"
#endif

#ifdef _WIN32

#ifndef HAVE_EPOLL
#define HAVE_EPOLL
#endif

#include "epoll.hpp"
#endif

#if defined(__APPLE__) || defined(__FreeBSD__)

#ifndef HAVE_KQUEUE
#define HAVE_KQUEUE
#endif

#include "kqueue.hpp"
#endif

Expand Down
37 changes: 33 additions & 4 deletions coroio/epoll.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,48 @@
#ifdef __linux__
#if defined(__linux__) || defined(_WIN32)
#include "epoll.hpp"

namespace NNet {

namespace {
#ifdef _WIN32
int epoll_pwait2(HANDLE ephnd, struct epoll_event* events, int maxevents, const struct timespec* ts, const void* /*sigmask*/) {
int timeout = 0;
if (ts) {
timeout = ts->tv_sec;
timeout += ts->tv_nsec / 1000000;
}
// TODO: support sigmask
return epoll_wait(ephnd, events, maxevents, timeout);
}
#endif
}

#ifdef __linux__
static constexpr int epoll_flags = EPOLL_CLOEXEC;
static constexpr int invalid_handle = -1;
#else
static constexpr int epoll_flags = 0;
static constexpr HANDLE invalid_handle = nullptr;
#endif

TEPoll::TEPoll()
: Fd_(epoll_create1(EPOLL_CLOEXEC))
: Fd_(epoll_create1(epoll_flags))
{
if (Fd_ < 0) {
if (Fd_ == invalid_handle) {
throw std::system_error(errno, std::generic_category(), "epoll_create1");
}
}

TEPoll::~TEPoll()
{
if (Fd_ >= 0) { close(Fd_); }
if (Fd_ != invalid_handle) {
#ifdef __linux__
close(Fd_);
#endif
#ifdef _WIN32
epoll_close(Fd_);
#endif
}
}

void TEPoll::Poll() {
Expand Down
13 changes: 13 additions & 0 deletions coroio/epoll.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
#pragma once

#ifdef __linux__
#include <sys/epoll.h>
#include <unistd.h>
#endif

#ifdef _WIN32
#include "wepoll.h"
#endif

#include "base.hpp"
#include "poller.hpp"
Expand All @@ -20,7 +26,14 @@ class TEPoll: public TPollerBase {
void Poll();

private:
#ifdef __linux__
int Fd_;
#endif

#ifdef _WIN32
HANDLE Fd_;
#endif

std::vector<THandlePair> InEvents_; // all events in epoll
std::vector<epoll_event> OutEvents_; // events out from epoll_wait
};
Expand Down
7 changes: 4 additions & 3 deletions examples/bench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,16 +165,17 @@ int main(int argc, char** argv) {
else if (!strcmp(method, "poll")) {
run_test<TPoll>(num_pipes, num_writes, num_active);
}
#ifdef __linux__
#ifdef HAVE_EPOLL
else if (!strcmp(method, "epoll")) {
run_test<TEPoll>(num_pipes, num_writes, num_active);
}
#endif
#ifdef HAVE_URING
else if (!strcmp(method, "uring")) {
run_test<TUring>(num_pipes, num_writes, num_active);
}
#endif

#if defined(__APPLE__) || defined(__FreeBSD__)
#ifdef HAVE_KQUEUE
else if (!strcmp(method, "kqueue")) {
run_test<TKqueue>(num_pipes, num_writes, num_active);
}
Expand Down
6 changes: 4 additions & 2 deletions examples/echoclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,17 @@ int main(int argc, char** argv) {
else if (method == "poll") {
run<TPoll>(debug, address);
}
#ifdef __linux__
#ifdef HAVE_EPOLL
else if (method == "epoll") {
run<TEPoll>(debug, address);
}
#endif
#ifdef HAVE_URING
else if (method == "uring") {
run<TUring>(debug, address);
}
#endif
#if defined(__APPLE__) || defined(__FreeBSD__)
#ifdef HAVE_KQUEUE
else if (method == "kqueue") {
run<TKqueue>(debug, address);
}
Expand Down
13 changes: 8 additions & 5 deletions examples/echoserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ using NNet::TAddress;
using NNet::TSelect;
using NNet::TPoll;

#ifdef __linux__
#ifdef HAVE_EPOLL
using NNet::TEPoll;
#endif
#ifdef HAVE_URING
using NNet::TUring;
#endif

#if defined(__APPLE__) || defined(__FreeBSD__)
#ifdef HAVE_KQUEUE
using NNet::TKqueue;
#endif

Expand Down Expand Up @@ -90,15 +91,17 @@ int main(int argc, char** argv) {
else if (method == "poll") {
run<TPoll>(debug, address, buffer_size);
}
#ifdef __linux__
#ifdef HAVE_EPOLL
else if (method == "epoll") {
run<TEPoll>(debug, address, buffer_size);
}
#endif
#ifdef HAVE_URING
else if (method == "uring") {
run<TUring>(debug, address, buffer_size);
}
#endif
#if defined(__APPLE__) || defined(__FreeBSD__)
#ifdef HAVE_KQUEUE
else if (method == "kqueue") {
run<TKqueue>(debug, address, buffer_size);
}
Expand Down
6 changes: 4 additions & 2 deletions examples/resolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,17 @@ int main(int argc, char** argv) {
else if (method == "poll") {
run<TPoll>(type);
}
#ifdef __linux__
#ifdef HAVE_EPOLL
else if (method == "epoll") {
run<TEPoll>(type);
}
#endif
#ifdef HAVE_URING
else if (method == "uring") {
run<TUring>(type);
}
#endif
#if defined(__APPLE__) || defined(__FreeBSD__)
#ifdef HAVE_KQUEUE
else if (method == "kqueue") {
run<TKqueue>(type);
}
Expand Down
6 changes: 4 additions & 2 deletions examples/sslechoclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,17 @@ int main(int argc, char** argv) {
else if (method == "poll") {
run<TPoll>(debug, address);
}
#ifdef __linux__
#ifdef HAVE_EPOLL
else if (method == "epoll") {
run<TEPoll>(debug, address);
}
#endif
#ifdef HAVE_URING
else if (method == "uring") {
run<TUring>(debug, address);
}
#endif
#if defined(__APPLE__) || defined(__FreeBSD__)
#ifdef HAVE_KQUEUE
else if (method == "kqueue") {
run<TKqueue>(debug, address);
}
Expand Down
13 changes: 8 additions & 5 deletions examples/sslechoserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ using NNet::TAddress;
using NNet::TSelect;
using NNet::TPoll;

#ifdef __linux__
#ifdef HAVE_EPOLL
using NNet::TEPoll;
#endif
#ifdef HAVE_URING
using NNet::TUring;
#endif

#if defined(__APPLE__) || defined(__FreeBSD__)
#ifdef HAVE_KQUEUE
using NNet::TKqueue;
#endif

Expand Down Expand Up @@ -104,15 +105,17 @@ int main(int argc, char** argv) {
else if (method == "poll") {
run<TPoll>(debug, address, buffer_size);
}
#ifdef __linux__
#ifdef HAVE_EPOLL
else if (method == "epoll") {
run<TEPoll>(debug, address, buffer_size);
}
#endif
#ifdef HAVE_URING
else if (method == "uring") {
run<TUring>(debug, address, buffer_size);
}
#endif
#if defined(__APPLE__) || defined(__FreeBSD__)
#ifdef HAVE_KQUEUE
else if (method == "kqueue") {
run<TKqueue>(debug, address, buffer_size);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1166,7 +1166,7 @@ void test_uring_cancel(void** ) {
#elif defined(__APPLE__) || defined(__FreeBSD__)
#define my_unit_poller(f) my_unit_test3(f, TSelect, TPoll, TKqueue)
#elif defined(_WIN32)
#define my_unit_poller(f) my_unit_test(f, TSelect)
#define my_unit_poller(f) my_unit_test2(f, TSelect, TPoll)
#else
#define my_unit_poller(f) my_unit_test2(f, TSelect, TPoll)
#endif
Expand Down

0 comments on commit e93b170

Please sign in to comment.