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#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> 26#include <utils/List.h> 27 28#include "Barrier.h" 29 30namespace android { 31 32// --------------------------------------------------------------------------- 33 34class MessageBase; 35 36class MessageList 37{ 38 List< sp<MessageBase> > mList; 39 typedef List< sp<MessageBase> > LIST; 40public: 41 inline LIST::iterator begin() { return mList.begin(); } 42 inline LIST::const_iterator begin() const { return mList.begin(); } 43 inline LIST::iterator end() { return mList.end(); } 44 inline LIST::const_iterator end() const { return mList.end(); } 45 inline bool isEmpty() const { return mList.empty(); } 46 void insert(const sp<MessageBase>& node); 47 void remove(LIST::iterator pos); 48}; 49 50// ============================================================================ 51 52class MessageBase : 53 public LightRefBase<MessageBase> 54{ 55public: 56 nsecs_t when; 57 uint32_t what; 58 int32_t arg0; 59 60 MessageBase() : when(0), what(0), arg0(0) { } 61 MessageBase(uint32_t what, int32_t arg0=0) 62 : when(0), what(what), arg0(arg0) { } 63 64 // return true if message has a handler 65 virtual bool handler() { return false; } 66 67 // waits for the handler to be processed 68 void wait() const { barrier.wait(); } 69 70 // releases all waiters. this is done automatically if 71 // handler returns true 72 void notify() const { barrier.open(); } 73 74protected: 75 virtual ~MessageBase() { } 76 77private: 78 mutable Barrier barrier; 79 friend class LightRefBase<MessageBase>; 80}; 81 82inline bool operator < (const MessageBase& lhs, const MessageBase& rhs) { 83 return lhs.when < rhs.when; 84} 85 86// --------------------------------------------------------------------------- 87 88class MessageQueue 89{ 90 typedef List< sp<MessageBase> > LIST; 91public: 92 93 MessageQueue(); 94 ~MessageQueue(); 95 96 // pre-defined messages 97 enum { 98 INVALIDATE = '_upd' 99 }; 100 101 sp<MessageBase> waitMessage(nsecs_t timeout = -1); 102 103 status_t postMessage(const sp<MessageBase>& message, 104 nsecs_t reltime=0, uint32_t flags = 0); 105 106 status_t invalidate(); 107 108 void dump(const sp<MessageBase>& message); 109 110private: 111 status_t queueMessage(const sp<MessageBase>& message, 112 nsecs_t reltime, uint32_t flags); 113 void dumpLocked(const sp<MessageBase>& message); 114 115 Mutex mLock; 116 Condition mCondition; 117 MessageList mMessages; 118 bool mInvalidate; 119 sp<MessageBase> mInvalidateMessage; 120}; 121 122// --------------------------------------------------------------------------- 123 124}; // namespace android 125 126#endif /* ANDROID_MESSAGE_QUEUE_H */ 127