blob: fdde75c0a0e8cfd655949786619a1d392a664160 [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 Agopian22289362011-12-02 16:11:19 -080047 : mLooper(new Looper(true))
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:
61 // we got woken-up there is work to do in the main loop
Mathias Agopian22289362011-12-02 16:11:19 -080062 return;
Mathias Agopianf61c57f2011-11-23 16:49:10 -080063
64 case ALOOPER_POLL_CALLBACK:
65 // callback was handled, loop again
66 continue;
67
68 case ALOOPER_POLL_TIMEOUT:
69 // timeout (should not happen)
70 continue;
71
72 case ALOOPER_POLL_ERROR:
73 LOGE("ALOOPER_POLL_ERROR");
74 continue;
75
76 default:
77 // should not happen
78 LOGE("Looper::pollOnce() returned unknown status %d", ret);
79 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 Agopianf61c57f2011-11-23 16:49:10 -080097 mLooper->wake();
Mathias Agopianf1d8e872009-04-20 19:39:12 -070098 return NO_ERROR;
99}
100
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700101// ---------------------------------------------------------------------------
102
103}; // namespace android