looper.cpp revision 682674154e3fe88f6061245145f934f25f1a2eb8
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() {
31    sp<PollLoop> loop = PollLoop::getForThread();
32    if (loop == NULL) {
33        loop = new PollLoop();
34        PollLoop::setForThread(loop);
35    }
36    return loop.get();
37}
38
39int32_t ALooper_pollOnce(int timeoutMillis) {
40    sp<PollLoop> loop = PollLoop::getForThread();
41    if (loop == NULL) {
42        LOGW("ALooper_pollOnce: No looper for this thread!");
43        return -1;
44    }
45    return loop->pollOnce(timeoutMillis) ? 1 : 0;
46}
47
48void ALooper_acquire(ALooper* looper) {
49    static_cast<PollLoop*>(looper)->incStrong((void*)ALooper_acquire);
50}
51
52void ALooper_release(ALooper* looper) {
53    static_cast<PollLoop*>(looper)->decStrong((void*)ALooper_acquire);
54}
55
56void ALooper_setCallback(ALooper* looper, int fd, int events,
57        ALooper_callbackFunc* callback, void* data) {
58    static_cast<PollLoop*>(looper)->setLooperCallback(fd, events, callback, data);
59}
60
61int32_t ALooper_removeCallback(ALooper* looper, int fd) {
62    return static_cast<PollLoop*>(looper)->removeCallback(fd) ? 1 : 0;
63}
64