blob: aebe1b82d2b2be08ea5ce9083323d2d8bee4809f [file] [log] [blame]
Mathias Agopian6ead5d92009-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>
Mathias Agopian947f4f42009-05-22 01:27:01 -070024#include <binder/IPCThreadState.h>
Mathias Agopian6ead5d92009-04-20 19:39:12 -070025
26#include "MessageQueue.h"
27
28namespace android {
29
30// ---------------------------------------------------------------------------
31
Mathias Agopianbdbe6022009-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 Agopian6ead5d92009-04-20 19:39:12 -070053MessageQueue::MessageQueue()
Mathias Agopian05dab6f2009-07-09 17:30:43 -070054 : mInvalidate(false)
Mathias Agopian6ead5d92009-04-20 19:39:12 -070055{
56 mInvalidateMessage = new MessageBase(INVALIDATE);
57}
58
59MessageQueue::~MessageQueue()
60{
61}
62
Mathias Agopian898c4c92010-05-18 17:06:55 -070063sp<MessageBase> MessageQueue::waitMessage(nsecs_t timeout)
Mathias Agopian6ead5d92009-04-20 19:39:12 -070064{
Mathias Agopian898c4c92010-05-18 17:06:55 -070065 sp<MessageBase> result;
Mathias Agopianbdbe6022009-04-28 03:17:50 -070066
Mathias Agopian6ead5d92009-04-20 19:39:12 -070067 bool again;
68 do {
69 const nsecs_t timeoutTime = systemTime() + timeout;
70 while (true) {
71 Mutex::Autolock _l(mLock);
72 nsecs_t now = systemTime();
73 nsecs_t nextEventTime = -1;
74
Mathias Agopianbdbe6022009-04-28 03:17:50 -070075 LIST::iterator cur(mMessages.begin());
76 if (cur != mMessages.end()) {
77 result = *cur;
78 }
79
Mathias Agopian6ead5d92009-04-20 19:39:12 -070080 if (result != 0) {
81 if (result->when <= now) {
82 // there is a message to deliver
Mathias Agopianbdbe6022009-04-28 03:17:50 -070083 mMessages.remove(cur);
Mathias Agopian6ead5d92009-04-20 19:39:12 -070084 break;
85 }
Mathias Agopian6ead5d92009-04-20 19:39:12 -070086 nextEventTime = result->when;
87 result = 0;
88 }
89
Mathias Agopian7b50a6d2010-10-11 14:11:15 -070090 // see if we have an invalidate message
91 if (mInvalidate) {
92 mInvalidate = false;
93 mInvalidateMessage->when = now;
94 result = mInvalidateMessage;
95 break;
96 }
97
Mathias Agopianb921d302010-10-07 19:59:06 -070098 if (timeout >= 0) {
99 if (timeoutTime < now) {
100 // we timed-out, return a NULL message
101 result = 0;
102 break;
103 }
104 if (nextEventTime > 0) {
105 if (nextEventTime > timeoutTime) {
106 nextEventTime = timeoutTime;
107 }
108 } else {
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700109 nextEventTime = timeoutTime;
110 }
111 }
112
113 if (nextEventTime >= 0) {
114 //LOGD("nextEventTime = %lld ms", nextEventTime);
115 if (nextEventTime > 0) {
116 // we're about to wait, flush the binder command buffer
117 IPCThreadState::self()->flushCommands();
Mathias Agopianb1c4ca52009-07-12 23:11:20 -0700118 const nsecs_t reltime = nextEventTime - systemTime();
119 if (reltime > 0) {
120 mCondition.waitRelative(mLock, reltime);
121 }
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700122 }
123 } else {
124 //LOGD("going to wait");
125 // we're about to wait, flush the binder command buffer
126 IPCThreadState::self()->flushCommands();
127 mCondition.wait(mLock);
128 }
129 }
130 // here we're not holding the lock anymore
131
132 if (result == 0)
133 break;
134
135 again = result->handler();
136 if (again) {
137 // the message has been processed. release our reference to it
138 // without holding the lock.
Mathias Agopian898c4c92010-05-18 17:06:55 -0700139 result->notify();
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700140 result = 0;
141 }
142
143 } while (again);
Mathias Agopianbdbe6022009-04-28 03:17:50 -0700144
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700145 return result;
146}
147
148status_t MessageQueue::postMessage(
Mathias Agopian898c4c92010-05-18 17:06:55 -0700149 const sp<MessageBase>& message, nsecs_t relTime, uint32_t flags)
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700150{
151 return queueMessage(message, relTime, flags);
152}
153
154status_t MessageQueue::invalidate() {
155 Mutex::Autolock _l(mLock);
156 mInvalidate = true;
157 mCondition.signal();
158 return NO_ERROR;
159}
160
161status_t MessageQueue::queueMessage(
Mathias Agopian898c4c92010-05-18 17:06:55 -0700162 const sp<MessageBase>& message, nsecs_t relTime, uint32_t flags)
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700163{
164 Mutex::Autolock _l(mLock);
165 message->when = systemTime() + relTime;
166 mMessages.insert(message);
167
168 //LOGD("MessageQueue::queueMessage time = %lld ms", message->when);
169 //dumpLocked(message);
170
171 mCondition.signal();
172 return NO_ERROR;
173}
174
Mathias Agopian898c4c92010-05-18 17:06:55 -0700175void MessageQueue::dump(const sp<MessageBase>& message)
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700176{
177 Mutex::Autolock _l(mLock);
178 dumpLocked(message);
179}
180
Mathias Agopian898c4c92010-05-18 17:06:55 -0700181void MessageQueue::dumpLocked(const sp<MessageBase>& message)
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700182{
Mathias Agopianbdbe6022009-04-28 03:17:50 -0700183 LIST::const_iterator cur(mMessages.begin());
184 LIST::const_iterator end(mMessages.end());
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700185 int c = 0;
Mathias Agopianbdbe6022009-04-28 03:17:50 -0700186 while (cur != end) {
187 const char tick = (*cur == message) ? '>' : ' ';
188 LOGD("%c %d: msg{.what=%08x, when=%lld}",
189 tick, c, (*cur)->what, (*cur)->when);
190 ++cur;
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700191 c++;
192 }
193}
194
195// ---------------------------------------------------------------------------
196
197}; // namespace android