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