Merge change Ib64ea2dd into eclair-mr2
* changes: Improvements to TimedEventQueue.
This commit is contained in:
@@ -31,7 +31,8 @@
|
|||||||
namespace android {
|
namespace android {
|
||||||
|
|
||||||
TimedEventQueue::TimedEventQueue()
|
TimedEventQueue::TimedEventQueue()
|
||||||
: mRunning(false),
|
: mNextEventID(1),
|
||||||
|
mRunning(false),
|
||||||
mStopped(false) {
|
mStopped(false) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,26 +77,29 @@ void TimedEventQueue::stop(bool flush) {
|
|||||||
mRunning = false;
|
mRunning = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void TimedEventQueue::postEvent(const sp<Event> &event) {
|
TimedEventQueue::event_id TimedEventQueue::postEvent(const sp<Event> &event) {
|
||||||
// Reserve an earlier timeslot an INT64_MIN to be able to post
|
// Reserve an earlier timeslot an INT64_MIN to be able to post
|
||||||
// the StopEvent to the absolute head of the queue.
|
// the StopEvent to the absolute head of the queue.
|
||||||
postTimedEvent(event, INT64_MIN + 1);
|
return postTimedEvent(event, INT64_MIN + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TimedEventQueue::postEventToBack(const sp<Event> &event) {
|
TimedEventQueue::event_id TimedEventQueue::postEventToBack(
|
||||||
postTimedEvent(event, INT64_MAX);
|
const sp<Event> &event) {
|
||||||
|
return postTimedEvent(event, INT64_MAX);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TimedEventQueue::postEventWithDelay(
|
TimedEventQueue::event_id TimedEventQueue::postEventWithDelay(
|
||||||
const sp<Event> &event, int64_t delay_us) {
|
const sp<Event> &event, int64_t delay_us) {
|
||||||
CHECK(delay_us >= 0);
|
CHECK(delay_us >= 0);
|
||||||
postTimedEvent(event, getRealTimeUs() + delay_us);
|
return postTimedEvent(event, getRealTimeUs() + delay_us);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TimedEventQueue::postTimedEvent(
|
TimedEventQueue::event_id TimedEventQueue::postTimedEvent(
|
||||||
const sp<Event> &event, int64_t realtime_us) {
|
const sp<Event> &event, int64_t realtime_us) {
|
||||||
Mutex::Autolock autoLock(mLock);
|
Mutex::Autolock autoLock(mLock);
|
||||||
|
|
||||||
|
event->setEventID(mNextEventID++);
|
||||||
|
|
||||||
List<QueueItem>::iterator it = mQueue.begin();
|
List<QueueItem>::iterator it = mQueue.begin();
|
||||||
while (it != mQueue.end() && realtime_us >= (*it).realtime_us) {
|
while (it != mQueue.end() && realtime_us >= (*it).realtime_us) {
|
||||||
++it;
|
++it;
|
||||||
@@ -112,29 +116,60 @@ void TimedEventQueue::postTimedEvent(
|
|||||||
mQueue.insert(it, item);
|
mQueue.insert(it, item);
|
||||||
|
|
||||||
mQueueNotEmptyCondition.signal();
|
mQueueNotEmptyCondition.signal();
|
||||||
|
|
||||||
|
return event->eventID();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool TimedEventQueue::cancelEvent(const sp<Event> &event) {
|
static bool MatchesEventID(
|
||||||
Mutex::Autolock autoLock(mLock);
|
void *cookie, const sp<TimedEventQueue::Event> &event) {
|
||||||
|
TimedEventQueue::event_id *id =
|
||||||
|
static_cast<TimedEventQueue::event_id *>(cookie);
|
||||||
|
|
||||||
List<QueueItem>::iterator it = mQueue.begin();
|
if (event->eventID() != *id) {
|
||||||
while (it != mQueue.end() && (*it).event != event) {
|
|
||||||
++it;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (it == mQueue.end()) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (it == mQueue.begin()) {
|
*id = 0;
|
||||||
mQueueHeadChangedCondition.signal();
|
|
||||||
}
|
|
||||||
|
|
||||||
mQueue.erase(it);
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool TimedEventQueue::cancelEvent(event_id id) {
|
||||||
|
CHECK(id != 0);
|
||||||
|
|
||||||
|
cancelEvents(&MatchesEventID, &id, true /* stopAfterFirstMatch */);
|
||||||
|
|
||||||
|
// if MatchesEventID found a match, it will have set id to 0
|
||||||
|
// (which is not a valid event_id).
|
||||||
|
|
||||||
|
return id == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void TimedEventQueue::cancelEvents(
|
||||||
|
bool (*predicate)(void *cookie, const sp<Event> &event),
|
||||||
|
void *cookie,
|
||||||
|
bool stopAfterFirstMatch) {
|
||||||
|
Mutex::Autolock autoLock(mLock);
|
||||||
|
|
||||||
|
List<QueueItem>::iterator it = mQueue.begin();
|
||||||
|
while (it != mQueue.end()) {
|
||||||
|
if (!(*predicate)(cookie, (*it).event)) {
|
||||||
|
++it;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (it == mQueue.begin()) {
|
||||||
|
mQueueHeadChangedCondition.signal();
|
||||||
|
}
|
||||||
|
|
||||||
|
it = mQueue.erase(it);
|
||||||
|
|
||||||
|
if (stopAfterFirstMatch) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// static
|
// static
|
||||||
int64_t TimedEventQueue::getRealTimeUs() {
|
int64_t TimedEventQueue::getRealTimeUs() {
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
|
|||||||
@@ -28,16 +28,31 @@ namespace android {
|
|||||||
|
|
||||||
struct TimedEventQueue {
|
struct TimedEventQueue {
|
||||||
|
|
||||||
|
typedef int32_t event_id;
|
||||||
|
|
||||||
struct Event : public RefBase {
|
struct Event : public RefBase {
|
||||||
Event() {}
|
Event()
|
||||||
|
: mEventID(0) {
|
||||||
|
}
|
||||||
|
|
||||||
virtual ~Event() {}
|
virtual ~Event() {}
|
||||||
|
|
||||||
|
event_id eventID() {
|
||||||
|
return mEventID;
|
||||||
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void fire(TimedEventQueue *queue, int64_t now_us) = 0;
|
virtual void fire(TimedEventQueue *queue, int64_t now_us) = 0;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend class TimedEventQueue;
|
friend class TimedEventQueue;
|
||||||
|
|
||||||
|
event_id mEventID;
|
||||||
|
|
||||||
|
void setEventID(event_id id) {
|
||||||
|
mEventID = id;
|
||||||
|
}
|
||||||
|
|
||||||
Event(const Event &);
|
Event(const Event &);
|
||||||
Event &operator=(const Event &);
|
Event &operator=(const Event &);
|
||||||
};
|
};
|
||||||
@@ -55,21 +70,29 @@ struct TimedEventQueue {
|
|||||||
|
|
||||||
// Posts an event to the front of the queue (after all events that
|
// Posts an event to the front of the queue (after all events that
|
||||||
// have previously been posted to the front but before timed events).
|
// have previously been posted to the front but before timed events).
|
||||||
void postEvent(const sp<Event> &event);
|
event_id postEvent(const sp<Event> &event);
|
||||||
|
|
||||||
void postEventToBack(const sp<Event> &event);
|
event_id postEventToBack(const sp<Event> &event);
|
||||||
|
|
||||||
// It is an error to post an event with a negative delay.
|
// It is an error to post an event with a negative delay.
|
||||||
void postEventWithDelay(const sp<Event> &event, int64_t delay_us);
|
event_id postEventWithDelay(const sp<Event> &event, int64_t delay_us);
|
||||||
|
|
||||||
// If the event is to be posted at a time that has already passed,
|
// If the event is to be posted at a time that has already passed,
|
||||||
// it will fire as soon as possible.
|
// it will fire as soon as possible.
|
||||||
void postTimedEvent(const sp<Event> &event, int64_t realtime_us);
|
event_id postTimedEvent(const sp<Event> &event, int64_t realtime_us);
|
||||||
|
|
||||||
// Returns true iff event is currently in the queue and has been
|
// Returns true iff event is currently in the queue and has been
|
||||||
// successfully cancelled. In this case the event will have been
|
// successfully cancelled. In this case the event will have been
|
||||||
// removed from the queue and won't fire.
|
// removed from the queue and won't fire.
|
||||||
bool cancelEvent(const sp<Event> &event);
|
bool cancelEvent(event_id id);
|
||||||
|
|
||||||
|
// Cancel any pending event that satisfies the predicate.
|
||||||
|
// If stopAfterFirstMatch is true, only cancels the first event
|
||||||
|
// satisfying the predicate (if any).
|
||||||
|
void cancelEvents(
|
||||||
|
bool (*predicate)(void *cookie, const sp<Event> &event),
|
||||||
|
void *cookie,
|
||||||
|
bool stopAfterFirstMatch = false);
|
||||||
|
|
||||||
static int64_t getRealTimeUs();
|
static int64_t getRealTimeUs();
|
||||||
|
|
||||||
@@ -90,6 +113,7 @@ private:
|
|||||||
Mutex mLock;
|
Mutex mLock;
|
||||||
Condition mQueueNotEmptyCondition;
|
Condition mQueueNotEmptyCondition;
|
||||||
Condition mQueueHeadChangedCondition;
|
Condition mQueueHeadChangedCondition;
|
||||||
|
event_id mNextEventID;
|
||||||
|
|
||||||
bool mRunning;
|
bool mRunning;
|
||||||
bool mStopped;
|
bool mStopped;
|
||||||
|
|||||||
Reference in New Issue
Block a user