blob: fb58ef914e3c3716672a6136030b78b23ee05e92 [file] [log] [blame]
codeworkx62f02ba2012-05-20 12:00:36 +02001/*
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 <binder/IPCThreadState.h>
25
26#include "MessageQueue.h"
27
28namespace android {
29
30void MessageList::insert(const sp<MessageBase>& node)
31{
32 LIST::iterator cur(mList.begin());
33 LIST::iterator end(mList.end());
34 while (cur != end) {
35 if (*node < **cur) {
36 mList.insert(cur, node);
37 return;
38 }
39 ++cur;
40 }
41 mList.insert(++end, node);
42}
43
44void MessageList::remove(MessageList::LIST::iterator pos)
45{
46 mList.erase(pos);
47}
48
49MessageQueue::MessageQueue()
50 : mInvalidate(false)
51{
52 mInvalidateMessage = new MessageBase(INVALIDATE);
53}
54
55MessageQueue::~MessageQueue()
56{
57}
58
59sp<MessageBase> MessageQueue::waitMessage(nsecs_t timeout)
60{
61 sp<MessageBase> result;
62
63 bool again;
64 do {
65 const nsecs_t timeoutTime = systemTime() + timeout;
66 while (true) {
67 Mutex::Autolock _l(mLock);
68 nsecs_t now = systemTime();
69 nsecs_t nextEventTime = -1;
70
71 LIST::iterator cur(mMessages.begin());
72 if (cur != mMessages.end()) {
73 result = *cur;
74 }
75
76 if (result != 0) {
77 if (result->when <= now) {
78 // there is a message to deliver
79 mMessages.remove(cur);
80 break;
81 }
82 nextEventTime = result->when;
83 result = 0;
84 }
85
86 // see if we have an invalidate message
87 if (mInvalidate) {
88 mInvalidate = false;
89 mInvalidateMessage->when = now;
90 result = mInvalidateMessage;
91 break;
92 }
93
94 if (timeout >= 0) {
95 if (timeoutTime < now) {
96 // we timed-out, return a NULL message
97 result = 0;
98 break;
99 }
100 if (nextEventTime > 0) {
101 if (nextEventTime > timeoutTime) {
102 nextEventTime = timeoutTime;
103 }
104 } else {
105 nextEventTime = timeoutTime;
106 }
107 }
108
109 if (nextEventTime >= 0) {
110 //LOGD("nextEventTime = %lld ms", nextEventTime);
111 if (nextEventTime > 0) {
112 // we're about to wait, flush the binder command buffer
113 IPCThreadState::self()->flushCommands();
114 const nsecs_t reltime = nextEventTime - systemTime();
115 if (reltime > 0) {
116 mCondition.waitRelative(mLock, reltime);
117 }
118 }
119 } else {
120 //LOGD("going to wait");
121 // we're about to wait, flush the binder command buffer
122 IPCThreadState::self()->flushCommands();
123 mCondition.wait(mLock);
124 }
125 }
126 // here we're not holding the lock anymore
127
128 if (result == 0)
129 break;
130
131 again = result->handler();
132 if (again) {
133 // the message has been processed. release our reference to it
134 // without holding the lock.
135 result->notify();
136 result = 0;
137 }
138 } while (again);
139
140 return result;
141}
142
143status_t MessageQueue::postMessage(
144 const sp<MessageBase>& message, nsecs_t relTime, uint32_t flags)
145{
146 return queueMessage(message, relTime, flags);
147}
148
149status_t MessageQueue::invalidate() {
150 Mutex::Autolock _l(mLock);
151 mInvalidate = true;
152 mCondition.signal();
153 return NO_ERROR;
154}
155
156status_t MessageQueue::queueMessage(
157 const sp<MessageBase>& message, nsecs_t relTime, uint32_t flags)
158{
159 Mutex::Autolock _l(mLock);
160 message->when = systemTime() + relTime;
161 mMessages.insert(message);
162
163 //LOGD("MessageQueue::queueMessage time = %lld ms", message->when);
164 //dumpLocked(message);
165
166 mCondition.signal();
167 return NO_ERROR;
168}
169
170void MessageQueue::dump(const sp<MessageBase>& message)
171{
172 Mutex::Autolock _l(mLock);
173 dumpLocked(message);
174}
175
176void MessageQueue::dumpLocked(const sp<MessageBase>& message)
177{
178 LIST::const_iterator cur(mMessages.begin());
179 LIST::const_iterator end(mMessages.end());
180 int c = 0;
181 while (cur != end) {
182 const char tick = (*cur == message) ? '>' : ' ';
183 LOGD("%c %d: msg{.what=%08x, when=%lld}",
184 tick, c, (*cur)->what, (*cur)->when);
185 ++cur;
186 c++;
187 }
188}
189
190}; // namespace android