mirror of
https://github.com/clangen/musikcube.git
synced 2025-04-16 23:42:41 +00:00
Added MessageQueue::WaitAndDispatch(), which should be usable for
timer-like functionality.
This commit is contained in:
parent
3aa4871e62
commit
1a00c83c7b
@ -45,6 +45,20 @@ MessageQueue::MessageQueue() {
|
||||
|
||||
}
|
||||
|
||||
void MessageQueue::WaitAndDispatch() {
|
||||
LockT lock(this->queueMutex);
|
||||
|
||||
if (this->queue.size()) {
|
||||
auto next = system_clock::now().time_since_epoch() - this->queue.front()->time;
|
||||
waitForDispatch.wait_for(lock, next);
|
||||
}
|
||||
else {
|
||||
waitForDispatch.wait(lock);
|
||||
}
|
||||
|
||||
this->Dispatch();
|
||||
}
|
||||
|
||||
void MessageQueue::Dispatch() {
|
||||
milliseconds now = duration_cast<milliseconds>(
|
||||
system_clock::now().time_since_epoch());
|
||||
@ -131,7 +145,13 @@ void MessageQueue::Post(IMessagePtr message, int64 delayMs) {
|
||||
}
|
||||
}
|
||||
|
||||
bool first = (curr == this->queue.begin());
|
||||
|
||||
this->queue.insert(curr, m);
|
||||
|
||||
if (first) {
|
||||
this->waitForDispatch.notify_all();
|
||||
}
|
||||
}
|
||||
|
||||
void MessageQueue::Debounce(IMessagePtr message, int64 delayMs) {
|
||||
|
@ -38,6 +38,7 @@
|
||||
#include "IMessageTarget.h"
|
||||
|
||||
#include <mutex>
|
||||
#include <condition_variable>
|
||||
#include <chrono>
|
||||
|
||||
namespace cursespp {
|
||||
@ -49,6 +50,7 @@ namespace cursespp {
|
||||
void Remove(IMessageTarget *target, int type = -1);
|
||||
void Debounce(IMessagePtr message, int64 delayMs = 0);
|
||||
|
||||
void WaitAndDispatch();
|
||||
void Dispatch();
|
||||
|
||||
private:
|
||||
@ -59,6 +61,7 @@ namespace cursespp {
|
||||
|
||||
std::recursive_mutex queueMutex;
|
||||
std::list<EnqueuedMessage*> queue;
|
||||
std::condition_variable_any waitForDispatch;
|
||||
|
||||
void Dispatch(IMessagePtr message);
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user