2 * Copyright 2016 Facebook, Inc.
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
23 #include <glog/logging.h>
24 #include <folly/io/async/EventUtil.h>
25 #include <folly/portability/Event.h>
26 #include <boost/noncopyable.hpp>
34 * The EventHandler class is used to asynchronously wait for events on a file
37 * Users that wish to wait on I/O events should derive from EventHandler and
38 * implement the handlerReady() method.
40 class EventHandler : private boost::noncopyable {
46 READ_WRITE = (READ | WRITE),
51 * Create a new EventHandler object.
53 * @param eventBase The EventBase to use to drive this event handler.
54 * This may be nullptr, in which case the EventBase must be
55 * set separately using initHandler() or attachEventBase()
56 * before the handler can be registered.
57 * @param fd The file descriptor that this EventHandler will
58 * monitor. This may be -1, in which case the file
59 * descriptor must be set separately using initHandler() or
60 * changeHandlerFD() before the handler can be registered.
62 explicit EventHandler(EventBase* eventBase = nullptr, int fd = -1);
65 * EventHandler destructor.
67 * The event will be automatically unregistered if it is still registered.
69 virtual ~EventHandler();
72 * handlerReady() is invoked when the handler is ready.
74 * @param events A bitset indicating the events that are ready.
76 virtual void handlerReady(uint16_t events) noexcept = 0;
79 * Register the handler.
81 * If the handler is already registered, the registration will be updated
82 * to wait on the new set of events.
84 * @param events A bitset specifying the events to monitor.
85 * If the PERSIST bit is set, the handler will remain
86 * registered even after handlerReady() is called.
88 * @return Returns true if the handler was successfully registered,
89 * or false if an error occurred. After an error, the handler is
90 * always unregistered, even if it was already registered prior to
91 * this call to registerHandler().
93 bool registerHandler(uint16_t events) {
94 return registerImpl(events, false);
98 * Unregister the handler, if it is registered.
100 void unregisterHandler();
103 * Returns true if the handler is currently registered.
105 bool isHandlerRegistered() const {
106 return EventUtil::isEventRegistered(&event_);
110 * Attach the handler to a EventBase.
112 * This may only be called if the handler is not currently attached to a
113 * EventBase (either by using the default constructor, or by calling
114 * detachEventBase()).
116 * This method must be invoked in the EventBase's thread.
118 void attachEventBase(EventBase* eventBase);
121 * Detach the handler from its EventBase.
123 * This may only be called when the handler is not currently registered.
124 * Once detached, the handler may not be registered again until it is
125 * re-attached to a EventBase by calling attachEventBase().
127 * This method must be called from the current EventBase's thread.
129 void detachEventBase();
132 * Change the file descriptor that this handler is associated with.
134 * This may only be called when the handler is not currently registered.
136 void changeHandlerFD(int fd);
139 * Attach the handler to a EventBase, and change the file descriptor.
141 * This method may only be called if the handler is not currently attached to
142 * a EventBase. This is primarily intended to be used to initialize
143 * EventHandler objects created using the default constructor.
145 void initHandler(EventBase* eventBase, int fd);
148 * Return the set of events that we're currently registered for.
150 uint16_t getRegisteredEvents() const {
151 return (isHandlerRegistered()) ?
152 event_.ev_events : 0;
156 * Register the handler as an internal event.
158 * This event will not count as an active event for determining if the
159 * EventBase loop has more events to process. The EventBase loop runs
160 * only as long as there are active EventHandlers, however "internal" event
161 * handlers are not counted. Therefore this event handler will not prevent
162 * EventBase loop from exiting with no more work to do if there are no other
163 * non-internal event handlers registered.
165 * This is intended to be used only in very rare cases by the internal
166 * EventBase code. This API is not guaranteed to remain stable or portable
169 bool registerInternalHandler(uint16_t events) {
170 return registerImpl(events, true);
173 bool isPending() const;
176 bool registerImpl(uint16_t events, bool internal);
177 void ensureNotRegistered(const char* fn);
179 void setEventBase(EventBase* eventBase);
181 static void libeventCallback(libevent_fd_t fd, short events, void* arg);
184 EventBase* eventBase_;