Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 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_POLL_LOOP_H |
| 18 | #define UTILS_POLL_LOOP_H |
| 19 | |
| 20 | #include <utils/Vector.h> |
| 21 | #include <utils/threads.h> |
| 22 | |
Jeff Brown | 66d9df5 | 2010-06-13 19:35:19 -0700 | [diff] [blame] | 23 | #include <sys/poll.h> |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 24 | |
Dianne Hackborn | efa1085 | 2010-07-02 18:52:01 -0700 | [diff] [blame] | 25 | #include <android/looper.h> |
| 26 | |
| 27 | struct ALooper : public android::RefBase { |
| 28 | protected: |
| 29 | virtual ~ALooper() { } |
| 30 | |
| 31 | public: |
| 32 | ALooper() { } |
| 33 | }; |
| 34 | |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 35 | namespace android { |
| 36 | |
| 37 | /** |
| 38 | * A basic file descriptor polling loop based on poll() with callbacks. |
| 39 | */ |
Dianne Hackborn | efa1085 | 2010-07-02 18:52:01 -0700 | [diff] [blame] | 40 | class PollLoop : public ALooper { |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 41 | protected: |
| 42 | virtual ~PollLoop(); |
| 43 | |
| 44 | public: |
Dianne Hackborn | 3c5d125 | 2010-07-07 14:27:31 -0700 | [diff] [blame] | 45 | PollLoop(bool allowNonCallbacks); |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 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 | |
Dianne Hackborn | 3c5d125 | 2010-07-07 14:27:31 -0700 | [diff] [blame] | 57 | enum { |
| 58 | POLL_CALLBACK = ALOOPER_POLL_CALLBACK, |
| 59 | POLL_TIMEOUT = ALOOPER_POLL_TIMEOUT, |
| 60 | POLL_ERROR = ALOOPER_POLL_ERROR, |
| 61 | }; |
| 62 | |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 63 | /** |
| 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 Hackborn | 3c5d125 | 2010-07-07 14:27:31 -0700 | [diff] [blame] | 70 | * Returns ALOOPER_POLL_CALLBACK if a callback was invoked. |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 71 | * |
Dianne Hackborn | 3c5d125 | 2010-07-07 14:27:31 -0700 | [diff] [blame] | 72 | * 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 Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 83 | * 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 Hackborn | 3c5d125 | 2010-07-07 14:27:31 -0700 | [diff] [blame] | 88 | int32_t pollOnce(int timeoutMillis, int* outEvents = NULL, void** outData = NULL); |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 89 | |
| 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 Hackborn | 3c5d125 | 2010-07-07 14:27:31 -0700 | [diff] [blame] | 99 | * Control whether this PollLoop instance allows using IDs instead |
| 100 | * of callbacks. |
| 101 | */ |
| 102 | bool getAllowNonCallbacks() const; |
| 103 | |
| 104 | /** |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 105 | * 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 Hackborn | 45e0acb | 2010-09-07 15:28:30 -0700 | [diff] [blame^] | 114 | void setCallback(int fd, int ident, int events, Callback callback, void* data = NULL); |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 115 | |
| 116 | /** |
Dianne Hackborn | 45e0acb | 2010-09-07 15:28:30 -0700 | [diff] [blame^] | 117 | * 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 Hackborn | efa1085 | 2010-07-02 18:52:01 -0700 | [diff] [blame] | 123 | * Like setCallback(), but for the NDK callback function. |
| 124 | */ |
Dianne Hackborn | 45e0acb | 2010-09-07 15:28:30 -0700 | [diff] [blame^] | 125 | void setLooperCallback(int fd, int ident, int events, ALooper_callbackFunc* callback, |
Dianne Hackborn | 3c5d125 | 2010-07-07 14:27:31 -0700 | [diff] [blame] | 126 | void* data); |
Dianne Hackborn | efa1085 | 2010-07-02 18:52:01 -0700 | [diff] [blame] | 127 | |
| 128 | /** |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 129 | * 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 Hackborn | efa1085 | 2010-07-02 18:52:01 -0700 | [diff] [blame] | 146 | /** |
| 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 Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 158 | private: |
| 159 | struct RequestedCallback { |
| 160 | Callback callback; |
Dianne Hackborn | efa1085 | 2010-07-02 18:52:01 -0700 | [diff] [blame] | 161 | ALooper_callbackFunc* looperCallback; |
Dianne Hackborn | 45e0acb | 2010-09-07 15:28:30 -0700 | [diff] [blame^] | 162 | int ident; |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 163 | void* data; |
| 164 | }; |
| 165 | |
| 166 | struct PendingCallback { |
| 167 | int fd; |
Dianne Hackborn | 45e0acb | 2010-09-07 15:28:30 -0700 | [diff] [blame^] | 168 | int ident; |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 169 | int events; |
| 170 | Callback callback; |
Dianne Hackborn | efa1085 | 2010-07-02 18:52:01 -0700 | [diff] [blame] | 171 | ALooper_callbackFunc* looperCallback; |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 172 | void* data; |
| 173 | }; |
Dianne Hackborn | 3c5d125 | 2010-07-07 14:27:31 -0700 | [diff] [blame] | 174 | |
| 175 | const bool mAllowNonCallbacks; |
| 176 | |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 177 | Mutex mLock; |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 178 | bool mPolling; |
Jeff Brown | f4a4ec2 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 179 | uint32_t mWaiters; |
| 180 | Condition mAwake; |
| 181 | Condition mResume; |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 182 | |
| 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 Hackborn | 3c5d125 | 2010-07-07 14:27:31 -0700 | [diff] [blame] | 190 | Vector<PendingCallback> mPendingFds; // used privately by pollOnce |
| 191 | size_t mPendingFdsPos; |
| 192 | |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 193 | void openWakePipe(); |
| 194 | void closeWakePipe(); |
| 195 | |
Dianne Hackborn | 45e0acb | 2010-09-07 15:28:30 -0700 | [diff] [blame^] | 196 | void setCallbackCommon(int fd, int ident, int events, Callback callback, |
Dianne Hackborn | efa1085 | 2010-07-02 18:52:01 -0700 | [diff] [blame] | 197 | ALooper_callbackFunc* looperCallback, void* data); |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 198 | ssize_t getRequestIndexLocked(int fd); |
| 199 | void wakeAndLock(); |
Dianne Hackborn | efa1085 | 2010-07-02 18:52:01 -0700 | [diff] [blame] | 200 | static void threadDestructor(void *st); |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 201 | }; |
| 202 | |
| 203 | } // namespace android |
| 204 | |
| 205 | #endif // UTILS_POLL_LOOP_H |