blob: b43d80173a3fe7c47581c92b7018dc415a5b1886 [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>
Mathias Agopian310f8da2009-05-22 01:27:01 -070024#include <binder/IPCThreadState.h>
Mathias Agopianf1d8e872009-04-20 19:39:12 -070025
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()
Mathias Agopian84f68142009-07-09 17:30:43 -070054 : mInvalidate(false)
Mathias Agopianf1d8e872009-04-20 19:39:12 -070055{
56 mInvalidateMessage = new MessageBase(INVALIDATE);
57}
58
59MessageQueue::~MessageQueue()
60{
61}
62
Mathias Agopianb6683b52009-04-28 03:17:50 -070063MessageList::value_type MessageQueue::waitMessage(nsecs_t timeout)
Mathias Agopianf1d8e872009-04-20 19:39:12 -070064{
Mathias Agopianb6683b52009-04-28 03:17:50 -070065 MessageList::value_type result;
66
Mathias Agopianf1d8e872009-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
75 // invalidate messages are always handled first
76 if (mInvalidate) {
77 mInvalidate = false;
78 mInvalidateMessage->when = now;
79 result = mInvalidateMessage;
80 break;
81 }
82
Mathias Agopianb6683b52009-04-28 03:17:50 -070083 LIST::iterator cur(mMessages.begin());
84 if (cur != mMessages.end()) {
85 result = *cur;
86 }
87
Mathias Agopianf1d8e872009-04-20 19:39:12 -070088 if (result != 0) {
89 if (result->when <= now) {
90 // there is a message to deliver
Mathias Agopianb6683b52009-04-28 03:17:50 -070091 mMessages.remove(cur);
Mathias Agopianf1d8e872009-04-20 19:39:12 -070092 break;
93 }
94 if (timeout>=0 && timeoutTime < now) {
95 // we timed-out, return a NULL message
96 result = 0;
97 break;
98 }
99 nextEventTime = result->when;
100 result = 0;
101 }
102
103 if (timeout >= 0 && nextEventTime > 0) {
104 if (nextEventTime > timeoutTime) {
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();
Mathias Agopianec0f1f62009-07-12 23:11:20 -0700114 const nsecs_t reltime = nextEventTime - systemTime();
115 if (reltime > 0) {
116 mCondition.waitRelative(mLock, reltime);
117 }
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700118 }
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 = 0;
136 }
137
138 } while (again);
Mathias Agopianb6683b52009-04-28 03:17:50 -0700139
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700140 return result;
141}
142
143status_t MessageQueue::postMessage(
Mathias Agopianb6683b52009-04-28 03:17:50 -0700144 const MessageList::value_type& message, nsecs_t relTime, uint32_t flags)
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700145{
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(
Mathias Agopianb6683b52009-04-28 03:17:50 -0700157 const MessageList::value_type& message, nsecs_t relTime, uint32_t flags)
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700158{
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
Mathias Agopianb6683b52009-04-28 03:17:50 -0700170void MessageQueue::dump(const MessageList::value_type& message)
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700171{
172 Mutex::Autolock _l(mLock);
173 dumpLocked(message);
174}
175
Mathias Agopianb6683b52009-04-28 03:17:50 -0700176void MessageQueue::dumpLocked(const MessageList::value_type& message)
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700177{
Mathias Agopianb6683b52009-04-28 03:17:50 -0700178 LIST::const_iterator cur(mMessages.begin());
179 LIST::const_iterator end(mMessages.end());
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700180 int c = 0;
Mathias Agopianb6683b52009-04-28 03:17:50 -0700181 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;
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700186 c++;
187 }
188}
189
190// ---------------------------------------------------------------------------
191
192}; // namespace android