blob: cbd530c822a5e8112e263eaf285d125173e2a24c [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 Agopianbe42aef2011-12-03 14:47:29 -080047 : mLooper(new Looper(true)), mWorkPending(0)
Mathias Agopianf1d8e872009-04-20 19:39:12 -070048{
49}
50
Mathias Agopianf61c57f2011-11-23 16:49:10 -080051MessageQueue::~MessageQueue() {
52}
Mathias Agopianb6683b52009-04-28 03:17:50 -070053
Mathias Agopianf61c57f2011-11-23 16:49:10 -080054void MessageQueue::waitMessage() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -070055 do {
Mathias Agopianf61c57f2011-11-23 16:49:10 -080056 IPCThreadState::self()->flushCommands();
Mathias Agopianb6683b52009-04-28 03:17:50 -070057
Mathias Agopianf61c57f2011-11-23 16:49:10 -080058 int32_t ret = mLooper->pollOnce(-1);
59 switch (ret) {
60 case ALOOPER_POLL_WAKE:
Mathias Agopianf61c57f2011-11-23 16:49:10 -080061 case ALOOPER_POLL_CALLBACK:
Mathias Agopianbe42aef2011-12-03 14:47:29 -080062 // callback and/or wake
63 if (android_atomic_and(0, &mWorkPending)) {
64 return;
65 }
Mathias Agopianf61c57f2011-11-23 16:49:10 -080066 continue;
67
68 case ALOOPER_POLL_TIMEOUT:
69 // timeout (should not happen)
70 continue;
71
72 case ALOOPER_POLL_ERROR:
Steve Blocke6f43dd2012-01-06 19:20:56 +000073 ALOGE("ALOOPER_POLL_ERROR");
Mathias Agopianf61c57f2011-11-23 16:49:10 -080074 continue;
75
76 default:
77 // should not happen
Steve Blocke6f43dd2012-01-06 19:20:56 +000078 ALOGE("Looper::pollOnce() returned unknown status %d", ret);
Mathias Agopianf61c57f2011-11-23 16:49:10 -080079 continue;
80 }
81 } while (true);
Mathias Agopianf1d8e872009-04-20 19:39:12 -070082}
83
84status_t MessageQueue::postMessage(
Mathias Agopianf61c57f2011-11-23 16:49:10 -080085 const sp<MessageBase>& messageHandler, nsecs_t relTime)
Mathias Agopianf1d8e872009-04-20 19:39:12 -070086{
Mathias Agopianf61c57f2011-11-23 16:49:10 -080087 const Message dummyMessage;
88 if (relTime > 0) {
89 mLooper->sendMessageDelayed(relTime, messageHandler, dummyMessage);
90 } else {
91 mLooper->sendMessage(messageHandler, dummyMessage);
92 }
93 return NO_ERROR;
Mathias Agopianf1d8e872009-04-20 19:39:12 -070094}
95
96status_t MessageQueue::invalidate() {
Mathias Agopianbe42aef2011-12-03 14:47:29 -080097 if (android_atomic_or(1, &mWorkPending) == 0) {
98 mLooper->wake();
99 }
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700100 return NO_ERROR;
101}
102
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700103// ---------------------------------------------------------------------------
104
105}; // namespace android