blob: 890f809a3b574ed60849f480a496d6b2cc57f3d7 [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
Mathias Agopianbb641242010-05-18 17:06:55 -070028#include "Barrier.h"
Mathias Agopianf1d8e872009-04-20 19:39:12 -070029
30namespace android {
31
32// ---------------------------------------------------------------------------
33
Mathias Agopianb6683b52009-04-28 03:17:50 -070034class MessageBase;
35
36class MessageList
Mathias Agopianf1d8e872009-04-20 19:39:12 -070037{
Mathias Agopianb6683b52009-04-28 03:17:50 -070038 List< sp<MessageBase> > mList;
39 typedef List< sp<MessageBase> > LIST;
Mathias Agopianf1d8e872009-04-20 19:39:12 -070040public:
Mathias Agopianb6683b52009-04-28 03:17:50 -070041 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; }
Mathias Agopianbb641242010-05-18 17:06:55 -070066
67 // waits for the handler to be processed
68 void wait() const { barrier.wait(); }
Mathias Agopianf1d8e872009-04-20 19:39:12 -070069
Mathias Agopianbb641242010-05-18 17:06:55 -070070 // releases all waiters. this is done automatically if
71 // handler returns true
72 void notify() const { barrier.open(); }
73
Mathias Agopianf1d8e872009-04-20 19:39:12 -070074protected:
75 virtual ~MessageBase() { }
76
77private:
Mathias Agopianbb641242010-05-18 17:06:55 -070078 mutable Barrier barrier;
Mathias Agopianf1d8e872009-04-20 19:39:12 -070079 friend class LightRefBase<MessageBase>;
80};
81
82inline bool operator < (const MessageBase& lhs, const MessageBase& rhs) {
83 return lhs.when < rhs.when;
84}
85
86// ---------------------------------------------------------------------------
87
Mathias Agopianf1d8e872009-04-20 19:39:12 -070088class MessageQueue
89{
Mathias Agopianb6683b52009-04-28 03:17:50 -070090 typedef List< sp<MessageBase> > LIST;
Mathias Agopianf1d8e872009-04-20 19:39:12 -070091public:
92
Mathias Agopianf1d8e872009-04-20 19:39:12 -070093 MessageQueue();
94 ~MessageQueue();
95
96 // pre-defined messages
97 enum {
Mathias Agopianbb641242010-05-18 17:06:55 -070098 INVALIDATE = '_upd'
Mathias Agopianf1d8e872009-04-20 19:39:12 -070099 };
100
Mathias Agopianbb641242010-05-18 17:06:55 -0700101 sp<MessageBase> waitMessage(nsecs_t timeout = -1);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700102
Mathias Agopianbb641242010-05-18 17:06:55 -0700103 status_t postMessage(const sp<MessageBase>& message,
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700104 nsecs_t reltime=0, uint32_t flags = 0);
Mathias Agopianbb641242010-05-18 17:06:55 -0700105
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700106 status_t invalidate();
107
Mathias Agopianbb641242010-05-18 17:06:55 -0700108 void dump(const sp<MessageBase>& message);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700109
110private:
Mathias Agopianbb641242010-05-18 17:06:55 -0700111 status_t queueMessage(const sp<MessageBase>& message,
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700112 nsecs_t reltime, uint32_t flags);
Mathias Agopianbb641242010-05-18 17:06:55 -0700113 void dumpLocked(const sp<MessageBase>& message);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700114
115 Mutex mLock;
116 Condition mCondition;
117 MessageList mMessages;
118 bool mInvalidate;
Mathias Agopianbb641242010-05-18 17:06:55 -0700119 sp<MessageBase> mInvalidateMessage;
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700120};
121
122// ---------------------------------------------------------------------------
123
124}; // namespace android
125
126#endif /* ANDROID_MESSAGE_QUEUE_H */