• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef UTILS_LOOPER_H
18 #define UTILS_LOOPER_H
19 
20 #include <utils/threads.h>
21 #include <utils/RefBase.h>
22 #include <utils/KeyedVector.h>
23 #include <utils/Timers.h>
24 
25 #include <sys/epoll.h>
26 
27 #include <android-base/unique_fd.h>
28 
29 #include <utility>
30 
31 namespace android {
32 
33 /*
34  * NOTE: Since Looper is used to implement the NDK ALooper, the Looper
35  * enums and the signature of Looper_callbackFunc need to align with
36  * that implementation.
37  */
38 
39 /**
40  * For callback-based event loops, this is the prototype of the function
41  * that is called when a file descriptor event occurs.
42  * It is given the file descriptor it is associated with,
43  * a bitmask of the poll events that were triggered (typically EVENT_INPUT),
44  * and the data pointer that was originally supplied.
45  *
46  * Implementations should return 1 to continue receiving callbacks, or 0
47  * to have this file descriptor and callback unregistered from the looper.
48  */
49 typedef int (*Looper_callbackFunc)(int fd, int events, void* data);
50 
51 /**
52  * A message that can be posted to a Looper.
53  */
54 struct Message {
MessageMessage55     Message() : what(0) { }
MessageMessage56     Message(int w) : what(w) { }
57 
58     /* The message type. (interpretation is left up to the handler) */
59     int what;
60 };
61 
62 
63 /**
64  * Interface for a Looper message handler.
65  *
66  * The Looper holds a strong reference to the message handler whenever it has
67  * a message to deliver to it.  Make sure to call Looper::removeMessages
68  * to remove any pending messages destined for the handler so that the handler
69  * can be destroyed.
70  */
71 class MessageHandler : public virtual RefBase {
72 protected:
73     virtual ~MessageHandler();
74 
75 public:
76     /**
77      * Handles a message.
78      */
79     virtual void handleMessage(const Message& message) = 0;
80 };
81 
82 
83 /**
84  * A simple proxy that holds a weak reference to a message handler.
85  */
86 class WeakMessageHandler : public MessageHandler {
87 protected:
88     virtual ~WeakMessageHandler();
89 
90 public:
91     WeakMessageHandler(const wp<MessageHandler>& handler);
92     virtual void handleMessage(const Message& message);
93 
94 private:
95     wp<MessageHandler> mHandler;
96 };
97 
98 
99 /**
100  * A looper callback.
101  */
102 class LooperCallback : public virtual RefBase {
103 protected:
104     virtual ~LooperCallback();
105 
106 public:
107     /**
108      * Handles a poll event for the given file descriptor.
109      * It is given the file descriptor it is associated with,
110      * a bitmask of the poll events that were triggered (typically EVENT_INPUT),
111      * and the data pointer that was originally supplied.
112      *
113      * Implementations should return 1 to continue receiving callbacks, or 0
114      * to have this file descriptor and callback unregistered from the looper.
115      */
116     virtual int handleEvent(int fd, int events, void* data) = 0;
117 };
118 
119 /**
120  * Wraps a Looper_callbackFunc function pointer.
121  */
122 class SimpleLooperCallback : public LooperCallback {
123 protected:
124     virtual ~SimpleLooperCallback();
125 
126 public:
127     SimpleLooperCallback(Looper_callbackFunc callback);
128     virtual int handleEvent(int fd, int events, void* data);
129 
130 private:
131     Looper_callbackFunc mCallback;
132 };
133 
134 /**
135  * A polling loop that supports monitoring file descriptor events, optionally
136  * using callbacks.  The implementation uses epoll() internally.
137  *
138  * A looper can be associated with a thread although there is no requirement that it must be.
139  */
140 class Looper : public RefBase {
141 protected:
142     virtual ~Looper();
143 
144 public:
145     enum {
146         /**
147          * Result from Looper_pollOnce() and Looper_pollAll():
148          * The poll was awoken using wake() before the timeout expired
149          * and no callbacks were executed and no other file descriptors were ready.
150          */
151         POLL_WAKE = -1,
152 
153         /**
154          * Result from Looper_pollOnce() and Looper_pollAll():
155          * One or more callbacks were executed.
156          */
157         POLL_CALLBACK = -2,
158 
159         /**
160          * Result from Looper_pollOnce() and Looper_pollAll():
161          * The timeout expired.
162          */
163         POLL_TIMEOUT = -3,
164 
165         /**
166          * Result from Looper_pollOnce() and Looper_pollAll():
167          * An error occurred.
168          */
169         POLL_ERROR = -4,
170     };
171 
172     /**
173      * Flags for file descriptor events that a looper can monitor.
174      *
175      * These flag bits can be combined to monitor multiple events at once.
176      */
177     enum {
178         /**
179          * The file descriptor is available for read operations.
180          */
181         EVENT_INPUT = 1 << 0,
182 
183         /**
184          * The file descriptor is available for write operations.
185          */
186         EVENT_OUTPUT = 1 << 1,
187 
188         /**
189          * The file descriptor has encountered an error condition.
190          *
191          * The looper always sends notifications about errors; it is not necessary
192          * to specify this event flag in the requested event set.
193          */
194         EVENT_ERROR = 1 << 2,
195 
196         /**
197          * The file descriptor was hung up.
198          * For example, indicates that the remote end of a pipe or socket was closed.
199          *
200          * The looper always sends notifications about hangups; it is not necessary
201          * to specify this event flag in the requested event set.
202          */
203         EVENT_HANGUP = 1 << 3,
204 
205         /**
206          * The file descriptor is invalid.
207          * For example, the file descriptor was closed prematurely.
208          *
209          * The looper always sends notifications about invalid file descriptors; it is not necessary
210          * to specify this event flag in the requested event set.
211          */
212         EVENT_INVALID = 1 << 4,
213     };
214 
215     enum {
216         /**
217          * Option for Looper_prepare: this looper will accept calls to
218          * Looper_addFd() that do not have a callback (that is provide NULL
219          * for the callback).  In this case the caller of Looper_pollOnce()
220          * or Looper_pollAll() MUST check the return from these functions to
221          * discover when data is available on such fds and process it.
222          */
223         PREPARE_ALLOW_NON_CALLBACKS = 1<<0
224     };
225 
226     /**
227      * Creates a looper.
228      *
229      * If allowNonCallbaks is true, the looper will allow file descriptors to be
230      * registered without associated callbacks.  This assumes that the caller of
231      * pollOnce() is prepared to handle callback-less events itself.
232      */
233     Looper(bool allowNonCallbacks);
234 
235     /**
236      * Returns whether this looper instance allows the registration of file descriptors
237      * using identifiers instead of callbacks.
238      */
239     bool getAllowNonCallbacks() const;
240 
241     /**
242      * Waits for events to be available, with optional timeout in milliseconds.
243      * Invokes callbacks for all file descriptors on which an event occurred.
244      *
245      * If the timeout is zero, returns immediately without blocking.
246      * If the timeout is negative, waits indefinitely until an event appears.
247      *
248      * Returns POLL_WAKE if the poll was awoken using wake() before
249      * the timeout expired and no callbacks were invoked and no other file
250      * descriptors were ready.
251      *
252      * Returns POLL_CALLBACK if one or more callbacks were invoked.
253      *
254      * Returns POLL_TIMEOUT if there was no data before the given
255      * timeout expired.
256      *
257      * Returns POLL_ERROR if an error occurred.
258      *
259      * Returns a value >= 0 containing an identifier if its file descriptor has data
260      * and it has no callback function (requiring the caller here to handle it).
261      * In this (and only this) case outFd, outEvents and outData will contain the poll
262      * events and data associated with the fd, otherwise they will be set to NULL.
263      *
264      * This method does not return until it has finished invoking the appropriate callbacks
265      * for all file descriptors that were signalled.
266      */
267     int pollOnce(int timeoutMillis, int* outFd, int* outEvents, void** outData);
pollOnce(int timeoutMillis)268     inline int pollOnce(int timeoutMillis) {
269         return pollOnce(timeoutMillis, nullptr, nullptr, nullptr);
270     }
271 
272     /**
273      * Like pollOnce(), but performs all pending callbacks until all
274      * data has been consumed or a file descriptor is available with no callback.
275      * This function will never return POLL_CALLBACK.
276      */
277     int pollAll(int timeoutMillis, int* outFd, int* outEvents, void** outData);
pollAll(int timeoutMillis)278     inline int pollAll(int timeoutMillis) {
279         return pollAll(timeoutMillis, nullptr, nullptr, nullptr);
280     }
281 
282     /**
283      * Wakes the poll asynchronously.
284      *
285      * This method can be called on any thread.
286      * This method returns immediately.
287      */
288     void wake();
289 
290     /**
291      * Adds a new file descriptor to be polled by the looper.
292      * If the same file descriptor was previously added, it is replaced.
293      *
294      * "fd" is the file descriptor to be added.
295      * "ident" is an identifier for this event, which is returned from pollOnce().
296      * The identifier must be >= 0, or POLL_CALLBACK if providing a non-NULL callback.
297      * "events" are the poll events to wake up on.  Typically this is EVENT_INPUT.
298      * "callback" is the function to call when there is an event on the file descriptor.
299      * "data" is a private data pointer to supply to the callback.
300      *
301      * There are two main uses of this function:
302      *
303      * (1) If "callback" is non-NULL, then this function will be called when there is
304      * data on the file descriptor.  It should execute any events it has pending,
305      * appropriately reading from the file descriptor.  The 'ident' is ignored in this case.
306      *
307      * (2) If "callback" is NULL, the 'ident' will be returned by Looper_pollOnce
308      * when its file descriptor has data available, requiring the caller to take
309      * care of processing it.
310      *
311      * Returns 1 if the file descriptor was added, 0 if the arguments were invalid.
312      *
313      * This method can be called on any thread.
314      * This method may block briefly if it needs to wake the poll.
315      *
316      * The callback may either be specified as a bare function pointer or as a smart
317      * pointer callback object.  The smart pointer should be preferred because it is
318      * easier to avoid races when the callback is removed from a different thread.
319      * See removeFd() for details.
320      */
321     int addFd(int fd, int ident, int events, Looper_callbackFunc callback, void* data);
322     int addFd(int fd, int ident, int events, const sp<LooperCallback>& callback, void* data);
323 
324     /**
325      * Removes a previously added file descriptor from the looper.
326      *
327      * When this method returns, it is safe to close the file descriptor since the looper
328      * will no longer have a reference to it.  However, it is possible for the callback to
329      * already be running or for it to run one last time if the file descriptor was already
330      * signalled.  Calling code is responsible for ensuring that this case is safely handled.
331      * For example, if the callback takes care of removing itself during its own execution either
332      * by returning 0 or by calling this method, then it can be guaranteed to not be invoked
333      * again at any later time unless registered anew.
334      *
335      * A simple way to avoid this problem is to use the version of addFd() that takes
336      * a sp<LooperCallback> instead of a bare function pointer.  The LooperCallback will
337      * be released at the appropriate time by the Looper.
338      *
339      * Returns 1 if the file descriptor was removed, 0 if none was previously registered.
340      *
341      * This method can be called on any thread.
342      * This method may block briefly if it needs to wake the poll.
343      */
344     int removeFd(int fd);
345 
346     /**
347      * Enqueues a message to be processed by the specified handler.
348      *
349      * The handler must not be null.
350      * This method can be called on any thread.
351      */
352     void sendMessage(const sp<MessageHandler>& handler, const Message& message);
353 
354     /**
355      * Enqueues a message to be processed by the specified handler after all pending messages
356      * after the specified delay.
357      *
358      * The time delay is specified in uptime nanoseconds.
359      * The handler must not be null.
360      * This method can be called on any thread.
361      */
362     void sendMessageDelayed(nsecs_t uptimeDelay, const sp<MessageHandler>& handler,
363             const Message& message);
364 
365     /**
366      * Enqueues a message to be processed by the specified handler after all pending messages
367      * at the specified time.
368      *
369      * The time is specified in uptime nanoseconds.
370      * The handler must not be null.
371      * This method can be called on any thread.
372      */
373     void sendMessageAtTime(nsecs_t uptime, const sp<MessageHandler>& handler,
374             const Message& message);
375 
376     /**
377      * Removes all messages for the specified handler from the queue.
378      *
379      * The handler must not be null.
380      * This method can be called on any thread.
381      */
382     void removeMessages(const sp<MessageHandler>& handler);
383 
384     /**
385      * Removes all messages of a particular type for the specified handler from the queue.
386      *
387      * The handler must not be null.
388      * This method can be called on any thread.
389      */
390     void removeMessages(const sp<MessageHandler>& handler, int what);
391 
392     /**
393      * Returns whether this looper's thread is currently polling for more work to do.
394      * This is a good signal that the loop is still alive rather than being stuck
395      * handling a callback.  Note that this method is intrinsically racy, since the
396      * state of the loop can change before you get the result back.
397      */
398     bool isPolling() const;
399 
400     /**
401      * Prepares a looper associated with the calling thread, and returns it.
402      * If the thread already has a looper, it is returned.  Otherwise, a new
403      * one is created, associated with the thread, and returned.
404      *
405      * The opts may be PREPARE_ALLOW_NON_CALLBACKS or 0.
406      */
407     static sp<Looper> prepare(int opts);
408 
409     /**
410      * Sets the given looper to be associated with the calling thread.
411      * If another looper is already associated with the thread, it is replaced.
412      *
413      * If "looper" is NULL, removes the currently associated looper.
414      */
415     static void setForThread(const sp<Looper>& looper);
416 
417     /**
418      * Returns the looper associated with the calling thread, or NULL if
419      * there is not one.
420      */
421     static sp<Looper> getForThread();
422 
423 private:
424     struct Request {
425         int fd;
426         int ident;
427         int events;
428         int seq;
429         sp<LooperCallback> callback;
430         void* data;
431 
432         void initEventItem(struct epoll_event* eventItem) const;
433     };
434 
435     struct Response {
436         int events;
437         Request request;
438     };
439 
440     struct MessageEnvelope {
MessageEnvelopeMessageEnvelope441         MessageEnvelope() : uptime(0) { }
442 
MessageEnvelopeMessageEnvelope443         MessageEnvelope(nsecs_t u, sp<MessageHandler> h, const Message& m)
444             : uptime(u), handler(std::move(h)), message(m) {}
445 
446         nsecs_t uptime;
447         sp<MessageHandler> handler;
448         Message message;
449     };
450 
451     const bool mAllowNonCallbacks; // immutable
452 
453     android::base::unique_fd mWakeEventFd;  // immutable
454     Mutex mLock;
455 
456     Vector<MessageEnvelope> mMessageEnvelopes; // guarded by mLock
457     bool mSendingMessage; // guarded by mLock
458 
459     // Whether we are currently waiting for work.  Not protected by a lock,
460     // any use of it is racy anyway.
461     volatile bool mPolling;
462 
463     android::base::unique_fd mEpollFd;  // guarded by mLock but only modified on the looper thread
464     bool mEpollRebuildRequired; // guarded by mLock
465 
466     // Locked list of file descriptor monitoring requests.
467     KeyedVector<int, Request> mRequests;  // guarded by mLock
468     int mNextRequestSeq;
469 
470     // This state is only used privately by pollOnce and does not require a lock since
471     // it runs on a single thread.
472     Vector<Response> mResponses;
473     size_t mResponseIndex;
474     nsecs_t mNextMessageUptime; // set to LLONG_MAX when none
475 
476     int pollInner(int timeoutMillis);
477     int removeFd(int fd, int seq);
478     void awoken();
479     void pushResponse(int events, const Request& request);
480     void rebuildEpollLocked();
481     void scheduleEpollRebuildLocked();
482 
483     static void initTLSKey();
484     static void threadDestructor(void *st);
485     static void initEpollEvent(struct epoll_event* eventItem);
486 };
487 
488 } // namespace android
489 
490 #endif // UTILS_LOOPER_H
491