ALooper.h revision 5804a76ac5f9f3c311f1bbbcc5ebdc8f8568ae14
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
56    const char *getName() const {
57        return mName.c_str();
58    }
59
60protected:
61    virtual ~ALooper();
62
63private:
64    friend struct AMessage;       // post()
65
66    struct Event {
67        int64_t mWhenUs;
68        sp<AMessage> mMessage;
69    };
70
71    Mutex mLock;
72    Condition mQueueChangedCondition;
73
74    AString mName;
75
76    List<Event> mEventQueue;
77
78    struct LooperThread;
79    sp<LooperThread> mThread;
80    bool mRunningLocally;
81
82    void post(const sp<AMessage> &msg, int64_t delayUs);
83    bool loop();
84
85    DISALLOW_EVIL_CONSTRUCTORS(ALooper);
86};
87
88} // namespace android
89
90#endif  // A_LOOPER_H_
91