2 * Copyright (C) 2009 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <sys/types.h>
21 #include <utils/threads.h>
22 #include <utils/Timers.h>
23 #include <utils/Log.h>
24 #include <binder/IPCThreadState.h>
26 #include "MessageQueue.h"
30 // ---------------------------------------------------------------------------
32 void MessageList::insert(const sp<MessageBase>& node)
34 LIST::iterator cur(mList.begin());
35 LIST::iterator end(mList.end());
38 mList.insert(cur, node);
43 mList.insert(++end, node);
46 void MessageList::remove(MessageList::LIST::iterator pos)
51 // ---------------------------------------------------------------------------
53 MessageQueue::MessageQueue()
56 mInvalidateMessage = new MessageBase(INVALIDATE);
59 MessageQueue::~MessageQueue()
63 sp<MessageBase> MessageQueue::waitMessage(nsecs_t timeout)
65 sp<MessageBase> result;
69 const nsecs_t timeoutTime = systemTime() + timeout;
71 Mutex::Autolock _l(mLock);
72 nsecs_t now = systemTime();
73 nsecs_t nextEventTime = -1;
75 LIST::iterator cur(mMessages.begin());
76 if (cur != mMessages.end()) {
81 if (result->when <= now) {
82 // there is a message to deliver
83 mMessages.remove(cur);
86 nextEventTime = result->when;
90 // see if we have an invalidate message
93 mInvalidateMessage->when = now;
94 result = mInvalidateMessage;
99 if (timeoutTime < now) {
100 // we timed-out, return a NULL message
104 if (nextEventTime > 0) {
105 if (nextEventTime > timeoutTime) {
106 nextEventTime = timeoutTime;
109 nextEventTime = timeoutTime;
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();
118 const nsecs_t reltime = nextEventTime - systemTime();
120 mCondition.waitRelative(mLock, reltime);
124 //LOGD("going to wait");
125 // we're about to wait, flush the binder command buffer
126 IPCThreadState::self()->flushCommands();
127 mCondition.wait(mLock);
130 // here we're not holding the lock anymore
135 again = result->handler();
137 // the message has been processed. release our reference to it
138 // without holding the lock.
148 status_t MessageQueue::postMessage(
149 const sp<MessageBase>& message, nsecs_t relTime, uint32_t flags)
151 return queueMessage(message, relTime, flags);
154 status_t MessageQueue::invalidate() {
155 Mutex::Autolock _l(mLock);
161 status_t MessageQueue::queueMessage(
162 const sp<MessageBase>& message, nsecs_t relTime, uint32_t flags)
164 Mutex::Autolock _l(mLock);
165 message->when = systemTime() + relTime;
166 mMessages.insert(message);
168 //LOGD("MessageQueue::queueMessage time = %lld ms", message->when);
169 //dumpLocked(message);
175 void MessageQueue::dump(const sp<MessageBase>& message)
177 Mutex::Autolock _l(mLock);
181 void MessageQueue::dumpLocked(const sp<MessageBase>& message)
183 LIST::const_iterator cur(mMessages.begin());
184 LIST::const_iterator end(mMessages.end());
187 const char tick = (*cur == message) ? '>' : ' ';
188 LOGD("%c %d: msg{.what=%08x, when=%lld}",
189 tick, c, (*cur)->what, (*cur)->when);
195 // ---------------------------------------------------------------------------
197 }; // namespace android