blob: 1846ccb31604ffb6df6d01721e1d696800fbd7c9 [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 Agopianf61c57f2011-11-23 16:49:10 -080032MessageBase::MessageBase()
33 : MessageHandler() {
Mathias Agopianb6683b52009-04-28 03:17:50 -070034}
35
Mathias Agopianf61c57f2011-11-23 16:49:10 -080036MessageBase::~MessageBase() {
Mathias Agopianb6683b52009-04-28 03:17:50 -070037}
38
Mathias Agopianf61c57f2011-11-23 16:49:10 -080039void MessageBase::handleMessage(const Message&) {
40 this->handler();
41 barrier.open();
42};
43
Mathias Agopianb6683b52009-04-28 03:17:50 -070044// ---------------------------------------------------------------------------
45
Mathias Agopianf1d8e872009-04-20 19:39:12 -070046MessageQueue::MessageQueue()
Mathias Agopianf61c57f2011-11-23 16:49:10 -080047 : mLooper(new Looper(true)),
48 mInvalidatePending(0)
Mathias Agopianf1d8e872009-04-20 19:39:12 -070049{
50}
51
Mathias Agopianf61c57f2011-11-23 16:49:10 -080052MessageQueue::~MessageQueue() {
53}
Mathias Agopianb6683b52009-04-28 03:17:50 -070054
Mathias Agopianf61c57f2011-11-23 16:49:10 -080055void MessageQueue::waitMessage() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -070056 do {
Mathias Agopianf61c57f2011-11-23 16:49:10 -080057 // handle invalidate events first
58 if (android_atomic_and(0, &mInvalidatePending) != 0)
Mathias Agopianf1d8e872009-04-20 19:39:12 -070059 break;
60
Mathias Agopianf61c57f2011-11-23 16:49:10 -080061 IPCThreadState::self()->flushCommands();
Mathias Agopianb6683b52009-04-28 03:17:50 -070062
Mathias Agopianf61c57f2011-11-23 16:49:10 -080063 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 Agopianf1d8e872009-04-20 19:39:12 -070087}
88
89status_t MessageQueue::postMessage(
Mathias Agopianf61c57f2011-11-23 16:49:10 -080090 const sp<MessageBase>& messageHandler, nsecs_t relTime)
Mathias Agopianf1d8e872009-04-20 19:39:12 -070091{
Mathias Agopianf61c57f2011-11-23 16:49:10 -080092 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 Agopianf1d8e872009-04-20 19:39:12 -070099}
100
101status_t MessageQueue::invalidate() {
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800102 android_atomic_or(1, &mInvalidatePending);
103 mLooper->wake();
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700104 return NO_ERROR;
105}
106
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700107// ---------------------------------------------------------------------------
108
109}; // namespace android