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 | |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame^] | 28 | #include "Barrier.h" |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 29 | |
| 30 | namespace android { |
| 31 | |
| 32 | // --------------------------------------------------------------------------- |
| 33 | |
Mathias Agopian | b6683b5 | 2009-04-28 03:17:50 -0700 | [diff] [blame] | 34 | class MessageBase; |
| 35 | |
| 36 | class MessageList |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 37 | { |
Mathias Agopian | b6683b5 | 2009-04-28 03:17:50 -0700 | [diff] [blame] | 38 | List< sp<MessageBase> > mList; |
| 39 | typedef List< sp<MessageBase> > LIST; |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 40 | public: |
Mathias Agopian | b6683b5 | 2009-04-28 03:17:50 -0700 | [diff] [blame] | 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; } |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame^] | 66 | |
| 67 | // waits for the handler to be processed |
| 68 | void wait() const { barrier.wait(); } |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 69 | |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame^] | 70 | // releases all waiters. this is done automatically if |
| 71 | // handler returns true |
| 72 | void notify() const { barrier.open(); } |
| 73 | |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 74 | protected: |
| 75 | virtual ~MessageBase() { } |
| 76 | |
| 77 | private: |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame^] | 78 | mutable Barrier barrier; |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 79 | friend class LightRefBase<MessageBase>; |
| 80 | }; |
| 81 | |
| 82 | inline bool operator < (const MessageBase& lhs, const MessageBase& rhs) { |
| 83 | return lhs.when < rhs.when; |
| 84 | } |
| 85 | |
| 86 | // --------------------------------------------------------------------------- |
| 87 | |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 88 | class MessageQueue |
| 89 | { |
Mathias Agopian | b6683b5 | 2009-04-28 03:17:50 -0700 | [diff] [blame] | 90 | typedef List< sp<MessageBase> > LIST; |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 91 | public: |
| 92 | |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 93 | MessageQueue(); |
| 94 | ~MessageQueue(); |
| 95 | |
| 96 | // pre-defined messages |
| 97 | enum { |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame^] | 98 | INVALIDATE = '_upd' |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 99 | }; |
| 100 | |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame^] | 101 | sp<MessageBase> waitMessage(nsecs_t timeout = -1); |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 102 | |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame^] | 103 | status_t postMessage(const sp<MessageBase>& message, |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 104 | nsecs_t reltime=0, uint32_t flags = 0); |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame^] | 105 | |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 106 | status_t invalidate(); |
| 107 | |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame^] | 108 | void dump(const sp<MessageBase>& message); |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 109 | |
| 110 | private: |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame^] | 111 | status_t queueMessage(const sp<MessageBase>& message, |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 112 | nsecs_t reltime, uint32_t flags); |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame^] | 113 | void dumpLocked(const sp<MessageBase>& message); |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 114 | |
| 115 | Mutex mLock; |
| 116 | Condition mCondition; |
| 117 | MessageList mMessages; |
| 118 | bool mInvalidate; |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame^] | 119 | sp<MessageBase> mInvalidateMessage; |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | // --------------------------------------------------------------------------- |
| 123 | |
| 124 | }; // namespace android |
| 125 | |
| 126 | #endif /* ANDROID_MESSAGE_QUEUE_H */ |