xref: /aosp_15_r20/frameworks/native/include/android/looper.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker  * Copyright (C) 2010 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker  *
4*38e8c45fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker  *
8*38e8c45fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker  *
10*38e8c45fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker  * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker  */
16*38e8c45fSAndroid Build Coastguard Worker 
17*38e8c45fSAndroid Build Coastguard Worker /**
18*38e8c45fSAndroid Build Coastguard Worker  * @addtogroup Looper
19*38e8c45fSAndroid Build Coastguard Worker  * @{
20*38e8c45fSAndroid Build Coastguard Worker  */
21*38e8c45fSAndroid Build Coastguard Worker 
22*38e8c45fSAndroid Build Coastguard Worker /**
23*38e8c45fSAndroid Build Coastguard Worker  * @file looper.h
24*38e8c45fSAndroid Build Coastguard Worker  */
25*38e8c45fSAndroid Build Coastguard Worker 
26*38e8c45fSAndroid Build Coastguard Worker #ifndef ANDROID_LOOPER_H
27*38e8c45fSAndroid Build Coastguard Worker #define ANDROID_LOOPER_H
28*38e8c45fSAndroid Build Coastguard Worker 
29*38e8c45fSAndroid Build Coastguard Worker #include <sys/cdefs.h>
30*38e8c45fSAndroid Build Coastguard Worker 
31*38e8c45fSAndroid Build Coastguard Worker #ifdef __cplusplus
32*38e8c45fSAndroid Build Coastguard Worker extern "C" {
33*38e8c45fSAndroid Build Coastguard Worker #endif
34*38e8c45fSAndroid Build Coastguard Worker 
35*38e8c45fSAndroid Build Coastguard Worker // This file may also be built on glibc or on Windows/MacOS libc's, so
36*38e8c45fSAndroid Build Coastguard Worker // deprecated definitions are provided.
37*38e8c45fSAndroid Build Coastguard Worker #if !defined(__REMOVED_IN)
38*38e8c45fSAndroid Build Coastguard Worker #define __REMOVED_IN(__api_level, msg) __attribute__((__deprecated__(msg)))
39*38e8c45fSAndroid Build Coastguard Worker #endif
40*38e8c45fSAndroid Build Coastguard Worker 
41*38e8c45fSAndroid Build Coastguard Worker struct ALooper;
42*38e8c45fSAndroid Build Coastguard Worker /**
43*38e8c45fSAndroid Build Coastguard Worker  * ALooper
44*38e8c45fSAndroid Build Coastguard Worker  *
45*38e8c45fSAndroid Build Coastguard Worker  * A looper is the state tracking an event loop for a thread.
46*38e8c45fSAndroid Build Coastguard Worker  * Loopers do not define event structures or other such things; rather
47*38e8c45fSAndroid Build Coastguard Worker  * they are a lower-level facility to attach one or more discrete objects
48*38e8c45fSAndroid Build Coastguard Worker  * listening for an event.  An "event" here is simply data available on
49*38e8c45fSAndroid Build Coastguard Worker  * a file descriptor: each attached object has an associated file descriptor,
50*38e8c45fSAndroid Build Coastguard Worker  * and waiting for "events" means (internally) polling on all of these file
51*38e8c45fSAndroid Build Coastguard Worker  * descriptors until one or more of them have data available.
52*38e8c45fSAndroid Build Coastguard Worker  *
53*38e8c45fSAndroid Build Coastguard Worker  * A thread can have only one ALooper associated with it.
54*38e8c45fSAndroid Build Coastguard Worker  */
55*38e8c45fSAndroid Build Coastguard Worker typedef struct ALooper ALooper;
56*38e8c45fSAndroid Build Coastguard Worker 
57*38e8c45fSAndroid Build Coastguard Worker /**
58*38e8c45fSAndroid Build Coastguard Worker  * Returns the looper associated with the calling thread, or NULL if
59*38e8c45fSAndroid Build Coastguard Worker  * there is not one.
60*38e8c45fSAndroid Build Coastguard Worker  */
61*38e8c45fSAndroid Build Coastguard Worker ALooper* ALooper_forThread();
62*38e8c45fSAndroid Build Coastguard Worker 
63*38e8c45fSAndroid Build Coastguard Worker /** Option for for ALooper_prepare(). */
64*38e8c45fSAndroid Build Coastguard Worker enum {
65*38e8c45fSAndroid Build Coastguard Worker     /**
66*38e8c45fSAndroid Build Coastguard Worker      * This looper will accept calls to ALooper_addFd() that do not
67*38e8c45fSAndroid Build Coastguard Worker      * have a callback (that is provide NULL for the callback).  In
68*38e8c45fSAndroid Build Coastguard Worker      * this case the caller of ALooper_pollOnce() or ALooper_pollAll()
69*38e8c45fSAndroid Build Coastguard Worker      * MUST check the return from these functions to discover when
70*38e8c45fSAndroid Build Coastguard Worker      * data is available on such fds and process it.
71*38e8c45fSAndroid Build Coastguard Worker      */
72*38e8c45fSAndroid Build Coastguard Worker     ALOOPER_PREPARE_ALLOW_NON_CALLBACKS = 1<<0
73*38e8c45fSAndroid Build Coastguard Worker };
74*38e8c45fSAndroid Build Coastguard Worker 
75*38e8c45fSAndroid Build Coastguard Worker /**
76*38e8c45fSAndroid Build Coastguard Worker  * Prepares a looper associated with the calling thread, and returns it.
77*38e8c45fSAndroid Build Coastguard Worker  * If the thread already has a looper, it is returned.  Otherwise, a new
78*38e8c45fSAndroid Build Coastguard Worker  * one is created, associated with the thread, and returned.
79*38e8c45fSAndroid Build Coastguard Worker  *
80*38e8c45fSAndroid Build Coastguard Worker  * The opts may be ALOOPER_PREPARE_ALLOW_NON_CALLBACKS or 0.
81*38e8c45fSAndroid Build Coastguard Worker  */
82*38e8c45fSAndroid Build Coastguard Worker ALooper* ALooper_prepare(int opts);
83*38e8c45fSAndroid Build Coastguard Worker 
84*38e8c45fSAndroid Build Coastguard Worker /** Result from ALooper_pollOnce() and ALooper_pollAll(). */
85*38e8c45fSAndroid Build Coastguard Worker enum {
86*38e8c45fSAndroid Build Coastguard Worker     /**
87*38e8c45fSAndroid Build Coastguard Worker      * The poll was awoken using wake() before the timeout expired
88*38e8c45fSAndroid Build Coastguard Worker      * and no callbacks were executed and no other file descriptors were ready.
89*38e8c45fSAndroid Build Coastguard Worker      */
90*38e8c45fSAndroid Build Coastguard Worker     ALOOPER_POLL_WAKE = -1,
91*38e8c45fSAndroid Build Coastguard Worker 
92*38e8c45fSAndroid Build Coastguard Worker     /**
93*38e8c45fSAndroid Build Coastguard Worker      * Result from ALooper_pollOnce():
94*38e8c45fSAndroid Build Coastguard Worker      * One or more callbacks were executed. The poll may also have been
95*38e8c45fSAndroid Build Coastguard Worker      * explicitly woken by ALooper_wake().
96*38e8c45fSAndroid Build Coastguard Worker      */
97*38e8c45fSAndroid Build Coastguard Worker     ALOOPER_POLL_CALLBACK = -2,
98*38e8c45fSAndroid Build Coastguard Worker 
99*38e8c45fSAndroid Build Coastguard Worker     /**
100*38e8c45fSAndroid Build Coastguard Worker      * Result from ALooper_pollOnce() and ALooper_pollAll():
101*38e8c45fSAndroid Build Coastguard Worker      * The timeout expired. The poll may also have been explicitly woken by
102*38e8c45fSAndroid Build Coastguard Worker      * ALooper_wake().
103*38e8c45fSAndroid Build Coastguard Worker      */
104*38e8c45fSAndroid Build Coastguard Worker     ALOOPER_POLL_TIMEOUT = -3,
105*38e8c45fSAndroid Build Coastguard Worker 
106*38e8c45fSAndroid Build Coastguard Worker     /**
107*38e8c45fSAndroid Build Coastguard Worker      * Result from ALooper_pollOnce() and ALooper_pollAll():
108*38e8c45fSAndroid Build Coastguard Worker      * An error occurred. The poll may also have been explicitly woken by
109*38e8c45fSAndroid Build Coastguard Worker      * ALooper_wake(()).
110*38e8c45fSAndroid Build Coastguard Worker      */
111*38e8c45fSAndroid Build Coastguard Worker     ALOOPER_POLL_ERROR = -4,
112*38e8c45fSAndroid Build Coastguard Worker };
113*38e8c45fSAndroid Build Coastguard Worker 
114*38e8c45fSAndroid Build Coastguard Worker /**
115*38e8c45fSAndroid Build Coastguard Worker  * Acquire a reference on the given ALooper object.  This prevents the object
116*38e8c45fSAndroid Build Coastguard Worker  * from being deleted until the reference is removed.  This is only needed
117*38e8c45fSAndroid Build Coastguard Worker  * to safely hand an ALooper from one thread to another.
118*38e8c45fSAndroid Build Coastguard Worker  */
119*38e8c45fSAndroid Build Coastguard Worker void ALooper_acquire(ALooper* looper);
120*38e8c45fSAndroid Build Coastguard Worker 
121*38e8c45fSAndroid Build Coastguard Worker /**
122*38e8c45fSAndroid Build Coastguard Worker  * Remove a reference that was previously acquired with ALooper_acquire().
123*38e8c45fSAndroid Build Coastguard Worker  */
124*38e8c45fSAndroid Build Coastguard Worker void ALooper_release(ALooper* looper);
125*38e8c45fSAndroid Build Coastguard Worker 
126*38e8c45fSAndroid Build Coastguard Worker /**
127*38e8c45fSAndroid Build Coastguard Worker  * Flags for file descriptor events that a looper can monitor.
128*38e8c45fSAndroid Build Coastguard Worker  *
129*38e8c45fSAndroid Build Coastguard Worker  * These flag bits can be combined to monitor multiple events at once.
130*38e8c45fSAndroid Build Coastguard Worker  */
131*38e8c45fSAndroid Build Coastguard Worker enum {
132*38e8c45fSAndroid Build Coastguard Worker     /**
133*38e8c45fSAndroid Build Coastguard Worker      * The file descriptor is available for read operations.
134*38e8c45fSAndroid Build Coastguard Worker      */
135*38e8c45fSAndroid Build Coastguard Worker     ALOOPER_EVENT_INPUT = 1 << 0,
136*38e8c45fSAndroid Build Coastguard Worker 
137*38e8c45fSAndroid Build Coastguard Worker     /**
138*38e8c45fSAndroid Build Coastguard Worker      * The file descriptor is available for write operations.
139*38e8c45fSAndroid Build Coastguard Worker      */
140*38e8c45fSAndroid Build Coastguard Worker     ALOOPER_EVENT_OUTPUT = 1 << 1,
141*38e8c45fSAndroid Build Coastguard Worker 
142*38e8c45fSAndroid Build Coastguard Worker     /**
143*38e8c45fSAndroid Build Coastguard Worker      * The file descriptor has encountered an error condition.
144*38e8c45fSAndroid Build Coastguard Worker      *
145*38e8c45fSAndroid Build Coastguard Worker      * The looper always sends notifications about errors; it is not necessary
146*38e8c45fSAndroid Build Coastguard Worker      * to specify this event flag in the requested event set.
147*38e8c45fSAndroid Build Coastguard Worker      */
148*38e8c45fSAndroid Build Coastguard Worker     ALOOPER_EVENT_ERROR = 1 << 2,
149*38e8c45fSAndroid Build Coastguard Worker 
150*38e8c45fSAndroid Build Coastguard Worker     /**
151*38e8c45fSAndroid Build Coastguard Worker      * The file descriptor was hung up.
152*38e8c45fSAndroid Build Coastguard Worker      * For example, indicates that the remote end of a pipe or socket was closed.
153*38e8c45fSAndroid Build Coastguard Worker      *
154*38e8c45fSAndroid Build Coastguard Worker      * The looper always sends notifications about hangups; it is not necessary
155*38e8c45fSAndroid Build Coastguard Worker      * to specify this event flag in the requested event set.
156*38e8c45fSAndroid Build Coastguard Worker      */
157*38e8c45fSAndroid Build Coastguard Worker     ALOOPER_EVENT_HANGUP = 1 << 3,
158*38e8c45fSAndroid Build Coastguard Worker 
159*38e8c45fSAndroid Build Coastguard Worker     /**
160*38e8c45fSAndroid Build Coastguard Worker      * The file descriptor is invalid.
161*38e8c45fSAndroid Build Coastguard Worker      * For example, the file descriptor was closed prematurely.
162*38e8c45fSAndroid Build Coastguard Worker      *
163*38e8c45fSAndroid Build Coastguard Worker      * The looper always sends notifications about invalid file descriptors; it is not necessary
164*38e8c45fSAndroid Build Coastguard Worker      * to specify this event flag in the requested event set.
165*38e8c45fSAndroid Build Coastguard Worker      */
166*38e8c45fSAndroid Build Coastguard Worker     ALOOPER_EVENT_INVALID = 1 << 4,
167*38e8c45fSAndroid Build Coastguard Worker };
168*38e8c45fSAndroid Build Coastguard Worker 
169*38e8c45fSAndroid Build Coastguard Worker /**
170*38e8c45fSAndroid Build Coastguard Worker  * For callback-based event loops, this is the prototype of the function
171*38e8c45fSAndroid Build Coastguard Worker  * that is called when a file descriptor event occurs.
172*38e8c45fSAndroid Build Coastguard Worker  * It is given the file descriptor it is associated with,
173*38e8c45fSAndroid Build Coastguard Worker  * a bitmask of the poll events that were triggered (typically ALOOPER_EVENT_INPUT),
174*38e8c45fSAndroid Build Coastguard Worker  * and the data pointer that was originally supplied.
175*38e8c45fSAndroid Build Coastguard Worker  *
176*38e8c45fSAndroid Build Coastguard Worker  * Implementations should return 1 to continue receiving callbacks, or 0
177*38e8c45fSAndroid Build Coastguard Worker  * to have this file descriptor and callback unregistered from the looper.
178*38e8c45fSAndroid Build Coastguard Worker  */
179*38e8c45fSAndroid Build Coastguard Worker typedef int (*ALooper_callbackFunc)(int fd, int events, void* data);
180*38e8c45fSAndroid Build Coastguard Worker 
181*38e8c45fSAndroid Build Coastguard Worker /**
182*38e8c45fSAndroid Build Coastguard Worker  * Waits for events to be available, with optional timeout in milliseconds.
183*38e8c45fSAndroid Build Coastguard Worker  * Invokes callbacks for all file descriptors on which an event occurred.
184*38e8c45fSAndroid Build Coastguard Worker  *
185*38e8c45fSAndroid Build Coastguard Worker  * If the timeout is zero, returns immediately without blocking.
186*38e8c45fSAndroid Build Coastguard Worker  * If the timeout is negative, waits indefinitely until an event appears.
187*38e8c45fSAndroid Build Coastguard Worker  *
188*38e8c45fSAndroid Build Coastguard Worker  * **All return values may also imply ALOOPER_POLL_WAKE.** If you call this in a
189*38e8c45fSAndroid Build Coastguard Worker  * loop, you must treat all return values as if they also indicated
190*38e8c45fSAndroid Build Coastguard Worker  * ALOOPER_POLL_WAKE.
191*38e8c45fSAndroid Build Coastguard Worker  *
192*38e8c45fSAndroid Build Coastguard Worker  * Returns ALOOPER_POLL_WAKE if the poll was awoken using ALooper_wake() before
193*38e8c45fSAndroid Build Coastguard Worker  * the timeout expired and no callbacks were invoked and no other file
194*38e8c45fSAndroid Build Coastguard Worker  * descriptors were ready.
195*38e8c45fSAndroid Build Coastguard Worker  *
196*38e8c45fSAndroid Build Coastguard Worker  * Returns ALOOPER_POLL_CALLBACK if one or more callbacks were invoked. The poll
197*38e8c45fSAndroid Build Coastguard Worker  * may also have been explicitly woken by ALooper_wake.
198*38e8c45fSAndroid Build Coastguard Worker  *
199*38e8c45fSAndroid Build Coastguard Worker  * Returns ALOOPER_POLL_TIMEOUT if there was no data before the given timeout
200*38e8c45fSAndroid Build Coastguard Worker  * expired. The poll may also have been explicitly woken by ALooper_wake.
201*38e8c45fSAndroid Build Coastguard Worker  *
202*38e8c45fSAndroid Build Coastguard Worker  * Returns ALOOPER_POLL_ERROR if the calling thread has no associated Looper or
203*38e8c45fSAndroid Build Coastguard Worker  * for unrecoverable internal errors. The poll may also have been explicitly
204*38e8c45fSAndroid Build Coastguard Worker  * woken by ALooper_wake.
205*38e8c45fSAndroid Build Coastguard Worker  *
206*38e8c45fSAndroid Build Coastguard Worker  * Returns a value >= 0 containing an identifier (the same identifier `ident`
207*38e8c45fSAndroid Build Coastguard Worker  * passed to ALooper_addFd()) if its file descriptor has data and it has no
208*38e8c45fSAndroid Build Coastguard Worker  * callback function (requiring the caller here to handle it).  In this (and
209*38e8c45fSAndroid Build Coastguard Worker  * only this) case outFd, outEvents and outData will contain the poll events and
210*38e8c45fSAndroid Build Coastguard Worker  * data associated with the fd, otherwise they will be set to NULL. The poll may
211*38e8c45fSAndroid Build Coastguard Worker  * also have been explicitly woken by ALooper_wake.
212*38e8c45fSAndroid Build Coastguard Worker  *
213*38e8c45fSAndroid Build Coastguard Worker  * This method does not return until it has finished invoking the appropriate callbacks
214*38e8c45fSAndroid Build Coastguard Worker  * for all file descriptors that were signalled.
215*38e8c45fSAndroid Build Coastguard Worker  */
216*38e8c45fSAndroid Build Coastguard Worker int ALooper_pollOnce(int timeoutMillis, int* outFd, int* outEvents, void** outData);
217*38e8c45fSAndroid Build Coastguard Worker 
218*38e8c45fSAndroid Build Coastguard Worker /**
219*38e8c45fSAndroid Build Coastguard Worker  * Like ALooper_pollOnce(), but performs all pending callbacks until all
220*38e8c45fSAndroid Build Coastguard Worker  * data has been consumed or a file descriptor is available with no callback.
221*38e8c45fSAndroid Build Coastguard Worker  * This function will never return ALOOPER_POLL_CALLBACK.
222*38e8c45fSAndroid Build Coastguard Worker  *
223*38e8c45fSAndroid Build Coastguard Worker  * This API will not reliably respond to ALooper_wake. As such, this API is
224*38e8c45fSAndroid Build Coastguard Worker  * hidden and callers should migrate to ALooper_pollOnce. Binary compatibility
225*38e8c45fSAndroid Build Coastguard Worker  * is preserved to support already-compiled apps.
226*38e8c45fSAndroid Build Coastguard Worker  *
227*38e8c45fSAndroid Build Coastguard Worker  * \bug ALooper_pollAll will not wake in response to ALooper_wake calls if it
228*38e8c45fSAndroid Build Coastguard Worker  * also handles another event at the same time.
229*38e8c45fSAndroid Build Coastguard Worker  *
230*38e8c45fSAndroid Build Coastguard Worker  * \deprecated Calls to ALooper_pollAll should be replaced with
231*38e8c45fSAndroid Build Coastguard Worker  * ALooper_pollOnce. If you call ALooper_pollOnce in a loop, you *must* treat
232*38e8c45fSAndroid Build Coastguard Worker  * all return values as if they also indicate ALOOPER_POLL_WAKE.
233*38e8c45fSAndroid Build Coastguard Worker  */
234*38e8c45fSAndroid Build Coastguard Worker int ALooper_pollAll(int timeoutMillis, int* outFd, int* outEvents, void** outData)
235*38e8c45fSAndroid Build Coastguard Worker         __REMOVED_IN(1,
236*38e8c45fSAndroid Build Coastguard Worker                      "ALooper_pollAll may ignore wakes. Use ALooper_pollOnce instead. See "
237*38e8c45fSAndroid Build Coastguard Worker                      "The API documentation for more information");
238*38e8c45fSAndroid Build Coastguard Worker 
239*38e8c45fSAndroid Build Coastguard Worker /**
240*38e8c45fSAndroid Build Coastguard Worker  * Wakes the poll asynchronously.
241*38e8c45fSAndroid Build Coastguard Worker  *
242*38e8c45fSAndroid Build Coastguard Worker  * This method can be called on any thread.
243*38e8c45fSAndroid Build Coastguard Worker  * This method returns immediately.
244*38e8c45fSAndroid Build Coastguard Worker  *
245*38e8c45fSAndroid Build Coastguard Worker  * \bug ALooper_pollAll will not reliably wake in response to ALooper_wake.
246*38e8c45fSAndroid Build Coastguard Worker  */
247*38e8c45fSAndroid Build Coastguard Worker void ALooper_wake(ALooper* looper);
248*38e8c45fSAndroid Build Coastguard Worker 
249*38e8c45fSAndroid Build Coastguard Worker /**
250*38e8c45fSAndroid Build Coastguard Worker  * Adds a new file descriptor to be polled by the looper.
251*38e8c45fSAndroid Build Coastguard Worker  * If the same file descriptor was previously added, it is replaced.
252*38e8c45fSAndroid Build Coastguard Worker  *
253*38e8c45fSAndroid Build Coastguard Worker  * "fd" is the file descriptor to be added.
254*38e8c45fSAndroid Build Coastguard Worker  * "ident" is an identifier for this event, which is returned from ALooper_pollOnce().
255*38e8c45fSAndroid Build Coastguard Worker  * The identifier must be >= 0, or ALOOPER_POLL_CALLBACK if providing a non-NULL callback.
256*38e8c45fSAndroid Build Coastguard Worker  * "events" are the poll events to wake up on.  Typically this is ALOOPER_EVENT_INPUT.
257*38e8c45fSAndroid Build Coastguard Worker  * "callback" is the function to call when there is an event on the file descriptor.
258*38e8c45fSAndroid Build Coastguard Worker  * "data" is a private data pointer to supply to the callback.
259*38e8c45fSAndroid Build Coastguard Worker  *
260*38e8c45fSAndroid Build Coastguard Worker  * There are two main uses of this function:
261*38e8c45fSAndroid Build Coastguard Worker  *
262*38e8c45fSAndroid Build Coastguard Worker  * (1) If "callback" is non-NULL, then this function will be called when there is
263*38e8c45fSAndroid Build Coastguard Worker  * data on the file descriptor.  It should execute any events it has pending,
264*38e8c45fSAndroid Build Coastguard Worker  * appropriately reading from the file descriptor.  The 'ident' is ignored in this case.
265*38e8c45fSAndroid Build Coastguard Worker  *
266*38e8c45fSAndroid Build Coastguard Worker  * (2) If "callback" is NULL, the 'ident' will be returned by ALooper_pollOnce
267*38e8c45fSAndroid Build Coastguard Worker  * when its file descriptor has data available, requiring the caller to take
268*38e8c45fSAndroid Build Coastguard Worker  * care of processing it.
269*38e8c45fSAndroid Build Coastguard Worker  *
270*38e8c45fSAndroid Build Coastguard Worker  * Returns 1 if the file descriptor was added or -1 if an error occurred.
271*38e8c45fSAndroid Build Coastguard Worker  *
272*38e8c45fSAndroid Build Coastguard Worker  * This method can be called on any thread.
273*38e8c45fSAndroid Build Coastguard Worker  * This method may block briefly if it needs to wake the poll.
274*38e8c45fSAndroid Build Coastguard Worker  */
275*38e8c45fSAndroid Build Coastguard Worker int ALooper_addFd(ALooper* looper, int fd, int ident, int events,
276*38e8c45fSAndroid Build Coastguard Worker         ALooper_callbackFunc callback, void* data);
277*38e8c45fSAndroid Build Coastguard Worker 
278*38e8c45fSAndroid Build Coastguard Worker /**
279*38e8c45fSAndroid Build Coastguard Worker  * Removes a previously added file descriptor from the looper.
280*38e8c45fSAndroid Build Coastguard Worker  *
281*38e8c45fSAndroid Build Coastguard Worker  * When this method returns, it is safe to close the file descriptor since the looper
282*38e8c45fSAndroid Build Coastguard Worker  * will no longer have a reference to it.  However, it is possible for the callback to
283*38e8c45fSAndroid Build Coastguard Worker  * already be running or for it to run one last time if the file descriptor was already
284*38e8c45fSAndroid Build Coastguard Worker  * signalled.  Calling code is responsible for ensuring that this case is safely handled.
285*38e8c45fSAndroid Build Coastguard Worker  * For example, if the callback takes care of removing itself during its own execution either
286*38e8c45fSAndroid Build Coastguard Worker  * by returning 0 or by calling this method, then it can be guaranteed to not be invoked
287*38e8c45fSAndroid Build Coastguard Worker  * again at any later time unless registered anew.
288*38e8c45fSAndroid Build Coastguard Worker  *
289*38e8c45fSAndroid Build Coastguard Worker  * Returns 1 if the file descriptor was removed, 0 if none was previously registered
290*38e8c45fSAndroid Build Coastguard Worker  * or -1 if an error occurred.
291*38e8c45fSAndroid Build Coastguard Worker  *
292*38e8c45fSAndroid Build Coastguard Worker  * This method can be called on any thread.
293*38e8c45fSAndroid Build Coastguard Worker  * This method may block briefly if it needs to wake the poll.
294*38e8c45fSAndroid Build Coastguard Worker  */
295*38e8c45fSAndroid Build Coastguard Worker int ALooper_removeFd(ALooper* looper, int fd);
296*38e8c45fSAndroid Build Coastguard Worker 
297*38e8c45fSAndroid Build Coastguard Worker #ifdef __cplusplus
298*38e8c45fSAndroid Build Coastguard Worker };
299*38e8c45fSAndroid Build Coastguard Worker #endif
300*38e8c45fSAndroid Build Coastguard Worker 
301*38e8c45fSAndroid Build Coastguard Worker #endif // ANDROID_LOOPER_H
302*38e8c45fSAndroid Build Coastguard Worker 
303*38e8c45fSAndroid Build Coastguard Worker /** @} */
304