blob: b3651caeacb9c7e57f85095bd3ef0830e85d7264 [file] [log] [blame]
Jeff Brown46b9ac02010-04-22 18:58:52 -07001/*
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_POLL_LOOP_H
18#define UTILS_POLL_LOOP_H
19
20#include <utils/Vector.h>
21#include <utils/threads.h>
22
Jeff Brown43a95272010-06-13 19:35:19 -070023#include <sys/poll.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070024
Dianne Hackborn68267412010-07-02 18:52:01 -070025#include <android/looper.h>
26
27struct ALooper : public android::RefBase {
28protected:
29 virtual ~ALooper() { }
30
31public:
32 ALooper() { }
33};
34
Jeff Brown46b9ac02010-04-22 18:58:52 -070035namespace android {
36
37/**
38 * A basic file descriptor polling loop based on poll() with callbacks.
39 */
Dianne Hackborn68267412010-07-02 18:52:01 -070040class PollLoop : public ALooper {
Jeff Brown46b9ac02010-04-22 18:58:52 -070041protected:
42 virtual ~PollLoop();
43
44public:
45 PollLoop();
46
47 /**
48 * A callback that it to be invoked when an event occurs on a file descriptor.
49 * Specifies the events that were triggered and the user data provided when the
50 * callback was set.
51 *
52 * Returns true if the callback should be kept, false if it should be removed automatically
53 * after the callback returns.
54 */
55 typedef bool (*Callback)(int fd, int events, void* data);
56
57 /**
58 * Performs a single call to poll() with optional timeout in milliseconds.
59 * Invokes callbacks for all file descriptors on which an event occurred.
60 *
61 * If the timeout is zero, returns immediately without blocking.
62 * If the timeout is negative, waits indefinitely until awoken.
63 *
64 * Returns true if a callback was invoked or if the loop was awoken by wake().
65 * Returns false if a timeout or error occurred.
66 *
67 * This method must only be called on the main thread.
68 * This method blocks until either a file descriptor is signalled, a timeout occurs,
69 * or wake() is called.
70 * This method does not return until it has finished invoking the appropriate callbacks
71 * for all file descriptors that were signalled.
72 */
73 bool pollOnce(int timeoutMillis);
74
75 /**
76 * Wakes the loop asynchronously.
77 *
78 * This method can be called on any thread.
79 * This method returns immediately.
80 */
81 void wake();
82
83 /**
84 * Sets the callback for a file descriptor, replacing the existing one, if any.
85 * It is an error to call this method with events == 0 or callback == NULL.
86 *
87 * Note that a callback can be invoked with the POLLERR, POLLHUP or POLLNVAL events
88 * even if it is not explicitly requested when registered.
89 *
90 * This method can be called on any thread.
91 * This method may block briefly if it needs to wake the poll loop.
92 */
93 void setCallback(int fd, int events, Callback callback, void* data = NULL);
94
95 /**
Dianne Hackborn68267412010-07-02 18:52:01 -070096 * Like setCallback(), but for the NDK callback function.
97 */
98 void setLooperCallback(int fd, int events, ALooper_callbackFunc* callback, void* data);
99
100 /**
Jeff Brown46b9ac02010-04-22 18:58:52 -0700101 * Removes the callback for a file descriptor, if one exists.
102 *
103 * When this method returns, it is safe to close the file descriptor since the poll loop
104 * will no longer have a reference to it. However, it is possible for the callback to
105 * already be running or for it to run one last time if the file descriptor was already
106 * signalled. Calling code is responsible for ensuring that this case is safely handled.
107 * For example, if the callback takes care of removing itself during its own execution either
108 * by returning false or calling this method, then it can be guaranteed to not be invoked
109 * again at any later time unless registered anew.
110 *
111 * This method can be called on any thread.
112 * This method may block briefly if it needs to wake the poll loop.
113 *
114 * Returns true if a callback was actually removed, false if none was registered.
115 */
116 bool removeCallback(int fd);
117
Dianne Hackborn68267412010-07-02 18:52:01 -0700118 /**
119 * Set the given PollLoop to be associated with the
120 * calling thread. There must be a 1:1 relationship between
121 * PollLoop and thread.
122 */
123 static void setForThread(const sp<PollLoop>& pollLoop);
124
125 /**
126 * Return the PollLoop associated with the calling thread.
127 */
128 static sp<PollLoop> getForThread();
129
Jeff Brown46b9ac02010-04-22 18:58:52 -0700130private:
131 struct RequestedCallback {
132 Callback callback;
Dianne Hackborn68267412010-07-02 18:52:01 -0700133 ALooper_callbackFunc* looperCallback;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700134 void* data;
135 };
136
137 struct PendingCallback {
138 int fd;
139 int events;
140 Callback callback;
Dianne Hackborn68267412010-07-02 18:52:01 -0700141 ALooper_callbackFunc* looperCallback;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700142 void* data;
143 };
144
145 Mutex mLock;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700146 bool mPolling;
Jeff Brown5c225b12010-06-16 01:53:36 -0700147 uint32_t mWaiters;
148 Condition mAwake;
149 Condition mResume;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700150
151 int mWakeReadPipeFd;
152 int mWakeWritePipeFd;
153
154 Vector<struct pollfd> mRequestedFds;
155 Vector<RequestedCallback> mRequestedCallbacks;
156
157 Vector<PendingCallback> mPendingCallbacks; // used privately by pollOnce
158
159 void openWakePipe();
160 void closeWakePipe();
161
Dianne Hackborn68267412010-07-02 18:52:01 -0700162 void setCallbackCommon(int fd, int events, Callback callback,
163 ALooper_callbackFunc* looperCallback, void* data);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700164 ssize_t getRequestIndexLocked(int fd);
165 void wakeAndLock();
Dianne Hackborn68267412010-07-02 18:52:01 -0700166 static void threadDestructor(void *st);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700167};
168
169} // namespace android
170
171#endif // UTILS_POLL_LOOP_H