blob: 2317d81ab8e94c70d71027cfd50741ece856751e [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#ifndef ANDROID_MESSAGE_QUEUE_H
18#define ANDROID_MESSAGE_QUEUE_H
19
20#include <stdint.h>
21#include <errno.h>
22#include <sys/types.h>
23
24#include <utils/threads.h>
25#include <utils/Timers.h>
Mathias Agopianf61c57f2011-11-23 16:49:10 -080026#include <utils/Looper.h>
Mathias Agopianf1d8e872009-04-20 19:39:12 -070027
Mathias Agopianbb641242010-05-18 17:06:55 -070028#include "Barrier.h"
Mathias Agopianf1d8e872009-04-20 19:39:12 -070029
30namespace android {
31
32// ---------------------------------------------------------------------------
33
Mathias Agopianf61c57f2011-11-23 16:49:10 -080034class MessageBase : public MessageHandler
Mathias Agopianf1d8e872009-04-20 19:39:12 -070035{
36public:
Mathias Agopianf61c57f2011-11-23 16:49:10 -080037 MessageBase();
Mathias Agopianf1d8e872009-04-20 19:39:12 -070038
39 // return true if message has a handler
Mathias Agopianf61c57f2011-11-23 16:49:10 -080040 virtual bool handler() = 0;
Mathias Agopianbb641242010-05-18 17:06:55 -070041
42 // waits for the handler to be processed
43 void wait() const { barrier.wait(); }
Mathias Agopianbb641242010-05-18 17:06:55 -070044
Mathias Agopianf1d8e872009-04-20 19:39:12 -070045protected:
Mathias Agopianf61c57f2011-11-23 16:49:10 -080046 virtual ~MessageBase();
Mathias Agopianf1d8e872009-04-20 19:39:12 -070047
48private:
Mathias Agopianf61c57f2011-11-23 16:49:10 -080049 virtual void handleMessage(const Message& message);
Mathias Agopianf1d8e872009-04-20 19:39:12 -070050
Mathias Agopianf61c57f2011-11-23 16:49:10 -080051 mutable Barrier barrier;
52};
Mathias Agopianf1d8e872009-04-20 19:39:12 -070053
54// ---------------------------------------------------------------------------
55
Mathias Agopianf61c57f2011-11-23 16:49:10 -080056class MessageQueue {
57 sp<Looper> mLooper;
Mathias Agopianbe42aef2011-12-03 14:47:29 -080058 volatile int32_t mWorkPending;
Mathias Agopianf1d8e872009-04-20 19:39:12 -070059
Mathias Agopianf61c57f2011-11-23 16:49:10 -080060public:
Mathias Agopianf1d8e872009-04-20 19:39:12 -070061 MessageQueue();
62 ~MessageQueue();
63
Mathias Agopianf61c57f2011-11-23 16:49:10 -080064 void waitMessage();
65 status_t postMessage(const sp<MessageBase>& message, nsecs_t reltime=0);
Mathias Agopianf1d8e872009-04-20 19:39:12 -070066 status_t invalidate();
Mathias Agopianf1d8e872009-04-20 19:39:12 -070067};
68
69// ---------------------------------------------------------------------------
70
71}; // namespace android
72
73#endif /* ANDROID_MESSAGE_QUEUE_H */