Home | History | Annotate | Download | only in android
      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>
     21 #include <utils/Looper.h>
     22 #include <binder/IPCThreadState.h>
     23 
     24 using android::Looper;
     25 using android::sp;
     26 using android::IPCThreadState;
     27 
     28 static inline Looper* ALooper_to_Looper(ALooper* alooper) {
     29     return reinterpret_cast<Looper*>(alooper);
     30 }
     31 
     32 static inline ALooper* Looper_to_ALooper(Looper* looper) {
     33     return reinterpret_cast<ALooper*>(looper);
     34 }
     35 
     36 ALooper* ALooper_forThread() {
     37     return Looper_to_ALooper(Looper::getForThread().get());
     38 }
     39 
     40 ALooper* ALooper_prepare(int opts) {
     41     return Looper_to_ALooper(Looper::prepare(opts).get());
     42 }
     43 
     44 void ALooper_acquire(ALooper* looper) {
     45     ALooper_to_Looper(looper)->incStrong((void*)ALooper_acquire);
     46 }
     47 
     48 void ALooper_release(ALooper* looper) {
     49     ALooper_to_Looper(looper)->decStrong((void*)ALooper_acquire);
     50 }
     51 
     52 int ALooper_pollOnce(int timeoutMillis, int* outFd, int* outEvents, void** outData) {
     53     sp<Looper> looper = Looper::getForThread();
     54     if (looper == NULL) {
     55         ALOGE("ALooper_pollOnce: No looper for this thread!");
     56         return ALOOPER_POLL_ERROR;
     57     }
     58 
     59     IPCThreadState::self()->flushCommands();
     60     return looper->pollOnce(timeoutMillis, outFd, outEvents, outData);
     61 }
     62 
     63 int ALooper_pollAll(int timeoutMillis, int* outFd, int* outEvents, void** outData) {
     64     sp<Looper> looper = Looper::getForThread();
     65     if (looper == NULL) {
     66         ALOGE("ALooper_pollAll: No looper for this thread!");
     67         return ALOOPER_POLL_ERROR;
     68     }
     69 
     70     IPCThreadState::self()->flushCommands();
     71     return looper->pollAll(timeoutMillis, outFd, outEvents, outData);
     72 }
     73 
     74 void ALooper_wake(ALooper* looper) {
     75     ALooper_to_Looper(looper)->wake();
     76 }
     77 
     78 int ALooper_addFd(ALooper* looper, int fd, int ident, int events,
     79         ALooper_callbackFunc callback, void* data) {
     80     return ALooper_to_Looper(looper)->addFd(fd, ident, events, callback, data);
     81 }
     82 
     83 int ALooper_removeFd(ALooper* looper, int fd) {
     84     return ALooper_to_Looper(looper)->removeFd(fd);
     85 }
     86