Dianne Hackborn | 6826741 | 2010-07-02 18:52:01 -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 | #define LOG_TAG "ALooper" |
| 18 | #include <utils/Log.h> |
| 19 | |
| 20 | #include <android/looper.h> |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 21 | #include <utils/Looper.h> |
Jeff Brown | c70bed0 | 2011-01-10 14:36:37 -0800 | [diff] [blame] | 22 | #include <binder/IPCThreadState.h> |
Dianne Hackborn | 6826741 | 2010-07-02 18:52:01 -0700 | [diff] [blame] | 23 | |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 24 | using android::Looper; |
Dianne Hackborn | 6826741 | 2010-07-02 18:52:01 -0700 | [diff] [blame] | 25 | using android::sp; |
Jeff Brown | c70bed0 | 2011-01-10 14:36:37 -0800 | [diff] [blame] | 26 | using android::IPCThreadState; |
Dianne Hackborn | 6826741 | 2010-07-02 18:52:01 -0700 | [diff] [blame] | 27 | |
| 28 | ALooper* ALooper_forThread() { |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 29 | return Looper::getForThread().get(); |
Dianne Hackborn | 6826741 | 2010-07-02 18:52:01 -0700 | [diff] [blame] | 30 | } |
| 31 | |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 32 | ALooper* ALooper_prepare(int opts) { |
| 33 | return Looper::prepare(opts).get(); |
Dianne Hackborn | 6826741 | 2010-07-02 18:52:01 -0700 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | void ALooper_acquire(ALooper* looper) { |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 37 | static_cast<Looper*>(looper)->incStrong((void*)ALooper_acquire); |
Dianne Hackborn | 6826741 | 2010-07-02 18:52:01 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | void ALooper_release(ALooper* looper) { |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 41 | static_cast<Looper*>(looper)->decStrong((void*)ALooper_acquire); |
Dianne Hackborn | 6826741 | 2010-07-02 18:52:01 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 44 | int ALooper_pollOnce(int timeoutMillis, int* outFd, int* outEvents, void** outData) { |
| 45 | sp<Looper> looper = Looper::getForThread(); |
| 46 | if (looper == NULL) { |
| 47 | LOGE("ALooper_pollOnce: No looper for this thread!"); |
| 48 | return ALOOPER_POLL_ERROR; |
| 49 | } |
| 50 | |
Jeff Brown | c70bed0 | 2011-01-10 14:36:37 -0800 | [diff] [blame] | 51 | IPCThreadState::self()->flushCommands(); |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 52 | return looper->pollOnce(timeoutMillis, outFd, outEvents, outData); |
Dianne Hackborn | 6826741 | 2010-07-02 18:52:01 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 55 | int ALooper_pollAll(int timeoutMillis, int* outFd, int* outEvents, void** outData) { |
| 56 | sp<Looper> looper = Looper::getForThread(); |
| 57 | if (looper == NULL) { |
| 58 | LOGE("ALooper_pollAll: No looper for this thread!"); |
| 59 | return ALOOPER_POLL_ERROR; |
| 60 | } |
Jeff Brown | c70bed0 | 2011-01-10 14:36:37 -0800 | [diff] [blame] | 61 | |
| 62 | IPCThreadState::self()->flushCommands(); |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 63 | return looper->pollAll(timeoutMillis, outFd, outEvents, outData); |
| 64 | } |
| 65 | |
| 66 | void ALooper_wake(ALooper* looper) { |
| 67 | static_cast<Looper*>(looper)->wake(); |
| 68 | } |
| 69 | |
| 70 | int ALooper_addFd(ALooper* looper, int fd, int ident, int events, |
| 71 | ALooper_callbackFunc callback, void* data) { |
| 72 | return static_cast<Looper*>(looper)->addFd(fd, ident, events, callback, data); |
| 73 | } |
| 74 | |
| 75 | int ALooper_removeFd(ALooper* looper, int fd) { |
| 76 | return static_cast<Looper*>(looper)->removeFd(fd); |
Dianne Hackborn | 6826741 | 2010-07-02 18:52:01 -0700 | [diff] [blame] | 77 | } |