Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 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> |
Mathias Agopian | b6683b5 | 2009-04-28 03:17:50 -0700 | [diff] [blame] | 26 | #include <utils/List.h> |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 27 | |
| 28 | |
| 29 | namespace android { |
| 30 | |
| 31 | // --------------------------------------------------------------------------- |
| 32 | |
Mathias Agopian | b6683b5 | 2009-04-28 03:17:50 -0700 | [diff] [blame] | 33 | class MessageBase; |
| 34 | |
| 35 | class MessageList |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 36 | { |
Mathias Agopian | b6683b5 | 2009-04-28 03:17:50 -0700 | [diff] [blame] | 37 | List< sp<MessageBase> > mList; |
| 38 | typedef List< sp<MessageBase> > LIST; |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 39 | public: |
Mathias Agopian | b6683b5 | 2009-04-28 03:17:50 -0700 | [diff] [blame] | 40 | typedef sp<MessageBase> value_type; |
| 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); |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | // ============================================================================ |
| 51 | |
| 52 | class MessageBase : |
Mathias Agopian | b6683b5 | 2009-04-28 03:17:50 -0700 | [diff] [blame] | 53 | public LightRefBase<MessageBase> |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 54 | { |
| 55 | public: |
| 56 | nsecs_t when; |
| 57 | uint32_t what; |
| 58 | int32_t arg0; |
| 59 | |
Mathias Agopian | 0aa758d | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 60 | MessageBase() : when(0), what(0), arg0(0) { } |
| 61 | MessageBase(uint32_t what, int32_t arg0=0) |
| 62 | : when(0), what(what), arg0(arg0) { } |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 63 | |
| 64 | // return true if message has a handler |
| 65 | virtual bool handler() { return false; } |
| 66 | |
| 67 | protected: |
| 68 | virtual ~MessageBase() { } |
| 69 | |
| 70 | private: |
| 71 | friend class LightRefBase<MessageBase>; |
| 72 | }; |
| 73 | |
| 74 | inline bool operator < (const MessageBase& lhs, const MessageBase& rhs) { |
| 75 | return lhs.when < rhs.when; |
| 76 | } |
| 77 | |
| 78 | // --------------------------------------------------------------------------- |
| 79 | |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 80 | class MessageQueue |
| 81 | { |
Mathias Agopian | b6683b5 | 2009-04-28 03:17:50 -0700 | [diff] [blame] | 82 | typedef List< sp<MessageBase> > LIST; |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 83 | public: |
| 84 | |
| 85 | // this is a work-around the multichar constant warning. A macro would |
| 86 | // work too, but would pollute the namespace. |
| 87 | template <int a, int b, int c, int d> |
| 88 | struct WHAT { |
| 89 | static const uint32_t Value = |
| 90 | (uint32_t(a&0xff)<<24)|(uint32_t(b&0xff)<<16)| |
| 91 | (uint32_t(c&0xff)<<8)|uint32_t(d&0xff); |
| 92 | }; |
| 93 | |
| 94 | MessageQueue(); |
| 95 | ~MessageQueue(); |
| 96 | |
| 97 | // pre-defined messages |
| 98 | enum { |
| 99 | INVALIDATE = WHAT<'_','p','d','t'>::Value |
| 100 | }; |
| 101 | |
Mathias Agopian | b6683b5 | 2009-04-28 03:17:50 -0700 | [diff] [blame] | 102 | MessageList::value_type waitMessage(nsecs_t timeout = -1); |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 103 | |
Mathias Agopian | b6683b5 | 2009-04-28 03:17:50 -0700 | [diff] [blame] | 104 | status_t postMessage(const MessageList::value_type& message, |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 105 | nsecs_t reltime=0, uint32_t flags = 0); |
| 106 | |
| 107 | status_t invalidate(); |
| 108 | |
Mathias Agopian | b6683b5 | 2009-04-28 03:17:50 -0700 | [diff] [blame] | 109 | void dump(const MessageList::value_type& message); |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 110 | |
| 111 | private: |
Mathias Agopian | b6683b5 | 2009-04-28 03:17:50 -0700 | [diff] [blame] | 112 | status_t queueMessage(const MessageList::value_type& message, |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 113 | nsecs_t reltime, uint32_t flags); |
Mathias Agopian | b6683b5 | 2009-04-28 03:17:50 -0700 | [diff] [blame] | 114 | void dumpLocked(const MessageList::value_type& message); |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 115 | |
| 116 | Mutex mLock; |
| 117 | Condition mCondition; |
| 118 | MessageList mMessages; |
| 119 | bool mInvalidate; |
Mathias Agopian | b6683b5 | 2009-04-28 03:17:50 -0700 | [diff] [blame] | 120 | MessageList::value_type mInvalidateMessage; |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | // --------------------------------------------------------------------------- |
| 124 | |
| 125 | }; // namespace android |
| 126 | |
| 127 | #endif /* ANDROID_MESSAGE_QUEUE_H */ |