Cheaper bumpHandlingTime
[folly.git] / folly / io / async / EventHandler.cpp
index 6d410de62555f88716a97d64a6a4d545b02ffd3e..a0ac4a8fa6119efc5f6e8148547179002ecbcfa3 100644 (file)
@@ -1,4 +1,6 @@
 /*
+ * Copyright 2016 Facebook, Inc.
+ *
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements. See the NOTICE file
  * distributed with this work for additional information
@@ -16,8 +18,8 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-#include "folly/io/async/EventHandler.h"
-#include "folly/io/async/EventBase.h"
+#include <folly/io/async/EventHandler.h>
+#include <folly/io/async/EventBase.h>
 
 #include <assert.h>
 
@@ -47,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;
     }
 
@@ -65,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.
@@ -144,11 +147,21 @@ void EventHandler::ensureNotRegistered(const char* fn) {
 void EventHandler::libeventCallback(int fd, short events, void* arg) {
   EventHandler* handler = reinterpret_cast<EventHandler*>(arg);
   assert(fd == handler->event_.ev_fd);
+  (void)fd; // prevent unused variable warnings
+
+  auto observer = handler->eventBase_->getExecutionObserver();
+  if (observer) {
+    observer->starting(reinterpret_cast<uintptr_t>(handler));
+  }
 
   // this can't possibly fire if handler->eventBase_ is nullptr
-  (void) handler->eventBase_->bumpHandlingTime();
+  handler->eventBase_->bumpHandlingTime();
 
   handler->handlerReady(events);
+
+  if (observer) {
+    observer->stopped(reinterpret_cast<uintptr_t>(handler));
+  }
 }
 
 void EventHandler::setEventBase(EventBase* eventBase) {
@@ -157,7 +170,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;
     }