Commit 82c55254 authored by Kirill Smelkov's avatar Kirill Smelkov

time: Rearrange code a bit

In the next patch we will add reworked implementation of timers - that
will no longer use dumb approach to work via threads - and in that
implementation it will make sense to regroup the organization of code a
bit for better clarity. Prepare for that:

- move Timer.reset to stay in between _new_timer and stop. This will be
  handy because Timer.reset will be interaction with both even loop
  (coming right before new) and stop.

- move new_timer to the place where we commonly keep wrapper
  "create-timer or ticker" routines to improve signal/noise ration for
  the place where actual interaction in between code parts happen.

For the reference my general approach to order things is to go from high
level to down and group things by interaction along the way. This way
things turns out to be the most easily readable and understandable.

/proposed-for-review-on !26
parent ae9b6f7d
// Copyright (C) 2019-2020 Nexedi SA and Contributors.
// Copyright (C) 2019-2024 Nexedi SA and Contributors.
// Kirill Smelkov <kirr@nexedi.com>
//
// This program is free software: you can Use, Study, Modify and Redistribute
......@@ -51,6 +51,11 @@ Timer after_func(double dt, func<void()> f) {
return _new_timer(dt, f);
}
Timer new_timer(double dt) {
return _new_timer(dt, nil);
}
// Ticker
_Ticker::_Ticker() {}
_Ticker::~_Ticker() {}
......@@ -128,8 +133,22 @@ Timer _new_timer(double dt, func<void()> f) {
return t;
}
Timer new_timer(double dt) {
return _new_timer(dt, nil);
void _Timer::reset(double dt) {
_Timer &t = *this;
t._mu.lock();
if (t._dt != INFINITY) {
t._mu.unlock();
panic("Timer.reset: the timer is armed; must be stopped or expired");
}
t._dt = dt;
t._ver += 1;
// TODO rework timers so that new timer does not spawn new goroutine.
Timer tref = newref(&t); // pass t reference to spawned goroutine
go([tref, dt](int ver) {
tref->_fire(dt, ver);
}, t._ver);
t._mu.unlock();
}
bool _Timer::stop() {
......@@ -155,24 +174,6 @@ bool _Timer::stop() {
return canceled;
}
void _Timer::reset(double dt) {
_Timer &t = *this;
t._mu.lock();
if (t._dt != INFINITY) {
t._mu.unlock();
panic("Timer.reset: the timer is armed; must be stopped or expired");
}
t._dt = dt;
t._ver += 1;
// TODO rework timers so that new timer does not spawn new goroutine.
Timer tref = newref(&t); // pass t reference to spawned goroutine
go([tref, dt](int ver) {
tref->_fire(dt, ver);
}, t._ver);
t._mu.unlock();
}
void _Timer::_fire(double dt, int ver) {
_Timer &t = *this;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment