Skip to content

Commit

Permalink
use the priority_queue instead of multimap
Browse files Browse the repository at this point in the history
  • Loading branch information
frodez committed Jan 4, 2022
1 parent d8974e0 commit 54a9a55
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
12 changes: 10 additions & 2 deletions include/spinet/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include <chrono>
#include <condition_variable>
#include <functional>
#include <map>
#include <mutex>
#include <queue>
#include <thread>

namespace spinet {
Expand All @@ -27,6 +27,14 @@ class Timer {
void async_wait_until(TimePoint time_point, const Callback& callback);

private:
struct WaitOperation {
TimePoint time_point;
Callback callback;
friend bool operator<(const WaitOperation& x, const WaitOperation& y) {
return x.time_point < y.time_point;
}
};

Timer(Timer&& other) = delete;
Timer& operator=(Timer&& other) = delete;
Timer(const Timer& other) = delete;
Expand All @@ -43,7 +51,7 @@ class Timer {

std::mutex waiter_mtx_;
std::condition_variable waiter_cv_;
std::multimap<TimePoint, Callback> waiters_;
std::priority_queue<WaitOperation> waiters_;
};

}
30 changes: 18 additions & 12 deletions src/timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,17 @@ void Timer::exec() {
{
std::unique_lock<std::mutex> lck { waiter_mtx_ };
time_point = std::chrono::steady_clock::now();
auto lower = waiters_.begin();
auto upper = waiters_.upper_bound(time_point);
std::vector<std::pair<TimePoint, Callback>> callbacks { lower, upper };
waiters_.erase(lower, upper);
std::vector<WaitOperation> operations {};
while (!waiters_.empty()) {
auto& operation = waiters_.top();
if (operation.time_point > time_point) {
break;
}
operations.push_back(operation);
waiters_.pop();
}
lck.unlock();
for (auto& [prev_time_point, callback] : callbacks) {
for (auto& [prev_time_point, callback] : operations) {
callback(prev_time_point, time_point);
}
}
Expand All @@ -75,22 +80,23 @@ void Timer::exec() {
}
}
std::unique_lock<std::mutex> lck { waiter_mtx_ };
waiters_.clear();
{
std::priority_queue<WaitOperation, std::vector<WaitOperation>> empty_queue {};
waiters_.swap(empty_queue);
}
running_ = false;
}

void Timer::async_wait_for(Duration duration, const Callback &callback) {
void Timer::async_wait_for(Duration duration, const Callback& callback) {
async_wait_until(std::chrono::steady_clock::now() + duration, callback);
}

void Timer::async_wait_until(TimePoint time_point, const Callback &callback) {
auto time_since_epoch = time_point.time_since_epoch();
TimePoint floored_time_point { time_since_epoch - (time_since_epoch % precision_) };
void Timer::async_wait_until(TimePoint time_point, const Callback& callback) {
std::unique_lock<std::mutex> lck { waiter_mtx_ };
if (waiters_.empty()) {
waiters_.insert({ floored_time_point, callback });
waiters_.push({ time_point, callback });
waiter_cv_.notify_one();
} else {
waiters_.insert({ floored_time_point, callback });
waiters_.push({ time_point, callback });
}
}

0 comments on commit 54a9a55

Please sign in to comment.