genode/repos/base-hw/src/core/kernel/clock.cc
Norman Feske 17c79a9e23 base: avoid use of deprecated base/printf.h
Besides adapting the components to the use of base/log.h, the patch
cleans up a few base headers, i.e., it removes unused includes from
root/component.h, specifically base/heap.h and
ram_session/ram_session.h. Hence, components that relied on the implicit
inclusion of those headers have to manually include those headers now.

While adjusting the log messages, I repeatedly stumbled over the problem
that printing char * arguments is ambiguous. It is unclear whether to
print the argument as pointer or null-terminated string. To overcome
this problem, the patch introduces a new type 'Cstring' that allows the
caller to express that the argument should be handled as null-terminated
string. As a nice side effect, with this type in place, the optional len
argument of the 'String' class could be removed. Instead of supplying a
pair of (char const *, size_t), the constructor accepts a 'Cstring'.
This, in turn, clears the way let the 'String' constructor use the new
output mechanism to assemble a string from multiple arguments (and
thereby getting rid of snprintf within Genode in the near future).

To enforce the explicit resolution of the char * ambiguity, the 'char *'
overload of the 'print' function is marked as deleted.

Issue #1987
2016-08-29 17:27:10 +02:00

101 lines
2.2 KiB
C++

/*
* \brief A clock manages a continuous time and timeouts on it
* \author Martin Stein
* \date 2016-03-23
*/
/*
* Copyright (C) 2016 Genode Labs GmbH
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU General Public License version 2.
*/
/* Core includes */
#include <kernel/clock.h>
using namespace Kernel;
void Clock::_insert_timeout(Timeout * const timeout)
{
/* timeouts may get re-inserted as result of an update */
if (timeout->_listed) { _timeout_list.remove(timeout); }
timeout->_listed = true;
/* timeouts are ordered ascending according to their end time */
Timeout * t1 = 0;
Timeout * t2 = _timeout_list.first();
for (; t2 && t2->_end < timeout->_end; t1 = t2, t2 = t2->next()) ;
_timeout_list.insert(timeout, t1);
}
void Clock::_remove_timeout(Timeout * const timeout)
{
timeout->_listed = false;
_timeout_list.remove(timeout);
}
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());
}
void Clock::set_timeout(Timeout * const timeout, time_t const duration)
{
timeout->_start = _time;
timeout->_end = _time + duration;
_insert_timeout(timeout);
}
void Clock::schedule_timeout()
{
Timeout const * const timeout = _timeout_list.first();
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()
{
/* update time */
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;
_time += duration;
return duration;
}
void Clock::process_timeouts()
{
while (true) {
Timeout * const timeout = _timeout_list.first();
if (!timeout || timeout->_end > _time) { break; }
_remove_timeout(timeout);
timeout->timeout_triggered();
}
}
Clock::Clock(unsigned const cpu_id, Timer * const timer)
:
_cpu_id(cpu_id), _timer(timer) { }