blob: 81230e84dd988dced1992bdc4cee0a208f85b2ed [file] [log] [blame]
Jeff Brown46b9ac0a2010-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 Brown46b9ac0a2010-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 Brown46b9ac0a2010-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 Brown46b9ac0a2010-04-22 18:58:52 -070041protected:
42 virtual ~PollLoop();
43
44public:
Dianne Hackborn85448bb2010-07-07 14:27:31 -070045 PollLoop(bool allowNonCallbacks);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070046
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
Dianne Hackborn85448bb2010-07-07 14:27:31 -070057 enum {
58 POLL_CALLBACK = ALOOPER_POLL_CALLBACK,
59 POLL_TIMEOUT = ALOOPER_POLL_TIMEOUT,
60 POLL_ERROR = ALOOPER_POLL_ERROR,
61 };
62
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070063 /**
64 * Performs a single call to poll() with optional timeout in milliseconds.
65 * Invokes callbacks for all file descriptors on which an event occurred.
66 *
67 * If the timeout is zero, returns immediately without blocking.
68 * If the timeout is negative, waits indefinitely until awoken.
69 *
Dianne Hackborn85448bb2010-07-07 14:27:31 -070070 * Returns ALOOPER_POLL_CALLBACK if a callback was invoked.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070071 *
Dianne Hackborn85448bb2010-07-07 14:27:31 -070072 * Returns ALOOPER_POLL_TIMEOUT if there was no data before the given
73 * timeout expired.
74 *
75 * Returns ALOPER_POLL_ERROR if an error occurred.
76 *
77 * Returns a value >= 0 containing a file descriptor if it has data
78 * and it has no callback function (requiring the caller here to handle it).
79 * In this (and only this) case outEvents and outData will contain the poll
80 * events and data associated with the fd.
81 *
82 * This method must only be called on the thread owning the PollLoop.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070083 * This method blocks until either a file descriptor is signalled, a timeout occurs,
84 * or wake() is called.
85 * This method does not return until it has finished invoking the appropriate callbacks
86 * for all file descriptors that were signalled.
87 */
Dianne Hackborn85448bb2010-07-07 14:27:31 -070088 int32_t pollOnce(int timeoutMillis, int* outEvents = NULL, void** outData = NULL);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070089
90 /**
91 * Wakes the loop asynchronously.
92 *
93 * This method can be called on any thread.
94 * This method returns immediately.
95 */
96 void wake();
97
98 /**
Dianne Hackborn85448bb2010-07-07 14:27:31 -070099 * Control whether this PollLoop instance allows using IDs instead
100 * of callbacks.
101 */
102 bool getAllowNonCallbacks() const;
103
104 /**
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700105 * Sets the callback for a file descriptor, replacing the existing one, if any.
106 * It is an error to call this method with events == 0 or callback == NULL.
107 *
108 * Note that a callback can be invoked with the POLLERR, POLLHUP or POLLNVAL events
109 * even if it is not explicitly requested when registered.
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 void setCallback(int fd, int events, Callback callback, void* data = NULL);
115
116 /**
Dianne Hackborn68267412010-07-02 18:52:01 -0700117 * Like setCallback(), but for the NDK callback function.
118 */
Dianne Hackborn85448bb2010-07-07 14:27:31 -0700119 void setLooperCallback(int fd, int events, ALooper_callbackFunc* callback,
120 void* data);
Dianne Hackborn68267412010-07-02 18:52:01 -0700121
122 /**
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700123 * Removes the callback for a file descriptor, if one exists.
124 *
125 * When this method returns, it is safe to close the file descriptor since the poll loop
126 * will no longer have a reference to it. However, it is possible for the callback to
127 * already be running or for it to run one last time if the file descriptor was already
128 * signalled. Calling code is responsible for ensuring that this case is safely handled.
129 * For example, if the callback takes care of removing itself during its own execution either
130 * by returning false or calling this method, then it can be guaranteed to not be invoked
131 * again at any later time unless registered anew.
132 *
133 * This method can be called on any thread.
134 * This method may block briefly if it needs to wake the poll loop.
135 *
136 * Returns true if a callback was actually removed, false if none was registered.
137 */
138 bool removeCallback(int fd);
139
Dianne Hackborn68267412010-07-02 18:52:01 -0700140 /**
141 * Set the given PollLoop to be associated with the
142 * calling thread. There must be a 1:1 relationship between
143 * PollLoop and thread.
144 */
145 static void setForThread(const sp<PollLoop>& pollLoop);
146
147 /**
148 * Return the PollLoop associated with the calling thread.
149 */
150 static sp<PollLoop> getForThread();
151
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700152private:
153 struct RequestedCallback {
154 Callback callback;
Dianne Hackborn68267412010-07-02 18:52:01 -0700155 ALooper_callbackFunc* looperCallback;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700156 void* data;
157 };
158
159 struct PendingCallback {
160 int fd;
161 int events;
162 Callback callback;
Dianne Hackborn68267412010-07-02 18:52:01 -0700163 ALooper_callbackFunc* looperCallback;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700164 void* data;
165 };
Dianne Hackborn85448bb2010-07-07 14:27:31 -0700166
167 const bool mAllowNonCallbacks;
168
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700169 Mutex mLock;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700170 bool mPolling;
Jeff Brown5c225b12010-06-16 01:53:36 -0700171 uint32_t mWaiters;
172 Condition mAwake;
173 Condition mResume;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700174
175 int mWakeReadPipeFd;
176 int mWakeWritePipeFd;
177
178 Vector<struct pollfd> mRequestedFds;
179 Vector<RequestedCallback> mRequestedCallbacks;
180
181 Vector<PendingCallback> mPendingCallbacks; // used privately by pollOnce
Dianne Hackborn85448bb2010-07-07 14:27:31 -0700182 Vector<PendingCallback> mPendingFds; // used privately by pollOnce
183 size_t mPendingFdsPos;
184
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700185 void openWakePipe();
186 void closeWakePipe();
187
Dianne Hackborn68267412010-07-02 18:52:01 -0700188 void setCallbackCommon(int fd, int events, Callback callback,
189 ALooper_callbackFunc* looperCallback, void* data);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700190 ssize_t getRequestIndexLocked(int fd);
191 void wakeAndLock();
Dianne Hackborn68267412010-07-02 18:52:01 -0700192 static void threadDestructor(void *st);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700193};
194
195} // namespace android
196
197#endif // UTILS_POLL_LOOP_H