ALooper.h revision 42d7f83904b8a91ce33e87a44de109572679ac1f
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#ifndef A_LOOPER_H_
18
19#define A_LOOPER_H_
20
21#include <media/stagefright/foundation/ABase.h>
22#include <utils/Errors.h>
23#include <utils/KeyedVector.h>
24#include <utils/List.h>
25#include <utils/RefBase.h>
26#include <utils/threads.h>
27
28namespace android {
29
30struct AHandler;
31struct AMessage;
32
33struct ALooper : public RefBase {
34    typedef int32_t event_id;
35    typedef int32_t handler_id;
36
37    ALooper();
38
39    handler_id registerHandler(const sp<AHandler> &handler);
40    void unregisterHandler(handler_id handlerID);
41
42    status_t start(
43            bool runOnCallingThread = false,
44            bool canCallJava = false);
45
46    status_t stop();
47
48    static int64_t GetNowUs();
49
50protected:
51    virtual ~ALooper();
52
53private:
54    friend struct ALooperRoster;
55
56    struct Event {
57        int64_t mWhenUs;
58        sp<AMessage> mMessage;
59    };
60
61    Mutex mLock;
62    Condition mQueueChangedCondition;
63
64    List<Event> mEventQueue;
65
66    struct LooperThread;
67    sp<LooperThread> mThread;
68    bool mRunningLocally;
69
70    void post(const sp<AMessage> &msg, int64_t delayUs);
71    bool loop();
72
73    DISALLOW_EVIL_CONSTRUCTORS(ALooper);
74};
75
76}  // namespace android
77
78#endif  // A_LOOPER_H_
79