2016-05-18 12:20:04 +02:00
|
|
|
/*
|
|
|
|
* \brief A clock manages a continuous time and timeouts on it
|
|
|
|
* \author Martin Stein
|
|
|
|
* \date 2016-03-23
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2017-02-20 13:23:52 +01:00
|
|
|
* Copyright (C) 2016-2017 Genode Labs GmbH
|
2016-05-18 12:20:04 +02:00
|
|
|
*
|
|
|
|
* This file is part of the Genode OS framework, which is distributed
|
2017-02-20 13:23:52 +01:00
|
|
|
* under the terms of the GNU Affero General Public License version 3.
|
2016-05-18 12:20:04 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* Core includes */
|
|
|
|
#include <kernel/clock.h>
|
2017-02-21 13:46:59 +01:00
|
|
|
#include <hw/assert.h>
|
2016-05-18 12:20:04 +02:00
|
|
|
|
|
|
|
using namespace Kernel;
|
|
|
|
|
|
|
|
|
|
|
|
time_t Clock::us_to_tics(time_t const us) const
|
|
|
|
{
|
|
|
|
return _timer->us_to_tics(us);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
time_t Clock::timeout_age_us(Timeout const * const timeout) const
|
|
|
|
{
|
|
|
|
time_t const age = (time_t)_time - timeout->_start;
|
|
|
|
return _timer->tics_to_us(age);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
time_t Clock::timeout_max_us() const
|
|
|
|
{
|
|
|
|
return _timer->tics_to_us(_timer->max_value());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-07 14:39:14 +02:00
|
|
|
bool Clock::_time_overflow(time_t const duration) const
|
|
|
|
{
|
|
|
|
return duration > ~(time_t)0 - _time;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-18 12:20:04 +02:00
|
|
|
void Clock::set_timeout(Timeout * const timeout, time_t const duration)
|
|
|
|
{
|
2016-09-07 14:39:14 +02:00
|
|
|
/*
|
|
|
|
* Remove timeout if it is already in use. Timeouts may get overridden as
|
|
|
|
* result of an update.
|
|
|
|
*/
|
|
|
|
if (timeout->_listed) {
|
|
|
|
_timeout_list[timeout->_end_period].remove(timeout);
|
|
|
|
} else {
|
|
|
|
timeout->_listed = true; }
|
|
|
|
|
|
|
|
/* set timeout parameters */
|
|
|
|
timeout->_start = _time;
|
|
|
|
timeout->_end = _time + duration;
|
|
|
|
timeout->_end_period = _time_overflow(duration) ? !_time_period :
|
|
|
|
_time_period;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Insert timeout. Timeouts are ordered ascending according to their end
|
|
|
|
* time to be able to quickly determine the nearest timeout.
|
|
|
|
*/
|
|
|
|
Genode::List<Timeout> & list = _timeout_list[timeout->_end_period];
|
|
|
|
Timeout * t1 = 0;
|
|
|
|
for (Timeout * t2 = list.first(); t2 && t2->_end < timeout->_end;
|
|
|
|
t1 = t2, t2 = t2->next()) { }
|
|
|
|
|
|
|
|
list.insert(timeout, t1);
|
2016-05-18 12:20:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Clock::schedule_timeout()
|
|
|
|
{
|
2016-09-07 14:39:14 +02:00
|
|
|
/* get the timeout with the nearest end time */
|
|
|
|
Timeout * timeout = _timeout_list[_time_period].first();
|
|
|
|
if (!timeout) {
|
|
|
|
timeout = _timeout_list[!_time_period].first();
|
|
|
|
assert(timeout);
|
|
|
|
}
|
|
|
|
/* install timeout at timer hardware */
|
2016-05-18 12:20:04 +02:00
|
|
|
time_t const duration = (time_t)timeout->_end - _time;
|
|
|
|
_last_timeout_duration = duration;
|
|
|
|
_timer->start_one_shot(duration, _cpu_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
time_t Clock::update_time()
|
|
|
|
{
|
2016-09-07 14:39:14 +02:00
|
|
|
/* determine how much time has passed */
|
2016-05-18 12:20:04 +02:00
|
|
|
time_t const old_value = _last_timeout_duration;
|
|
|
|
time_t const new_value = _timer->value(_cpu_id);
|
|
|
|
time_t const duration = old_value > new_value ? old_value - new_value : 1;
|
2016-09-07 14:39:14 +02:00
|
|
|
|
|
|
|
/* is this the end of the current period? */
|
|
|
|
if (_time_overflow(duration)) {
|
|
|
|
|
|
|
|
/* flush all timeouts of current period */
|
|
|
|
Genode::List<Timeout> & list = _timeout_list[_time_period];
|
|
|
|
while (true) {
|
|
|
|
Timeout * const timeout = list.first();
|
|
|
|
if (!timeout) { break; }
|
|
|
|
list.remove(timeout);
|
|
|
|
timeout->_listed = false;
|
|
|
|
timeout->timeout_triggered();
|
|
|
|
}
|
|
|
|
/* switch to next period */
|
|
|
|
_time_period = !_time_period;
|
|
|
|
|
|
|
|
}
|
|
|
|
/* update time */
|
2016-05-18 12:20:04 +02:00
|
|
|
_time += duration;
|
|
|
|
return duration;
|
|
|
|
}
|
|
|
|
|
2016-09-07 14:39:14 +02:00
|
|
|
|
2016-05-18 12:20:04 +02:00
|
|
|
void Clock::process_timeouts()
|
|
|
|
{
|
2016-09-07 14:39:14 +02:00
|
|
|
/*
|
|
|
|
* Walk through timeouts until the first whose end time is in the future.
|
|
|
|
* Consider only the current periods list as all timeouts of the next
|
|
|
|
* period must be in the future.
|
|
|
|
*/
|
|
|
|
Genode::List<Timeout> & list = _timeout_list[_time_period];
|
2016-05-18 12:20:04 +02:00
|
|
|
while (true) {
|
2016-09-07 14:39:14 +02:00
|
|
|
Timeout * const timeout = list.first();
|
|
|
|
if (!timeout) { break; }
|
|
|
|
if (timeout->_end > _time) { break; }
|
|
|
|
list.remove(timeout);
|
|
|
|
timeout->_listed = false;
|
2016-05-18 12:20:04 +02:00
|
|
|
timeout->timeout_triggered();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Clock::Clock(unsigned const cpu_id, Timer * const timer)
|
|
|
|
:
|
|
|
|
_cpu_id(cpu_id), _timer(timer) { }
|