Compatibility: libevent: accessors for struct event
authorYedidya Feldblum <yfeldblum@fb.com>
Wed, 16 Sep 2015 21:15:19 +0000 (14:15 -0700)
committerfacebook-github-bot-4 <folly-bot@fb.com>
Wed, 16 Sep 2015 22:20:18 +0000 (15:20 -0700)
Summary: [Folly] Compatibility: libevent: accessors for `struct event`.

Some libevent fields in `struct event` moved between `v1.4` and `v2`. Add some accessors which are defined per-libevent-version.

Reviewed By: @JoelMarcey

Differential Revision: D2447537

folly/io/async/AsyncTimeout.cpp
folly/io/async/EventBase.cpp
folly/io/async/EventHandler.cpp
folly/io/async/EventUtil.h

index 31a1c0b5aadb803f2ecdc84e763291d6f42ad75c..7b6ab3db67e81c8af0ada0541f67c2ce9ee06673 100644 (file)
@@ -144,7 +144,7 @@ void AsyncTimeout::libeventCallback(int fd, short events, void* arg) {
   assert(events == EV_TIMEOUT);
 
   // double check that ev_flags gets reset when the timeout is not running
-  assert((timeout->event_.ev_flags & ~EVLIST_INTERNAL) == EVLIST_INIT);
+  assert((event_ref_flags(&timeout->event_) & ~EVLIST_INTERNAL) == EVLIST_INIT);
 
   // this can't possibly fire if timeout->eventBase_ is nullptr
   (void) timeout->timeoutManager_->bumpHandlingTime();
index e73bdb3e58aaffa91f8332aaf4d95288a5f9e4a3..5adb8c1543630de781fe56c7802f9d63f9fa65b6 100644 (file)
@@ -797,7 +797,7 @@ void EventBase::attachTimeoutManager(AsyncTimeout* obj,
   event_base_set(getLibeventBase(), ev);
   if (internal == AsyncTimeout::InternalEnum::INTERNAL) {
     // Set the EVLIST_INTERNAL flag
-    ev->ev_flags |= EVLIST_INTERNAL;
+    event_ref_flags(ev) |= EVLIST_INTERNAL;
   }
 }
 
index dcccfafbb047b7e529ddc555f8cade928858d9aa..ab5cad6bdf39e833ea8de142d8205fdf952c60c5 100644 (file)
@@ -49,8 +49,9 @@ bool EventHandler::registerImpl(uint16_t events, bool internal) {
   if (isHandlerRegistered()) {
     // If the new events are the same are the same as the already registered
     // flags, we don't have to do anything.  Just return.
+    auto flags = event_ref_flags(&event_);
     if (events == event_.ev_events &&
-        static_cast<bool>(event_.ev_flags & EVLIST_INTERNAL) == internal) {
+        static_cast<bool>(flags & EVLIST_INTERNAL) == internal) {
       return true;
     }
 
@@ -67,7 +68,7 @@ bool EventHandler::registerImpl(uint16_t events, bool internal) {
 
   // Set EVLIST_INTERNAL if this is an internal event
   if (internal) {
-    event_.ev_flags |= EVLIST_INTERNAL;
+    event_ref_flags(&event_) |= EVLIST_INTERNAL;
   }
 
   // Add the event.
@@ -168,7 +169,7 @@ void EventHandler::setEventBase(EventBase* eventBase) {
 }
 
 bool EventHandler::isPending() const {
-  if (event_.ev_flags & EVLIST_ACTIVE) {
+  if (event_ref_flags(&event_) & EVLIST_ACTIVE) {
     if (event_.ev_res & EV_READ) {
       return true;
     }
index 116cf4c5cf8918776b08d101341b66b7cf00bfc8..7b82aa0b82a6f0d43c7c92985f0f37f9b0544c5c 100644 (file)
  */
 #pragma once
 
+#include <functional>
 #include <event.h>  // libevent
 
 namespace folly {
 
+# if LIBEVENT_VERSION_NUMBER <= 0x02010101
+#   define FOLLY_LIBEVENT_COMPAT_PLUCK(name) ev_##name
+# else
+#   define FOLLY_LIBEVENT_COMPAT_PLUCK(name) ev_evcallback.evcb_##name
+# endif
+# define FOLLY_LIBEVENT_DEF_ACCESSORS(name) \
+    inline auto event_ref_##name(struct event* ev) -> \
+      decltype(std::ref(ev->FOLLY_LIBEVENT_COMPAT_PLUCK(name))) \
+      { return std::ref(ev->FOLLY_LIBEVENT_COMPAT_PLUCK(name)); } \
+    inline auto event_ref_##name(struct event const* ev) -> \
+      decltype(std::cref(ev->FOLLY_LIBEVENT_COMPAT_PLUCK(name))) \
+      { return std::cref(ev->FOLLY_LIBEVENT_COMPAT_PLUCK(name)); } \
+    //
+
+FOLLY_LIBEVENT_DEF_ACCESSORS(flags)
+
+# undef FOLLY_LIBEVENT_COMPAT_PLUCK
+# undef FOLLY_LIBEVENT_DEF_ACCESSORS
+
 /**
  * low-level libevent utility functions
  */
@@ -35,7 +55,7 @@ class EventUtil {
       EVLIST_REGISTERED = (EVLIST_INSERTED | EVLIST_ACTIVE |
                            EVLIST_TIMEOUT | EVLIST_SIGNAL)
     };
-    return (ev->ev_flags & EVLIST_REGISTERED);
+    return (event_ref_flags(ev) & EVLIST_REGISTERED);
   }
 };