blob: 52d4db80638888eea601b0f255654829dea604b8 [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#include <stdint.h>
18#include <errno.h>
19#include <sys/types.h>
20
21#include <utils/threads.h>
22#include <utils/Timers.h>
23#include <utils/Log.h>
24#include <utils/IPCThreadState.h>
25
26#include "MessageQueue.h"
27
28namespace android {
29
30// ---------------------------------------------------------------------------
31
Mathias Agopianb6683b52009-04-28 03:17:50 -070032void MessageList::insert(const sp<MessageBase>& node)
33{
34 LIST::iterator cur(mList.begin());
35 LIST::iterator end(mList.end());
36 while (cur != end) {
37 if (*node < **cur) {
38 mList.insert(cur, node);
39 return;
40 }
41 ++cur;
42 }
43 mList.insert(++end, node);
44}
45
46void MessageList::remove(MessageList::LIST::iterator pos)
47{
48 mList.erase(pos);
49}
50
51// ---------------------------------------------------------------------------
52
Mathias Agopianf1d8e872009-04-20 19:39:12 -070053MessageQueue::MessageQueue()
54{
55 mInvalidateMessage = new MessageBase(INVALIDATE);
56}
57
58MessageQueue::~MessageQueue()
59{
60}
61
Mathias Agopianb6683b52009-04-28 03:17:50 -070062MessageList::value_type MessageQueue::waitMessage(nsecs_t timeout)
Mathias Agopianf1d8e872009-04-20 19:39:12 -070063{
Mathias Agopianb6683b52009-04-28 03:17:50 -070064 MessageList::value_type result;
65
Mathias Agopianf1d8e872009-04-20 19:39:12 -070066 bool again;
67 do {
68 const nsecs_t timeoutTime = systemTime() + timeout;
69 while (true) {
70 Mutex::Autolock _l(mLock);
71 nsecs_t now = systemTime();
72 nsecs_t nextEventTime = -1;
73
74 // invalidate messages are always handled first
75 if (mInvalidate) {
76 mInvalidate = false;
77 mInvalidateMessage->when = now;
78 result = mInvalidateMessage;
79 break;
80 }
81
Mathias Agopianb6683b52009-04-28 03:17:50 -070082 LIST::iterator cur(mMessages.begin());
83 if (cur != mMessages.end()) {
84 result = *cur;
85 }
86
Mathias Agopianf1d8e872009-04-20 19:39:12 -070087 if (result != 0) {
88 if (result->when <= now) {
89 // there is a message to deliver
Mathias Agopianb6683b52009-04-28 03:17:50 -070090 mMessages.remove(cur);
Mathias Agopianf1d8e872009-04-20 19:39:12 -070091 break;
92 }
93 if (timeout>=0 && timeoutTime < now) {
94 // we timed-out, return a NULL message
95 result = 0;
96 break;
97 }
98 nextEventTime = result->when;
99 result = 0;
100 }
101
102 if (timeout >= 0 && nextEventTime > 0) {
103 if (nextEventTime > timeoutTime) {
104 nextEventTime = timeoutTime;
105 }
106 }
107
108 if (nextEventTime >= 0) {
109 //LOGD("nextEventTime = %lld ms", nextEventTime);
110 if (nextEventTime > 0) {
111 // we're about to wait, flush the binder command buffer
112 IPCThreadState::self()->flushCommands();
113 mCondition.wait(mLock, nextEventTime);
114 }
115 } else {
116 //LOGD("going to wait");
117 // we're about to wait, flush the binder command buffer
118 IPCThreadState::self()->flushCommands();
119 mCondition.wait(mLock);
120 }
121 }
122 // here we're not holding the lock anymore
123
124 if (result == 0)
125 break;
126
127 again = result->handler();
128 if (again) {
129 // the message has been processed. release our reference to it
130 // without holding the lock.
131 result = 0;
132 }
133
134 } while (again);
Mathias Agopianb6683b52009-04-28 03:17:50 -0700135
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700136 return result;
137}
138
139status_t MessageQueue::postMessage(
Mathias Agopianb6683b52009-04-28 03:17:50 -0700140 const MessageList::value_type& message, nsecs_t relTime, uint32_t flags)
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700141{
142 return queueMessage(message, relTime, flags);
143}
144
145status_t MessageQueue::invalidate() {
146 Mutex::Autolock _l(mLock);
147 mInvalidate = true;
148 mCondition.signal();
149 return NO_ERROR;
150}
151
152status_t MessageQueue::queueMessage(
Mathias Agopianb6683b52009-04-28 03:17:50 -0700153 const MessageList::value_type& message, nsecs_t relTime, uint32_t flags)
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700154{
155 Mutex::Autolock _l(mLock);
156 message->when = systemTime() + relTime;
157 mMessages.insert(message);
158
159 //LOGD("MessageQueue::queueMessage time = %lld ms", message->when);
160 //dumpLocked(message);
161
162 mCondition.signal();
163 return NO_ERROR;
164}
165
Mathias Agopianb6683b52009-04-28 03:17:50 -0700166void MessageQueue::dump(const MessageList::value_type& message)
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700167{
168 Mutex::Autolock _l(mLock);
169 dumpLocked(message);
170}
171
Mathias Agopianb6683b52009-04-28 03:17:50 -0700172void MessageQueue::dumpLocked(const MessageList::value_type& message)
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700173{
Mathias Agopianb6683b52009-04-28 03:17:50 -0700174 LIST::const_iterator cur(mMessages.begin());
175 LIST::const_iterator end(mMessages.end());
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700176 int c = 0;
Mathias Agopianb6683b52009-04-28 03:17:50 -0700177 while (cur != end) {
178 const char tick = (*cur == message) ? '>' : ' ';
179 LOGD("%c %d: msg{.what=%08x, when=%lld}",
180 tick, c, (*cur)->what, (*cur)->when);
181 ++cur;
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700182 c++;
183 }
184}
185
186// ---------------------------------------------------------------------------
187
188}; // namespace android