ALooper.h revision 348a8eab84f4bba76c04ca83b2f5418467aa1a48
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            int32_t priority = PRIORITY_DEFAULT
46            );
47
48    status_t stop();
49
50    static int64_t GetNowUs();
51
52protected:
53    virtual ~ALooper();
54
55private:
56    friend struct ALooperRoster;
57
58    struct Event {
59        int64_t mWhenUs;
60        sp<AMessage> mMessage;
61    };
62
63    Mutex mLock;
64    Condition mQueueChangedCondition;
65
66    List<Event> mEventQueue;
67
68    struct LooperThread;
69    sp<LooperThread> mThread;
70    bool mRunningLocally;
71
72    void post(const sp<AMessage> &msg, int64_t delayUs);
73    bool loop();
74
75    DISALLOW_EVIL_CONSTRUCTORS(ALooper);
76};
77
78}  // namespace android
79
80#endif  // A_LOOPER_H_
81