Example #1
0
bool EventHandler::registerImpl(uint16_t events, bool internal) {
  assert(event_.ev_base != nullptr);

  // We have to unregister the event before we can change the event flags
  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>(flags & EVLIST_INTERNAL) == internal) {
      return true;
    }

    event_del(&event_);
  }

  // Update the event flags
  // Unfortunately, event_set() resets the event_base, so we have to remember
  // it before hand, then pass it back into event_base_set() afterwards
  struct event_base* evb = event_.ev_base;
  event_set(&event_, event_.ev_fd, events,
            &EventHandler::libeventCallback, this);
  event_base_set(evb, &event_);

  // Set EVLIST_INTERNAL if this is an internal event
  if (internal) {
    event_ref_flags(&event_) |= EVLIST_INTERNAL;
  }

  // Add the event.
  //
  // Although libevent allows events to wait on both I/O and a timeout,
  // we intentionally don't allow an EventHandler to also use a timeout.
  // Callers must maintain a separate AsyncTimeout object if they want a
  // timeout.
  //
  // Otherwise, it is difficult to handle persistent events properly.  (The I/O
  // event and timeout may both fire together the same time around the event
  // loop.  Normally we would want to inform the caller of the I/O event first,
  // then the timeout.  However, it is difficult to do this properly since the
  // I/O callback could delete the EventHandler.)  Additionally, if a caller
  // uses the same struct event for both I/O and timeout, and they just want to
  // reschedule the timeout, libevent currently makes an epoll_ctl() call even
  // if the I/O event flags haven't changed.  Using a separate event struct is
  // therefore slightly more efficient in this case (although it does take up
  // more space).
  if (event_add(&event_, nullptr) < 0) {
    LOG(ERROR) << "EventBase: failed to register event handler for fd "
               << event_.ev_fd << ": " << strerror(errno);
    // Call event_del() to make sure the event is completely uninstalled
    event_del(&event_);
    return false;
  }

  return true;
}
Example #2
0
bool EventHandler::isPending() const {
  if (event_ref_flags(&event_) & EVLIST_ACTIVE) {
    if (event_.ev_res & EV_READ) {
      return true;
    }
  }
  return false;
}
Example #3
0
void AsyncTimeout::libeventCallback(libevent_fd_t fd, short events, void* arg) {
  AsyncTimeout* timeout = reinterpret_cast<AsyncTimeout*>(arg);
  assert(libeventFdToFd(fd) == -1);
  assert(events == EV_TIMEOUT);
  // prevent unused variable warnings
  (void)fd;
  (void)events;

  // double check that ev_flags gets reset when the timeout is not running
  assert((event_ref_flags(&timeout->event_) & ~EVLIST_INTERNAL) == EVLIST_INIT);

  // this can't possibly fire if timeout->eventBase_ is nullptr
  timeout->timeoutManager_->bumpHandlingTime();

  RequestContextScopeGuard rctx(timeout->context_);

  timeout->timeoutExpired();
}