MessageQueue.h revision be48137beb68893870c26b7f8c59eac5d0bf517a
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/List.h>
27
28
29namespace android {
30
31// ---------------------------------------------------------------------------
32
33class MessageBase;
34
35class MessageList
36{
37    List< sp<MessageBase> > mList;
38    typedef List< sp<MessageBase> > LIST;
39public:
40    typedef sp<MessageBase> value_type;
41    inline LIST::iterator begin()                { return mList.begin(); }
42    inline LIST::const_iterator begin() const    { return mList.begin(); }
43    inline LIST::iterator end()                  { return mList.end(); }
44    inline LIST::const_iterator end() const      { return mList.end(); }
45    inline bool isEmpty() const { return mList.empty(); }
46    void insert(const sp<MessageBase>& node);
47    void remove(LIST::iterator pos);
48};
49
50// ============================================================================
51
52class MessageBase :
53    public LightRefBase<MessageBase>
54{
55public:
56    nsecs_t     when;
57    uint32_t    what;
58    int32_t     arg0;
59
60    MessageBase() : when(0), what(0), arg0(0) { }
61    MessageBase(uint32_t what, int32_t arg0=0)
62        : when(0), what(what), arg0(arg0) { }
63
64    // return true if message has a handler
65    virtual bool handler() { return false; }
66
67protected:
68    virtual ~MessageBase() { }
69
70private:
71    friend class LightRefBase<MessageBase>;
72};
73
74inline bool operator < (const MessageBase& lhs, const MessageBase& rhs) {
75    return lhs.when < rhs.when;
76}
77
78// ---------------------------------------------------------------------------
79
80class MessageQueue
81{
82    typedef List< sp<MessageBase> > LIST;
83public:
84
85    // this is a work-around the multichar constant warning. A macro would
86    // work too, but would pollute the namespace.
87    template <int a, int b, int c, int d>
88    struct WHAT {
89        static const uint32_t Value =
90            (uint32_t(a&0xff)<<24)|(uint32_t(b&0xff)<<16)|
91            (uint32_t(c&0xff)<<8)|uint32_t(d&0xff);
92    };
93
94    MessageQueue();
95    ~MessageQueue();
96
97    // pre-defined messages
98    enum {
99        INVALIDATE = WHAT<'_','p','d','t'>::Value
100    };
101
102    MessageList::value_type waitMessage(nsecs_t timeout = -1);
103
104    status_t postMessage(const MessageList::value_type& message,
105            nsecs_t reltime=0, uint32_t flags = 0);
106
107    status_t invalidate();
108
109    void dump(const MessageList::value_type& message);
110
111private:
112    status_t queueMessage(const MessageList::value_type& message,
113            nsecs_t reltime, uint32_t flags);
114    void dumpLocked(const MessageList::value_type& message);
115
116    Mutex           mLock;
117    Condition       mCondition;
118    MessageList     mMessages;
119    bool            mInvalidate;
120    MessageList::value_type mInvalidateMessage;
121};
122
123// ---------------------------------------------------------------------------
124
125}; // namespace android
126
127#endif /* ANDROID_MESSAGE_QUEUE_H */
128