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 6bc9e3c commit cd4c789
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
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
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 {
});