blob: 24cb2342501d8e17fe3e50086f9744162a8682ec [file] [log] [blame]
Dianne Hackborn68267412010-07-02 18:52:01 -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#define LOG_TAG "ALooper"
18#include <utils/Log.h>
19
20#include <android/looper.h>
Jeff Brown4fe6c3e2010-09-13 23:17:30 -070021#include <utils/Looper.h>
Jeff Brownc70bed02011-01-10 14:36:37 -080022#include <binder/IPCThreadState.h>
Dianne Hackborn68267412010-07-02 18:52:01 -070023
Jeff Brown4fe6c3e2010-09-13 23:17:30 -070024using android::Looper;
Dianne Hackborn68267412010-07-02 18:52:01 -070025using android::sp;
Jeff Brownc70bed02011-01-10 14:36:37 -080026using android::IPCThreadState;
Dianne Hackborn68267412010-07-02 18:52:01 -070027
Brian Carlstrom82b007d2013-12-12 23:12:55 -080028static inline Looper* ALooper_to_Looper(ALooper* alooper) {
29 return reinterpret_cast<Looper*>(alooper);
30}
31
32static inline ALooper* Looper_to_ALooper(Looper* looper) {
33 return reinterpret_cast<ALooper*>(looper);
34}
35
Dianne Hackborn68267412010-07-02 18:52:01 -070036ALooper* ALooper_forThread() {
Brian Carlstrom82b007d2013-12-12 23:12:55 -080037 return Looper_to_ALooper(Looper::getForThread().get());
Dianne Hackborn68267412010-07-02 18:52:01 -070038}
39
Jeff Brown4fe6c3e2010-09-13 23:17:30 -070040ALooper* ALooper_prepare(int opts) {
Brian Carlstrom82b007d2013-12-12 23:12:55 -080041 return Looper_to_ALooper(Looper::prepare(opts).get());
Dianne Hackborn68267412010-07-02 18:52:01 -070042}
43
44void ALooper_acquire(ALooper* looper) {
Brian Carlstrom82b007d2013-12-12 23:12:55 -080045 ALooper_to_Looper(looper)->incStrong((void*)ALooper_acquire);
Dianne Hackborn68267412010-07-02 18:52:01 -070046}
47
48void ALooper_release(ALooper* looper) {
Brian Carlstrom82b007d2013-12-12 23:12:55 -080049 ALooper_to_Looper(looper)->decStrong((void*)ALooper_acquire);
Dianne Hackborn68267412010-07-02 18:52:01 -070050}
51
Jeff Brown4fe6c3e2010-09-13 23:17:30 -070052int ALooper_pollOnce(int timeoutMillis, int* outFd, int* outEvents, void** outData) {
53 sp<Looper> looper = Looper::getForThread();
54 if (looper == NULL) {
Steve Block3762c312012-01-06 19:20:56 +000055 ALOGE("ALooper_pollOnce: No looper for this thread!");
Jeff Brown4fe6c3e2010-09-13 23:17:30 -070056 return ALOOPER_POLL_ERROR;
57 }
58
Jeff Brownc70bed02011-01-10 14:36:37 -080059 IPCThreadState::self()->flushCommands();
Jeff Brown4fe6c3e2010-09-13 23:17:30 -070060 return looper->pollOnce(timeoutMillis, outFd, outEvents, outData);
Dianne Hackborn68267412010-07-02 18:52:01 -070061}
62
Jeff Brown4fe6c3e2010-09-13 23:17:30 -070063int ALooper_pollAll(int timeoutMillis, int* outFd, int* outEvents, void** outData) {
64 sp<Looper> looper = Looper::getForThread();
65 if (looper == NULL) {
Steve Block3762c312012-01-06 19:20:56 +000066 ALOGE("ALooper_pollAll: No looper for this thread!");
Jeff Brown4fe6c3e2010-09-13 23:17:30 -070067 return ALOOPER_POLL_ERROR;
68 }
Jeff Brownc70bed02011-01-10 14:36:37 -080069
70 IPCThreadState::self()->flushCommands();
Jeff Brown4fe6c3e2010-09-13 23:17:30 -070071 return looper->pollAll(timeoutMillis, outFd, outEvents, outData);
72}
73
74void ALooper_wake(ALooper* looper) {
Brian Carlstrom82b007d2013-12-12 23:12:55 -080075 ALooper_to_Looper(looper)->wake();
Jeff Brown4fe6c3e2010-09-13 23:17:30 -070076}
77
78int ALooper_addFd(ALooper* looper, int fd, int ident, int events,
79 ALooper_callbackFunc callback, void* data) {
Brian Carlstrom82b007d2013-12-12 23:12:55 -080080 return ALooper_to_Looper(looper)->addFd(fd, ident, events, callback, data);
Jeff Brown4fe6c3e2010-09-13 23:17:30 -070081}
82
83int ALooper_removeFd(ALooper* looper, int fd) {
Brian Carlstrom82b007d2013-12-12 23:12:55 -080084 return ALooper_to_Looper(looper)->removeFd(fd);
Dianne Hackborn68267412010-07-02 18:52:01 -070085}