/*
- * Copyright 2014 Facebook, Inc.
+ * Copyright 2004-present 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
- * regarding copyright ownership. The ASF licenses this file
- * to you 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
+ * 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.
+ * 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.
*/
+
#include <folly/io/async/EventHandler.h>
#include <folly/io/async/EventBase.h>
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 {
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;
}
// 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_set(
+ &event_,
+ event_.ev_fd,
+ short(events),
+ &EventHandler::libeventCallback,
+ this);
event_base_set(evb, &event_);
// 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.
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
+
+ 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);
+ handler->handlerReady(uint16_t(events));
+
+ if (observer) {
+ observer->stopped(reinterpret_cast<uintptr_t>(handler));
+ }
}
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;
}