genode/repos/base/src/timer/periodic/time_source.cc
Norman Feske bf62d6b896 Move timer from os to base repository
Since the timer and timeout handling is part of the base library (the
dynamic linker), it belongs to the base repository.

Besides moving the timer and its related infrastructure (alarm, timeout
libs, tests) to the base repository, this patch also moves the timer
from the 'drivers' subdirectory directly to 'src' and disamibuates the
timer's build locations for the various kernels. Otherwise the different
timer implementations could interfere with each other when using one
build directory with multiple kernels.

Note that this patch changes the include paths for the former os/timer,
os/alarm.h, os/duration.h, and os/timed_semaphore.h to base/.

Issue #3101
2019-01-14 12:33:57 +01:00

53 lines
1.2 KiB
C++

/*
* \brief Time source that uses sleeping by the means of the kernel
* \author Norman Feske
* \author Martin Stein
* \date 2009-06-16
*/
/*
* Copyright (C) 2009-2017 Genode Labs GmbH
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU Affero General Public License version 3.
*/
/* local includes */
#include <time_source.h>
using namespace Genode;
void Timer::Time_source::schedule_timeout(Microseconds duration,
Timeout_handler &handler)
{
Genode::Lock::Guard lock_guard(_lock);
Threaded_time_source::handler(handler);
_next_timeout_us = duration.value;
}
void Timer::Time_source::_wait_for_irq()
{
enum { SLEEP_GRANULARITY_US = 1000UL };
unsigned long last_time_us = curr_time().trunc_to_plain_us().value;
_lock.lock();
while (_next_timeout_us > 0) {
_lock.unlock();
try { _usleep(SLEEP_GRANULARITY_US); }
catch (Blocking_canceled) { }
unsigned long curr_time_us = curr_time().trunc_to_plain_us().value;
unsigned long sleep_duration_us = curr_time_us - last_time_us;
last_time_us = curr_time_us;
_lock.lock();
if (_next_timeout_us >= sleep_duration_us)
_next_timeout_us -= sleep_duration_us;
else
break;
}
_lock.unlock();
}