mirror of
https://github.com/TomHarte/CLK.git
synced 2025-01-13 07:30:21 +00:00
Merge branch 'master' into C++BestEffortUpdater
This commit is contained in:
commit
ec202ed8be
@ -13,15 +13,20 @@
|
||||
using namespace Concurrency;
|
||||
|
||||
void BestEffortUpdater::update() {
|
||||
// Perform an update only if one is not currently ongoing.
|
||||
if(!update_is_ongoing_.test_and_set()) {
|
||||
async_task_queue_.enqueue([this]() {
|
||||
// Get time now using the highest-resolution clock provided by the implementation, and determine
|
||||
// the duration since the last time this section was entered.
|
||||
std::chrono::time_point<std::chrono::high_resolution_clock> now = std::chrono::high_resolution_clock::now();
|
||||
auto elapsed = now - previous_time_point_;
|
||||
previous_time_point_ = now;
|
||||
|
||||
if(has_previous_time_point_) {
|
||||
int64_t timestamp = std::chrono::duration_cast<std::chrono::nanoseconds>(elapsed).count();
|
||||
double cycles = ((timestamp * clock_rate_) / 1e9) + error_;
|
||||
// If the duration is valid, convert it to integer cycles, maintaining a rolling error and call the delegate
|
||||
// if there is one.
|
||||
int64_t duration = std::chrono::duration_cast<std::chrono::nanoseconds>(elapsed).count();
|
||||
double cycles = ((static_cast<double>(duration) * clock_rate_) / 1e9) + error_;
|
||||
error_ = fmod(cycles, 1.0);
|
||||
|
||||
if(delegate_) {
|
||||
@ -32,6 +37,7 @@ void BestEffortUpdater::update() {
|
||||
has_previous_time_point_ = true;
|
||||
}
|
||||
|
||||
// Allow furthers updates to occur.
|
||||
update_is_ongoing_.clear();
|
||||
});
|
||||
} else {
|
||||
|
@ -20,6 +20,8 @@ namespace Concurrency {
|
||||
Accepts timing cues from multiple threads and ensures that a delegate receives calls to total
|
||||
a certain number of cycles per second, that those calls are strictly serialised, and that no
|
||||
backlog of calls accrues.
|
||||
|
||||
No guarantees about the thread that the delegate will be called on are made.
|
||||
*/
|
||||
class BestEffortUpdater {
|
||||
public:
|
||||
|
Loading…
x
Reference in New Issue
Block a user