2 * Copyright (C) 2009 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef ANDROID_MESSAGE_QUEUE_H
18 #define ANDROID_MESSAGE_QUEUE_H
22 #include <sys/types.h>
24 #include <utils/threads.h>
25 #include <utils/Timers.h>
26 #include <utils/List.h>
32 // ---------------------------------------------------------------------------
38 List< sp<MessageBase> > mList;
39 typedef List< sp<MessageBase> > LIST;
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);
50 // ============================================================================
53 public LightRefBase<MessageBase>
60 MessageBase() : when(0), what(0), arg0(0) { }
61 MessageBase(uint32_t what, int32_t arg0=0)
62 : when(0), what(what), arg0(arg0) { }
64 // return true if message has a handler
65 virtual bool handler() { return false; }
67 // waits for the handler to be processed
68 void wait() const { barrier.wait(); }
70 // releases all waiters. this is done automatically if
71 // handler returns true
72 void notify() const { barrier.open(); }
75 virtual ~MessageBase() { }
78 mutable Barrier barrier;
79 friend class LightRefBase<MessageBase>;
82 inline bool operator < (const MessageBase& lhs, const MessageBase& rhs) {
83 return lhs.when < rhs.when;
86 // ---------------------------------------------------------------------------
90 typedef List< sp<MessageBase> > LIST;
96 // pre-defined messages
101 sp<MessageBase> waitMessage(nsecs_t timeout = -1);
103 status_t postMessage(const sp<MessageBase>& message,
104 nsecs_t reltime=0, uint32_t flags = 0);
106 status_t invalidate();
108 void dump(const sp<MessageBase>& message);
111 status_t queueMessage(const sp<MessageBase>& message,
112 nsecs_t reltime, uint32_t flags);
113 void dumpLocked(const sp<MessageBase>& message);
116 Condition mCondition;
117 MessageList mMessages;
119 sp<MessageBase> mInvalidateMessage;
122 // ---------------------------------------------------------------------------
124 }; // namespace android
126 #endif /* ANDROID_MESSAGE_QUEUE_H */