Clarify Alarm::Schedule() API with kImmediately.
Alarm::Schedule() was often being call with a {} time_point argument, to
request the task be run immediately. Instead, add a kImmediately
constant to make it clear to readers of the calling code what is
intended.
This change also makes minor changes inside the Alarm::Schedule()
implementation to prevent underflow in the delay calculations when
kImmediately is used.
Change-Id: I22e6f0d5ac4c1fd6926c8d20dd5332acd538ca68
Reviewed-on: https://chromium-review.googlesource.com/c/openscreen/+/2057807
Commit-Queue: Yuri Wiitala <miu@chromium.org>
Reviewed-by: Max Yakimakha <yakimakha@chromium.org>
diff --git a/platform/impl/task_runner.cc b/platform/impl/task_runner.cc
index dae79c4..d735f3c 100644
--- a/platform/impl/task_runner.cc
+++ b/platform/impl/task_runner.cc
@@ -36,8 +36,12 @@
void TaskRunnerImpl::PostPackagedTaskWithDelay(Task task,
Clock::duration delay) {
std::lock_guard<std::mutex> lock(task_mutex_);
- delayed_tasks_.emplace(
- std::make_pair(now_function_() + delay, std::move(task)));
+ if (delay <= Clock::duration::zero()) {
+ tasks_.emplace_back(std::move(task));
+ } else {
+ delayed_tasks_.emplace(
+ std::make_pair(now_function_() + delay, std::move(task)));
+ }
if (task_waiter_) {
task_waiter_->OnTaskPosted();
} else {