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