looper.cpp revision 42c03e579aade011b451e2a13ea3f44a2ef0056a
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/PollLoop.h> 22 23using android::PollLoop; 24using android::sp; 25 26ALooper* ALooper_forThread() { 27 return PollLoop::getForThread().get(); 28} 29 30ALooper* ALooper_prepare(int32_t opts) { 31 bool allowFds = (opts&ALOOPER_PREPARE_ALLOW_NON_CALLBACKS) != 0; 32 sp<PollLoop> loop = PollLoop::getForThread(); 33 if (loop == NULL) { 34 loop = new PollLoop(allowFds); 35 PollLoop::setForThread(loop); 36 } 37 if (loop->getAllowNonCallbacks() != allowFds) { 38 LOGW("ALooper_prepare again with different ALOOPER_PREPARE_ALLOW_NON_CALLBACKS"); 39 } 40 return loop.get(); 41} 42 43int32_t ALooper_pollOnce(int timeoutMillis, int* outEvents, void** outData) { 44 sp<PollLoop> loop = PollLoop::getForThread(); 45 if (loop == NULL) { 46 LOGW("ALooper_pollOnce: No looper for this thread!"); 47 return -1; 48 } 49 return loop->pollOnce(timeoutMillis, outEvents, outData); 50} 51 52int32_t ALooper_pollAll(int timeoutMillis, int* outEvents, void** outData) { 53 sp<PollLoop> loop = PollLoop::getForThread(); 54 if (loop == NULL) { 55 LOGW("ALooper_pollOnce: No looper for this thread!"); 56 return -1; 57 } 58 59 int32_t result; 60 while ((result = loop->pollOnce(timeoutMillis, outEvents, outData)) == ALOOPER_POLL_CALLBACK) { 61 ; 62 } 63 64 return result; 65} 66 67void ALooper_acquire(ALooper* looper) { 68 static_cast<PollLoop*>(looper)->incStrong((void*)ALooper_acquire); 69} 70 71void ALooper_release(ALooper* looper) { 72 static_cast<PollLoop*>(looper)->decStrong((void*)ALooper_acquire); 73} 74 75void ALooper_addFd(ALooper* looper, int fd, int ident, int events, 76 ALooper_callbackFunc* callback, void* data) { 77 static_cast<PollLoop*>(looper)->setLooperCallback(fd, ident, events, callback, data); 78} 79 80int32_t ALooper_removeFd(ALooper* looper, int fd) { 81 return static_cast<PollLoop*>(looper)->removeCallback(fd) ? 1 : 0; 82} 83