2 * Copyright 2015 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 <boost/noncopyable.hpp>
33 * The EventHandler class is used to asynchronously wait for events on a file
36 * Users that wish to wait on I/O events should derive from EventHandler and
37 * implement the handlerReady() method.
39 class EventHandler : private boost::noncopyable {
45 READ_WRITE = (READ | WRITE),
50 * Create a new EventHandler object.
52 * @param eventBase The EventBase to use to drive this event handler.
53 * This may be nullptr, in which case the EventBase must be
54 * set separately using initHandler() or attachEventBase()
55 * before the handler can be registered.
56 * @param fd The file descriptor that this EventHandler will
57 * monitor. This may be -1, in which case the file
58 * descriptor must be set separately using initHandler() or
59 * changeHandlerFD() before the handler can be registered.
61 explicit EventHandler(EventBase* eventBase = nullptr, int fd = -1);
64 * EventHandler destructor.
66 * The event will be automatically unregistered if it is still registered.
68 virtual ~EventHandler();
71 * handlerReady() is invoked when the handler is ready.
73 * @param events A bitset indicating the events that are ready.
75 virtual void handlerReady(uint16_t events) noexcept = 0;
78 * Register the handler.
80 * If the handler is already registered, the registration will be updated
81 * to wait on the new set of events.
83 * @param events A bitset specifying the events to monitor.
84 * If the PERSIST bit is set, the handler will remain
85 * registered even after handlerReady() is called.
87 * @return Returns true if the handler was successfully registered,
88 * or false if an error occurred. After an error, the handler is
89 * always unregistered, even if it was already registered prior to
90 * this call to registerHandler().
92 bool registerHandler(uint16_t events) {
93 return registerImpl(events, false);
97 * Unregister the handler, if it is registered.
99 void unregisterHandler();
102 * Returns true if the handler is currently registered.
104 bool isHandlerRegistered() const {
105 return EventUtil::isEventRegistered(&event_);
109 * Attach the handler to a EventBase.
111 * This may only be called if the handler is not currently attached to a
112 * EventBase (either by using the default constructor, or by calling
113 * detachEventBase()).
115 * This method must be invoked in the EventBase's thread.
117 void attachEventBase(EventBase* eventBase);
120 * Detach the handler from its EventBase.
122 * This may only be called when the handler is not currently registered.
123 * Once detached, the handler may not be registered again until it is
124 * re-attached to a EventBase by calling attachEventBase().
126 * This method must be called from the current EventBase's thread.
128 void detachEventBase();
131 * Change the file descriptor that this handler is associated with.
133 * This may only be called when the handler is not currently registered.
135 void changeHandlerFD(int fd);
138 * Attach the handler to a EventBase, and change the file descriptor.
140 * This method may only be called if the handler is not currently attached to
141 * a EventBase. This is primarily intended to be used to initialize
142 * EventHandler objects created using the default constructor.
144 void initHandler(EventBase* eventBase, int fd);
147 * Return the set of events that we're currently registered for.
149 uint16_t getRegisteredEvents() const {
150 return (isHandlerRegistered()) ?
151 event_.ev_events : 0;
155 * Register the handler as an internal event.
157 * This event will not count as an active event for determining if the
158 * EventBase loop has more events to process. The EventBase loop runs
159 * only as long as there are active EventHandlers, however "internal" event
160 * handlers are not counted. Therefore this event handler will not prevent
161 * EventBase loop from exiting with no more work to do if there are no other
162 * non-internal event handlers registered.
164 * This is intended to be used only in very rare cases by the internal
165 * EventBase code. This API is not guaranteed to remain stable or portable
168 bool registerInternalHandler(uint16_t events) {
169 return registerImpl(events, true);
172 bool isPending() const;
175 bool registerImpl(uint16_t events, bool internal);
176 void ensureNotRegistered(const char* fn);
178 void setEventBase(EventBase* eventBase);
180 static void libeventCallback(int fd, short events, void* arg);
183 EventBase* eventBase_;