blob: bc616ebc7d536cf5638184864f03f3cf88b0c72c [file] [log] [blame]
Jeff Browne839a582010-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 Brown66d9df52010-06-13 19:35:19 -070023#include <sys/poll.h>
Jeff Browne839a582010-04-22 18:58:52 -070024
Dianne Hackbornefa10852010-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 Browne839a582010-04-22 18:58:52 -070035namespace android {
36
37/**
38 * A basic file descriptor polling loop based on poll() with callbacks.
39 */
Dianne Hackbornefa10852010-07-02 18:52:01 -070040class PollLoop : public ALooper {
Jeff Browne839a582010-04-22 18:58:52 -070041protected:
42 virtual ~PollLoop();
43
44public:
Dianne Hackborn3c5d1252010-07-07 14:27:31 -070045 PollLoop(bool allowNonCallbacks);
Jeff Browne839a582010-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 Hackborn3c5d1252010-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 Browne839a582010-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 Hackborn3c5d1252010-07-07 14:27:31 -070070 * Returns ALOOPER_POLL_CALLBACK if a callback was invoked.
Jeff Browne839a582010-04-22 18:58:52 -070071 *
Dianne Hackborn3c5d1252010-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 Browne839a582010-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 Hackborn3c5d1252010-07-07 14:27:31 -070088 int32_t pollOnce(int timeoutMillis, int* outEvents = NULL, void** outData = NULL);
Jeff Browne839a582010-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 Hackborn3c5d1252010-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 Browne839a582010-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 */
Dianne Hackborn45e0acb2010-09-07 15:28:30 -0700114 void setCallback(int fd, int ident, int events, Callback callback, void* data = NULL);
Jeff Browne839a582010-04-22 18:58:52 -0700115
116 /**
Dianne Hackborn45e0acb2010-09-07 15:28:30 -0700117 * Convenience for above setCallback when ident is not used. In this case
118 * the ident is set to POLL_CALLBACK.
119 */
120 void setCallback(int fd, int events, Callback callback, void* data = NULL);
121
122 /**
Dianne Hackbornefa10852010-07-02 18:52:01 -0700123 * Like setCallback(), but for the NDK callback function.
124 */
Dianne Hackborn45e0acb2010-09-07 15:28:30 -0700125 void setLooperCallback(int fd, int ident, int events, ALooper_callbackFunc* callback,
Dianne Hackborn3c5d1252010-07-07 14:27:31 -0700126 void* data);
Dianne Hackbornefa10852010-07-02 18:52:01 -0700127
128 /**
Jeff Browne839a582010-04-22 18:58:52 -0700129 * Removes the callback for a file descriptor, if one exists.
130 *
131 * When this method returns, it is safe to close the file descriptor since the poll loop
132 * will no longer have a reference to it. However, it is possible for the callback to
133 * already be running or for it to run one last time if the file descriptor was already
134 * signalled. Calling code is responsible for ensuring that this case is safely handled.
135 * For example, if the callback takes care of removing itself during its own execution either
136 * by returning false or calling this method, then it can be guaranteed to not be invoked
137 * again at any later time unless registered anew.
138 *
139 * This method can be called on any thread.
140 * This method may block briefly if it needs to wake the poll loop.
141 *
142 * Returns true if a callback was actually removed, false if none was registered.
143 */
144 bool removeCallback(int fd);
145
Dianne Hackbornefa10852010-07-02 18:52:01 -0700146 /**
147 * Set the given PollLoop to be associated with the
148 * calling thread. There must be a 1:1 relationship between
149 * PollLoop and thread.
150 */
151 static void setForThread(const sp<PollLoop>& pollLoop);
152
153 /**
154 * Return the PollLoop associated with the calling thread.
155 */
156 static sp<PollLoop> getForThread();
157
Jeff Browne839a582010-04-22 18:58:52 -0700158private:
159 struct RequestedCallback {
160 Callback callback;
Dianne Hackbornefa10852010-07-02 18:52:01 -0700161 ALooper_callbackFunc* looperCallback;
Dianne Hackborn45e0acb2010-09-07 15:28:30 -0700162 int ident;
Jeff Browne839a582010-04-22 18:58:52 -0700163 void* data;
164 };
165
166 struct PendingCallback {
167 int fd;
Dianne Hackborn45e0acb2010-09-07 15:28:30 -0700168 int ident;
Jeff Browne839a582010-04-22 18:58:52 -0700169 int events;
170 Callback callback;
Dianne Hackbornefa10852010-07-02 18:52:01 -0700171 ALooper_callbackFunc* looperCallback;
Jeff Browne839a582010-04-22 18:58:52 -0700172 void* data;
173 };
Dianne Hackborn3c5d1252010-07-07 14:27:31 -0700174
175 const bool mAllowNonCallbacks;
176
Jeff Browne839a582010-04-22 18:58:52 -0700177 Mutex mLock;
Jeff Browne839a582010-04-22 18:58:52 -0700178 bool mPolling;
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700179 uint32_t mWaiters;
180 Condition mAwake;
181 Condition mResume;
Jeff Browne839a582010-04-22 18:58:52 -0700182
183 int mWakeReadPipeFd;
184 int mWakeWritePipeFd;
185
186 Vector<struct pollfd> mRequestedFds;
187 Vector<RequestedCallback> mRequestedCallbacks;
188
189 Vector<PendingCallback> mPendingCallbacks; // used privately by pollOnce
Dianne Hackborn3c5d1252010-07-07 14:27:31 -0700190 Vector<PendingCallback> mPendingFds; // used privately by pollOnce
191 size_t mPendingFdsPos;
192
Jeff Browne839a582010-04-22 18:58:52 -0700193 void openWakePipe();
194 void closeWakePipe();
195
Dianne Hackborn45e0acb2010-09-07 15:28:30 -0700196 void setCallbackCommon(int fd, int ident, int events, Callback callback,
Dianne Hackbornefa10852010-07-02 18:52:01 -0700197 ALooper_callbackFunc* looperCallback, void* data);
Jeff Browne839a582010-04-22 18:58:52 -0700198 ssize_t getRequestIndexLocked(int fd);
199 void wakeAndLock();
Dianne Hackbornefa10852010-07-02 18:52:01 -0700200 static void threadDestructor(void *st);
Jeff Browne839a582010-04-22 18:58:52 -0700201};
202
203} // namespace android
204
205#endif // UTILS_POLL_LOOP_H