MessageQueue.h revision f61c57fe2e955e1c195bb0ca2dd7bcdaa922d5a9
1/*
2 * Copyright (C) 2009 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 ANDROID_MESSAGE_QUEUE_H
18#define ANDROID_MESSAGE_QUEUE_H
19
20#include <stdint.h>
21#include <errno.h>
22#include <sys/types.h>
23
24#include <utils/threads.h>
25#include <utils/Timers.h>
26#include <utils/Looper.h>
27
28#include "Barrier.h"
29
30namespace android {
31
32// ---------------------------------------------------------------------------
33
34class MessageBase : public MessageHandler
35{
36public:
37    MessageBase();
38
39    // return true if message has a handler
40    virtual bool handler() = 0;
41
42    // waits for the handler to be processed
43    void wait() const { barrier.wait(); }
44
45protected:
46    virtual ~MessageBase();
47
48private:
49    virtual void handleMessage(const Message& message);
50
51    mutable Barrier barrier;
52};
53
54// ---------------------------------------------------------------------------
55
56class MessageQueue {
57    sp<Looper> mLooper;
58    volatile int32_t mInvalidatePending;
59
60public:
61    MessageQueue();
62    ~MessageQueue();
63
64    void waitMessage();
65    status_t postMessage(const sp<MessageBase>& message, nsecs_t reltime=0);
66    status_t invalidate();
67};
68
69// ---------------------------------------------------------------------------
70
71}; // namespace android
72
73#endif /* ANDROID_MESSAGE_QUEUE_H */
74