2009-04-21 02:39:12 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2009 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef ANDROID_MESSAGE_QUEUE_H
|
|
|
|
#define ANDROID_MESSAGE_QUEUE_H
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <utils/threads.h>
|
|
|
|
#include <utils/Timers.h>
|
2009-04-28 10:17:50 +00:00
|
|
|
#include <utils/List.h>
|
2009-04-21 02:39:12 +00:00
|
|
|
|
2010-05-19 00:06:55 +00:00
|
|
|
#include "Barrier.h"
|
2009-04-21 02:39:12 +00:00
|
|
|
|
|
|
|
namespace android {
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2009-04-28 10:17:50 +00:00
|
|
|
class MessageBase;
|
2009-04-21 02:39:12 +00:00
|
|
|
|
2009-04-28 10:17:50 +00:00
|
|
|
class MessageList
|
2009-04-21 02:39:12 +00:00
|
|
|
{
|
2009-04-28 10:17:50 +00:00
|
|
|
List< sp<MessageBase> > mList;
|
|
|
|
typedef List< sp<MessageBase> > LIST;
|
2009-04-21 02:39:12 +00:00
|
|
|
public:
|
2009-04-28 10:17:50 +00:00
|
|
|
inline LIST::iterator begin() { return mList.begin(); }
|
|
|
|
inline LIST::const_iterator begin() const { return mList.begin(); }
|
|
|
|
inline LIST::iterator end() { return mList.end(); }
|
|
|
|
inline LIST::const_iterator end() const { return mList.end(); }
|
|
|
|
inline bool isEmpty() const { return mList.empty(); }
|
|
|
|
void insert(const sp<MessageBase>& node);
|
|
|
|
void remove(LIST::iterator pos);
|
2009-04-21 02:39:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
|
|
|
|
class MessageBase :
|
2009-04-28 10:17:50 +00:00
|
|
|
public LightRefBase<MessageBase>
|
2009-04-21 02:39:12 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsecs_t when;
|
|
|
|
uint32_t what;
|
|
|
|
int32_t arg0;
|
|
|
|
|
2009-04-22 22:23:34 +00:00
|
|
|
MessageBase() : when(0), what(0), arg0(0) { }
|
|
|
|
MessageBase(uint32_t what, int32_t arg0=0)
|
|
|
|
: when(0), what(what), arg0(arg0) { }
|
2009-04-21 02:39:12 +00:00
|
|
|
|
|
|
|
// return true if message has a handler
|
|
|
|
virtual bool handler() { return false; }
|
2010-05-19 00:06:55 +00:00
|
|
|
|
|
|
|
// waits for the handler to be processed
|
|
|
|
void wait() const { barrier.wait(); }
|
2009-04-21 02:39:12 +00:00
|
|
|
|
2010-05-19 00:06:55 +00:00
|
|
|
// releases all waiters. this is done automatically if
|
|
|
|
// handler returns true
|
|
|
|
void notify() const { barrier.open(); }
|
|
|
|
|
2009-04-21 02:39:12 +00:00
|
|
|
protected:
|
|
|
|
virtual ~MessageBase() { }
|
|
|
|
|
|
|
|
private:
|
2010-05-19 00:06:55 +00:00
|
|
|
mutable Barrier barrier;
|
2009-04-21 02:39:12 +00:00
|
|
|
friend class LightRefBase<MessageBase>;
|
|
|
|
};
|
|
|
|
|
|
|
|
inline bool operator < (const MessageBase& lhs, const MessageBase& rhs) {
|
|
|
|
return lhs.when < rhs.when;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class MessageQueue
|
|
|
|
{
|
2009-04-28 10:17:50 +00:00
|
|
|
typedef List< sp<MessageBase> > LIST;
|
2009-04-21 02:39:12 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
MessageQueue();
|
|
|
|
~MessageQueue();
|
|
|
|
|
|
|
|
// pre-defined messages
|
|
|
|
enum {
|
2010-05-19 00:06:55 +00:00
|
|
|
INVALIDATE = '_upd'
|
2009-04-21 02:39:12 +00:00
|
|
|
};
|
|
|
|
|
2010-05-19 00:06:55 +00:00
|
|
|
sp<MessageBase> waitMessage(nsecs_t timeout = -1);
|
2009-04-21 02:39:12 +00:00
|
|
|
|
2010-05-19 00:06:55 +00:00
|
|
|
status_t postMessage(const sp<MessageBase>& message,
|
2009-04-21 02:39:12 +00:00
|
|
|
nsecs_t reltime=0, uint32_t flags = 0);
|
2010-05-19 00:06:55 +00:00
|
|
|
|
2009-04-21 02:39:12 +00:00
|
|
|
status_t invalidate();
|
|
|
|
|
2010-05-19 00:06:55 +00:00
|
|
|
void dump(const sp<MessageBase>& message);
|
2009-04-21 02:39:12 +00:00
|
|
|
|
|
|
|
private:
|
2010-05-19 00:06:55 +00:00
|
|
|
status_t queueMessage(const sp<MessageBase>& message,
|
2009-04-21 02:39:12 +00:00
|
|
|
nsecs_t reltime, uint32_t flags);
|
2010-05-19 00:06:55 +00:00
|
|
|
void dumpLocked(const sp<MessageBase>& message);
|
2009-04-21 02:39:12 +00:00
|
|
|
|
|
|
|
Mutex mLock;
|
|
|
|
Condition mCondition;
|
|
|
|
MessageList mMessages;
|
|
|
|
bool mInvalidate;
|
2010-05-19 00:06:55 +00:00
|
|
|
sp<MessageBase> mInvalidateMessage;
|
2009-04-21 02:39:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
}; // namespace android
|
|
|
|
|
|
|
|
#endif /* ANDROID_MESSAGE_QUEUE_H */
|