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 | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame^] | 32 | MessageBase::MessageBase() |
| 33 | : MessageHandler() { |
Mathias Agopian | b6683b5 | 2009-04-28 03:17:50 -0700 | [diff] [blame] | 34 | } |
| 35 | |
Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame^] | 36 | MessageBase::~MessageBase() { |
Mathias Agopian | b6683b5 | 2009-04-28 03:17:50 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame^] | 39 | void MessageBase::handleMessage(const Message&) { |
| 40 | this->handler(); |
| 41 | barrier.open(); |
| 42 | }; |
| 43 | |
Mathias Agopian | b6683b5 | 2009-04-28 03:17:50 -0700 | [diff] [blame] | 44 | // --------------------------------------------------------------------------- |
| 45 | |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 46 | MessageQueue::MessageQueue() |
Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame^] | 47 | : mLooper(new Looper(true)), |
| 48 | mInvalidatePending(0) |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 49 | { |
| 50 | } |
| 51 | |
Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame^] | 52 | MessageQueue::~MessageQueue() { |
| 53 | } |
Mathias Agopian | b6683b5 | 2009-04-28 03:17:50 -0700 | [diff] [blame] | 54 | |
Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame^] | 55 | void MessageQueue::waitMessage() { |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 56 | do { |
Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame^] | 57 | // handle invalidate events first |
| 58 | if (android_atomic_and(0, &mInvalidatePending) != 0) |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 59 | break; |
| 60 | |
Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame^] | 61 | IPCThreadState::self()->flushCommands(); |
Mathias Agopian | b6683b5 | 2009-04-28 03:17:50 -0700 | [diff] [blame] | 62 | |
Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame^] | 63 | int32_t ret = mLooper->pollOnce(-1); |
| 64 | switch (ret) { |
| 65 | case ALOOPER_POLL_WAKE: |
| 66 | // we got woken-up there is work to do in the main loop |
| 67 | continue; |
| 68 | |
| 69 | case ALOOPER_POLL_CALLBACK: |
| 70 | // callback was handled, loop again |
| 71 | continue; |
| 72 | |
| 73 | case ALOOPER_POLL_TIMEOUT: |
| 74 | // timeout (should not happen) |
| 75 | continue; |
| 76 | |
| 77 | case ALOOPER_POLL_ERROR: |
| 78 | LOGE("ALOOPER_POLL_ERROR"); |
| 79 | continue; |
| 80 | |
| 81 | default: |
| 82 | // should not happen |
| 83 | LOGE("Looper::pollOnce() returned unknown status %d", ret); |
| 84 | continue; |
| 85 | } |
| 86 | } while (true); |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | status_t MessageQueue::postMessage( |
Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame^] | 90 | const sp<MessageBase>& messageHandler, nsecs_t relTime) |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 91 | { |
Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame^] | 92 | const Message dummyMessage; |
| 93 | if (relTime > 0) { |
| 94 | mLooper->sendMessageDelayed(relTime, messageHandler, dummyMessage); |
| 95 | } else { |
| 96 | mLooper->sendMessage(messageHandler, dummyMessage); |
| 97 | } |
| 98 | return NO_ERROR; |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | status_t MessageQueue::invalidate() { |
Mathias Agopian | f61c57f | 2011-11-23 16:49:10 -0800 | [diff] [blame^] | 102 | android_atomic_or(1, &mInvalidatePending); |
| 103 | mLooper->wake(); |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 104 | return NO_ERROR; |
| 105 | } |
| 106 | |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 107 | // --------------------------------------------------------------------------- |
| 108 | |
| 109 | }; // namespace android |