portability/Constexpr.h \
portability/Dirent.h \
portability/Environment.h \
+ portability/Event.h \
portability/Fcntl.h \
portability/GFlags.h \
portability/IOVec.h \
signalEvents_.erase(it);
}
-void AsyncSignalHandler::libeventCallback(int signum,
+void AsyncSignalHandler::libeventCallback(libevent_fd_t signum,
short /* events */,
void* arg) {
AsyncSignalHandler* handler = static_cast<AsyncSignalHandler*>(arg);
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_;
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,
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(
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();
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();
}
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<AsyncTimeout*>(arg);
- assert(fd == -1);
+ assert(libeventFdToFd(fd) == -1);
assert(events == EV_TIMEOUT);
// prevent unused variable warnings
(void)fd;
#include <folly/io/async/TimeoutManager.h>
+#include <folly/portability/Event.h>
+
#include <boost/noncopyable.hpp>
#include <event.h>
#include <memory>
);
private:
- static void libeventCallback(int fd, short events, void* arg);
+ static void libeventCallback(libevent_fd_t fd, short events, void* arg);
struct event event_;
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 {
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);
}
}
}
-void EventHandler::libeventCallback(int fd, short events, void* arg) {
+void EventHandler::libeventCallback(libevent_fd_t fd, short events, void* arg) {
EventHandler* handler = reinterpret_cast<EventHandler*>(arg);
assert(fd == handler->event_.ev_fd);
(void)fd; // prevent unused variable warnings
#include <glog/logging.h>
#include <folly/io/async/EventUtil.h>
+#include <folly/portability/Event.h>
#include <boost/noncopyable.hpp>
#include <stddef.h>
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_;
--- /dev/null
+/*
+ * 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 <event.h>
+
+#ifdef _MSC_VER
+# include <event2/event_compat.h>
+# include <folly/portability/Fcntl.h>
+# include <folly/portability/Windows.h>
+#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);
+}
+}