blob: 907938300ff13081025f84ca3303e33311e5ab99 [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
32MessageQueue::MessageQueue()
33{
34 mInvalidateMessage = new MessageBase(INVALIDATE);
35}
36
37MessageQueue::~MessageQueue()
38{
39}
40
41MessageList::NODE_PTR MessageQueue::waitMessage(nsecs_t timeout)
42{
43 MessageList::NODE_PTR result;
44 bool again;
45 do {
46 const nsecs_t timeoutTime = systemTime() + timeout;
47 while (true) {
48 Mutex::Autolock _l(mLock);
49 nsecs_t now = systemTime();
50 nsecs_t nextEventTime = -1;
51
52 // invalidate messages are always handled first
53 if (mInvalidate) {
54 mInvalidate = false;
55 mInvalidateMessage->when = now;
56 result = mInvalidateMessage;
57 break;
58 }
59
60 result = mMessages.head();
61
62 if (result != 0) {
63 if (result->when <= now) {
64 // there is a message to deliver
65 mMessages.remove(result);
66 result->detach();
67 break;
68 }
69 if (timeout>=0 && timeoutTime < now) {
70 // we timed-out, return a NULL message
71 result = 0;
72 break;
73 }
74 nextEventTime = result->when;
75 result = 0;
76 }
77
78 if (timeout >= 0 && nextEventTime > 0) {
79 if (nextEventTime > timeoutTime) {
80 nextEventTime = timeoutTime;
81 }
82 }
83
84 if (nextEventTime >= 0) {
85 //LOGD("nextEventTime = %lld ms", nextEventTime);
86 if (nextEventTime > 0) {
87 // we're about to wait, flush the binder command buffer
88 IPCThreadState::self()->flushCommands();
89 mCondition.wait(mLock, nextEventTime);
90 }
91 } else {
92 //LOGD("going to wait");
93 // we're about to wait, flush the binder command buffer
94 IPCThreadState::self()->flushCommands();
95 mCondition.wait(mLock);
96 }
97 }
98 // here we're not holding the lock anymore
99
100 if (result == 0)
101 break;
102
103 again = result->handler();
104 if (again) {
105 // the message has been processed. release our reference to it
106 // without holding the lock.
107 result = 0;
108 }
109
110 } while (again);
111 return result;
112}
113
114status_t MessageQueue::postMessage(
115 const MessageList::NODE_PTR& message, nsecs_t relTime, uint32_t flags)
116{
117 return queueMessage(message, relTime, flags);
118}
119
120status_t MessageQueue::invalidate() {
121 Mutex::Autolock _l(mLock);
122 mInvalidate = true;
123 mCondition.signal();
124 return NO_ERROR;
125}
126
127status_t MessageQueue::queueMessage(
128 const MessageList::NODE_PTR& message, nsecs_t relTime, uint32_t flags)
129{
130 Mutex::Autolock _l(mLock);
131 message->when = systemTime() + relTime;
132 mMessages.insert(message);
133
134 //LOGD("MessageQueue::queueMessage time = %lld ms", message->when);
135 //dumpLocked(message);
136
137 mCondition.signal();
138 return NO_ERROR;
139}
140
141void MessageQueue::dump(const MessageList::NODE_PTR& message)
142{
143 Mutex::Autolock _l(mLock);
144 dumpLocked(message);
145}
146
147void MessageQueue::dumpLocked(const MessageList::NODE_PTR& message)
148{
149 MessageList::NODE_PTR l(mMessages.head());
150 int c = 0;
151 while (l != 0) {
152 const char tick = (l == message) ? '>' : ' ';
153 LOGD("%c %d: msg{.what=%08x, when=%lld}", tick, c, l->what, l->when);
154 l = l->getNext();
155 c++;
156 }
157}
158
159// ---------------------------------------------------------------------------
160
161}; // namespace android