From: Christopher Dykes Date: Wed, 11 May 2016 16:51:11 +0000 (-0700) Subject: Add a compatibility shim for working with libevent on MSVC X-Git-Tag: 2016.07.26~249 X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=d618bf95f1b70d3cb24953399ac2552b065e14be;p=folly.git Add a compatibility shim for working with libevent on MSVC Summary: MSVC builds of libevent expect `evutil_socket_t` to be `HANDLE` values, but Folly, HHVM, and Thrift all use them as file descriptors. This adds a `folly_event_set` function that always expects a file descriptor rather than a socket. This also changes the places in Folly that use it. Reviewed By: mzlee, yfeldblum Differential Revision: D2874655 fbshipit-source-id: 66cfd86fd69a9fbac30d150445e4814cd5ca799e --- diff --git a/folly/Makefile.am b/folly/Makefile.am index d6511ccd..3b248629 100644 --- a/folly/Makefile.am +++ b/folly/Makefile.am @@ -278,6 +278,7 @@ nobase_follyinclude_HEADERS = \ portability/Constexpr.h \ portability/Dirent.h \ portability/Environment.h \ + portability/Event.h \ portability/Fcntl.h \ portability/GFlags.h \ portability/IOVec.h \ diff --git a/folly/io/async/AsyncSignalHandler.cpp b/folly/io/async/AsyncSignalHandler.cpp index 3fd19db9..c8197cc8 100644 --- a/folly/io/async/AsyncSignalHandler.cpp +++ b/folly/io/async/AsyncSignalHandler.cpp @@ -80,7 +80,7 @@ void AsyncSignalHandler::unregisterSignalHandler(int signum) { signalEvents_.erase(it); } -void AsyncSignalHandler::libeventCallback(int signum, +void AsyncSignalHandler::libeventCallback(libevent_fd_t signum, short /* events */, void* arg) { AsyncSignalHandler* handler = static_cast(arg); diff --git a/folly/io/async/AsyncSignalHandler.h b/folly/io/async/AsyncSignalHandler.h index 0b0c49b1..213197e0 100644 --- a/folly/io/async/AsyncSignalHandler.h +++ b/folly/io/async/AsyncSignalHandler.h @@ -83,7 +83,7 @@ class AsyncSignalHandler { AsyncSignalHandler(AsyncSignalHandler const &); AsyncSignalHandler& operator=(AsyncSignalHandler const &); - static void libeventCallback(int signum, short events, void* arg); + static void libeventCallback(libevent_fd_t signum, short events, void* arg); EventBase* eventBase_; SignalEventMap signalEvents_; diff --git a/folly/io/async/AsyncTimeout.cpp b/folly/io/async/AsyncTimeout.cpp index 6758aec7..e4375f41 100644 --- a/folly/io/async/AsyncTimeout.cpp +++ b/folly/io/async/AsyncTimeout.cpp @@ -31,7 +31,8 @@ namespace folly { AsyncTimeout::AsyncTimeout(TimeoutManager* timeoutManager) : timeoutManager_(timeoutManager) { - event_set(&event_, -1, EV_TIMEOUT, &AsyncTimeout::libeventCallback, this); + folly_event_set( + &event_, -1, EV_TIMEOUT, &AsyncTimeout::libeventCallback, this); event_.ev_base = nullptr; timeoutManager_->attachTimeoutManager( this, @@ -42,7 +43,8 @@ AsyncTimeout::AsyncTimeout(TimeoutManager* timeoutManager) AsyncTimeout::AsyncTimeout(EventBase* eventBase) : timeoutManager_(eventBase) { - event_set(&event_, -1, EV_TIMEOUT, &AsyncTimeout::libeventCallback, this); + folly_event_set( + &event_, -1, EV_TIMEOUT, &AsyncTimeout::libeventCallback, this); event_.ev_base = nullptr; if (eventBase) { timeoutManager_->attachTimeoutManager( @@ -56,7 +58,8 @@ AsyncTimeout::AsyncTimeout(TimeoutManager* timeoutManager, InternalEnum internal) : timeoutManager_(timeoutManager) { - event_set(&event_, -1, EV_TIMEOUT, &AsyncTimeout::libeventCallback, this); + folly_event_set( + &event_, -1, EV_TIMEOUT, &AsyncTimeout::libeventCallback, this); event_.ev_base = nullptr; timeoutManager_->attachTimeoutManager(this, internal); RequestContext::saveContext(); @@ -65,14 +68,16 @@ AsyncTimeout::AsyncTimeout(TimeoutManager* timeoutManager, AsyncTimeout::AsyncTimeout(EventBase* eventBase, InternalEnum internal) : timeoutManager_(eventBase) { - event_set(&event_, -1, EV_TIMEOUT, &AsyncTimeout::libeventCallback, this); + folly_event_set( + &event_, -1, EV_TIMEOUT, &AsyncTimeout::libeventCallback, this); event_.ev_base = nullptr; timeoutManager_->attachTimeoutManager(this, internal); RequestContext::saveContext(); } AsyncTimeout::AsyncTimeout(): timeoutManager_(nullptr) { - event_set(&event_, -1, EV_TIMEOUT, &AsyncTimeout::libeventCallback, this); + folly_event_set( + &event_, -1, EV_TIMEOUT, &AsyncTimeout::libeventCallback, this); event_.ev_base = nullptr; RequestContext::saveContext(); } @@ -138,9 +143,9 @@ void AsyncTimeout::detachEventBase() { detachTimeoutManager(); } -void AsyncTimeout::libeventCallback(int fd, short events, void* arg) { +void AsyncTimeout::libeventCallback(libevent_fd_t fd, short events, void* arg) { AsyncTimeout* timeout = reinterpret_cast(arg); - assert(fd == -1); + assert(libeventFdToFd(fd) == -1); assert(events == EV_TIMEOUT); // prevent unused variable warnings (void)fd; diff --git a/folly/io/async/AsyncTimeout.h b/folly/io/async/AsyncTimeout.h index 62b10455..0c86ef74 100644 --- a/folly/io/async/AsyncTimeout.h +++ b/folly/io/async/AsyncTimeout.h @@ -22,6 +22,8 @@ #include +#include + #include #include #include @@ -219,7 +221,7 @@ class AsyncTimeout : private boost::noncopyable { ); private: - static void libeventCallback(int fd, short events, void* arg); + static void libeventCallback(libevent_fd_t fd, short events, void* arg); struct event event_; diff --git a/folly/io/async/EventHandler.cpp b/folly/io/async/EventHandler.cpp index a0ac4a8f..c97b0d16 100644 --- a/folly/io/async/EventHandler.cpp +++ b/folly/io/async/EventHandler.cpp @@ -26,7 +26,7 @@ namespace folly { EventHandler::EventHandler(EventBase* eventBase, int fd) { - event_set(&event_, fd, 0, &EventHandler::libeventCallback, this); + folly_event_set(&event_, fd, 0, &EventHandler::libeventCallback, this); if (eventBase != nullptr) { setEventBase(eventBase); } else { @@ -124,13 +124,13 @@ void EventHandler::changeHandlerFD(int fd) { ensureNotRegistered(__func__); // event_set() resets event_base.ev_base, so manually restore it afterwards struct event_base* evb = event_.ev_base; - event_set(&event_, fd, 0, &EventHandler::libeventCallback, this); + folly_event_set(&event_, fd, 0, &EventHandler::libeventCallback, this); event_.ev_base = evb; // don't use event_base_set(), since evb may be nullptr } void EventHandler::initHandler(EventBase* eventBase, int fd) { ensureNotRegistered(__func__); - event_set(&event_, fd, 0, &EventHandler::libeventCallback, this); + folly_event_set(&event_, fd, 0, &EventHandler::libeventCallback, this); setEventBase(eventBase); } @@ -144,7 +144,7 @@ void EventHandler::ensureNotRegistered(const char* fn) { } } -void EventHandler::libeventCallback(int fd, short events, void* arg) { +void EventHandler::libeventCallback(libevent_fd_t fd, short events, void* arg) { EventHandler* handler = reinterpret_cast(arg); assert(fd == handler->event_.ev_fd); (void)fd; // prevent unused variable warnings diff --git a/folly/io/async/EventHandler.h b/folly/io/async/EventHandler.h index 18493304..af50c36e 100644 --- a/folly/io/async/EventHandler.h +++ b/folly/io/async/EventHandler.h @@ -22,6 +22,7 @@ #include #include +#include #include #include @@ -177,7 +178,7 @@ class EventHandler : private boost::noncopyable { void setEventBase(EventBase* eventBase); - static void libeventCallback(int fd, short events, void* arg); + static void libeventCallback(libevent_fd_t fd, short events, void* arg); struct event event_; EventBase* eventBase_; diff --git a/folly/portability/Event.h b/folly/portability/Event.h new file mode 100644 index 00000000..f921cd5f --- /dev/null +++ b/folly/portability/Event.h @@ -0,0 +1,62 @@ +/* + * Copyright 2016 Facebook, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include + +#ifdef _MSC_VER +# include +# include +# include +#endif + +namespace folly { +#ifdef _MSC_VER +using libevent_fd_t = evutil_socket_t; +#else +using libevent_fd_t = int; +#endif + +inline libevent_fd_t getLibeventFd(int fd) { +#ifdef _MSC_VER + if (fd == -1) { + return (libevent_fd_t)INVALID_HANDLE_VALUE; + } + return _get_osfhandle(fd); +#else + return fd; +#endif +} + +inline int libeventFdToFd(libevent_fd_t fd) { +#ifdef _MSC_VER + if (fd == (libevent_fd_t)INVALID_HANDLE_VALUE) { + return -1; + } + return _open_osfhandle((intptr_t)fd, O_RDWR | O_BINARY); +#else + return fd; +#endif +} + +using EventSetCallback = void (*)(libevent_fd_t, short, void*); +inline void +folly_event_set(event* e, int fd, short s, EventSetCallback f, void* arg) { + auto lfd = getLibeventFd(fd); + event_set(e, lfd, s, f, arg); +} +}