Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 1 | /* |
| 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 Agopian | 310f8da | 2009-05-22 01:27:01 -0700 | [diff] [blame] | 24 | #include <binder/IPCThreadState.h> |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 25 | |
| 26 | #include "MessageQueue.h" |
| 27 | |
| 28 | namespace android { |
| 29 | |
| 30 | // --------------------------------------------------------------------------- |
| 31 | |
Mathias Agopian | b6683b5 | 2009-04-28 03:17:50 -0700 | [diff] [blame] | 32 | void 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 | |
| 46 | void MessageList::remove(MessageList::LIST::iterator pos) |
| 47 | { |
| 48 | mList.erase(pos); |
| 49 | } |
| 50 | |
| 51 | // --------------------------------------------------------------------------- |
| 52 | |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 53 | MessageQueue::MessageQueue() |
Mathias Agopian | 84f6814 | 2009-07-09 17:30:43 -0700 | [diff] [blame^] | 54 | : mInvalidate(false) |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 55 | { |
| 56 | mInvalidateMessage = new MessageBase(INVALIDATE); |
| 57 | } |
| 58 | |
| 59 | MessageQueue::~MessageQueue() |
| 60 | { |
| 61 | } |
| 62 | |
Mathias Agopian | b6683b5 | 2009-04-28 03:17:50 -0700 | [diff] [blame] | 63 | MessageList::value_type MessageQueue::waitMessage(nsecs_t timeout) |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 64 | { |
Mathias Agopian | b6683b5 | 2009-04-28 03:17:50 -0700 | [diff] [blame] | 65 | MessageList::value_type result; |
| 66 | |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 67 | 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 Agopian | b6683b5 | 2009-04-28 03:17:50 -0700 | [diff] [blame] | 83 | LIST::iterator cur(mMessages.begin()); |
| 84 | if (cur != mMessages.end()) { |
| 85 | result = *cur; |
| 86 | } |
| 87 | |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 88 | if (result != 0) { |
| 89 | if (result->when <= now) { |
| 90 | // there is a message to deliver |
Mathias Agopian | b6683b5 | 2009-04-28 03:17:50 -0700 | [diff] [blame] | 91 | mMessages.remove(cur); |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 92 | 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(); |
| 114 | mCondition.wait(mLock, nextEventTime); |
| 115 | } |
| 116 | } else { |
| 117 | //LOGD("going to wait"); |
| 118 | // we're about to wait, flush the binder command buffer |
| 119 | IPCThreadState::self()->flushCommands(); |
| 120 | mCondition.wait(mLock); |
| 121 | } |
| 122 | } |
| 123 | // here we're not holding the lock anymore |
| 124 | |
| 125 | if (result == 0) |
| 126 | break; |
| 127 | |
| 128 | again = result->handler(); |
| 129 | if (again) { |
| 130 | // the message has been processed. release our reference to it |
| 131 | // without holding the lock. |
| 132 | result = 0; |
| 133 | } |
| 134 | |
| 135 | } while (again); |
Mathias Agopian | b6683b5 | 2009-04-28 03:17:50 -0700 | [diff] [blame] | 136 | |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 137 | return result; |
| 138 | } |
| 139 | |
| 140 | status_t MessageQueue::postMessage( |
Mathias Agopian | b6683b5 | 2009-04-28 03:17:50 -0700 | [diff] [blame] | 141 | const MessageList::value_type& message, nsecs_t relTime, uint32_t flags) |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 142 | { |
| 143 | return queueMessage(message, relTime, flags); |
| 144 | } |
| 145 | |
| 146 | status_t MessageQueue::invalidate() { |
| 147 | Mutex::Autolock _l(mLock); |
| 148 | mInvalidate = true; |
| 149 | mCondition.signal(); |
| 150 | return NO_ERROR; |
| 151 | } |
| 152 | |
| 153 | status_t MessageQueue::queueMessage( |
Mathias Agopian | b6683b5 | 2009-04-28 03:17:50 -0700 | [diff] [blame] | 154 | const MessageList::value_type& message, nsecs_t relTime, uint32_t flags) |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 155 | { |
| 156 | Mutex::Autolock _l(mLock); |
| 157 | message->when = systemTime() + relTime; |
| 158 | mMessages.insert(message); |
| 159 | |
| 160 | //LOGD("MessageQueue::queueMessage time = %lld ms", message->when); |
| 161 | //dumpLocked(message); |
| 162 | |
| 163 | mCondition.signal(); |
| 164 | return NO_ERROR; |
| 165 | } |
| 166 | |
Mathias Agopian | b6683b5 | 2009-04-28 03:17:50 -0700 | [diff] [blame] | 167 | void MessageQueue::dump(const MessageList::value_type& message) |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 168 | { |
| 169 | Mutex::Autolock _l(mLock); |
| 170 | dumpLocked(message); |
| 171 | } |
| 172 | |
Mathias Agopian | b6683b5 | 2009-04-28 03:17:50 -0700 | [diff] [blame] | 173 | void MessageQueue::dumpLocked(const MessageList::value_type& message) |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 174 | { |
Mathias Agopian | b6683b5 | 2009-04-28 03:17:50 -0700 | [diff] [blame] | 175 | LIST::const_iterator cur(mMessages.begin()); |
| 176 | LIST::const_iterator end(mMessages.end()); |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 177 | int c = 0; |
Mathias Agopian | b6683b5 | 2009-04-28 03:17:50 -0700 | [diff] [blame] | 178 | while (cur != end) { |
| 179 | const char tick = (*cur == message) ? '>' : ' '; |
| 180 | LOGD("%c %d: msg{.what=%08x, when=%lld}", |
| 181 | tick, c, (*cur)->what, (*cur)->when); |
| 182 | ++cur; |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 183 | c++; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | // --------------------------------------------------------------------------- |
| 188 | |
| 189 | }; // namespace android |