blob: dc8138d1667d417ae49f605d4f6c8f43c74ca4dc [file] [log] [blame]
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001/*
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>
Mathias Agopianb6683b52009-04-28 03:17:50 -070026#include <utils/List.h>
Mathias Agopianf1d8e872009-04-20 19:39:12 -070027
28
29namespace android {
30
31// ---------------------------------------------------------------------------
32
Mathias Agopianb6683b52009-04-28 03:17:50 -070033class MessageBase;
34
35class MessageList
Mathias Agopianf1d8e872009-04-20 19:39:12 -070036{
Mathias Agopianb6683b52009-04-28 03:17:50 -070037 List< sp<MessageBase> > mList;
38 typedef List< sp<MessageBase> > LIST;
Mathias Agopianf1d8e872009-04-20 19:39:12 -070039public:
Mathias Agopianb6683b52009-04-28 03:17:50 -070040 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);
Mathias Agopianf1d8e872009-04-20 19:39:12 -070048};
49
50// ============================================================================
51
52class MessageBase :
Mathias Agopianb6683b52009-04-28 03:17:50 -070053 public LightRefBase<MessageBase>
Mathias Agopianf1d8e872009-04-20 19:39:12 -070054{
55public:
56 nsecs_t when;
57 uint32_t what;
58 int32_t arg0;
59
Mathias Agopian0aa758d2009-04-22 15:23:34 -070060 MessageBase() : when(0), what(0), arg0(0) { }
61 MessageBase(uint32_t what, int32_t arg0=0)
62 : when(0), what(what), arg0(arg0) { }
Mathias Agopianf1d8e872009-04-20 19:39:12 -070063
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
Mathias Agopianf1d8e872009-04-20 19:39:12 -070080class MessageQueue
81{
Mathias Agopianb6683b52009-04-28 03:17:50 -070082 typedef List< sp<MessageBase> > LIST;
Mathias Agopianf1d8e872009-04-20 19:39:12 -070083public:
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
Mathias Agopianb6683b52009-04-28 03:17:50 -0700102 MessageList::value_type waitMessage(nsecs_t timeout = -1);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700103
Mathias Agopianb6683b52009-04-28 03:17:50 -0700104 status_t postMessage(const MessageList::value_type& message,
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700105 nsecs_t reltime=0, uint32_t flags = 0);
106
107 status_t invalidate();
108
Mathias Agopianb6683b52009-04-28 03:17:50 -0700109 void dump(const MessageList::value_type& message);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700110
111private:
Mathias Agopianb6683b52009-04-28 03:17:50 -0700112 status_t queueMessage(const MessageList::value_type& message,
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700113 nsecs_t reltime, uint32_t flags);
Mathias Agopianb6683b52009-04-28 03:17:50 -0700114 void dumpLocked(const MessageList::value_type& message);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700115
116 Mutex mLock;
117 Condition mCondition;
118 MessageList mMessages;
119 bool mInvalidate;
Mathias Agopianb6683b52009-04-28 03:17:50 -0700120 MessageList::value_type mInvalidateMessage;
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700121};
122
123// ---------------------------------------------------------------------------
124
125}; // namespace android
126
127#endif /* ANDROID_MESSAGE_QUEUE_H */