Skip to content

Commit

Permalink
Update vendor
Browse files Browse the repository at this point in the history
  • Loading branch information
tekezo committed Oct 18, 2023
1 parent 2a56846 commit 2078a8a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

// pqrs::dispatcher v2.12
// pqrs::dispatcher v2.13

// (C) Copyright Takayama Fumihiko 2018.
// Distributed under the Boost Software License, Version 1.0.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class timer final {
}
}

// First, `function` is called once, and then `function` is called every interval specified by `interval`.
void start(std::function<void(void)> function,
duration interval) {
enabled_ = true;
Expand Down Expand Up @@ -62,6 +63,25 @@ class timer final {
return enabled_;
}

// Update the interval.
// Any `function` call reserved before calling this method will be canceled, and the `function` will be called after `interval` duration.
//
// Special cases:.
// - If `interval` == duration(0), this method works same as `stop`.
// - If `interval` is same as the current interval, this method does nothing.
void set_interval(duration interval) {
if (interval == duration(0)) {
stop();
} else if (interval != interval_) {
dispatcher_client_.enqueue_to_dispatcher([this, interval] {
++current_function_id_;
interval_ = interval;

enqueue(current_function_id_);
});
}
}

private:
// This method is executed in the dispatcher thread.
void call_function(int function_id) {
Expand All @@ -79,6 +99,10 @@ class timer final {
});
}

enqueue(function_id);
}

void enqueue(int function_id) {
dispatcher_client_.enqueue_to_dispatcher(
[this, function_id] {
call_function(function_id);
Expand Down

0 comments on commit 2078a8a

Please sign in to comment.