libc: split task.cc into multiple files
This patch is the first step of re-organizing the internal structure of
the libc. The original version involved many direct calls of global
functions (often with side effects) across compilation units, which
made the control flow (e.g., the initialization sequence) hard to
follow.
The new version replaces those ad-hoc interactions with dedicated
interfaces (like suspend.h, resume.h, select.h, current_time.h). The
underlying facilities are provided by the central Libc::Kernel and
selectively propagated to the various compilation units. The latter is
done by a sequence of 'init_*' calls, which eventually will be replaced
by constructor calls.
The addition of new headers increases the chance for name clashes with
existing (public) headers. To disambiguate libc-internal header files
from public headers, this patch moves the former into a new 'internal/'
subdirectory. This makes the include directives easier to follow and the
libc's source-tree structure more tidy.
There are still a few legacies left, which cannot easily be removed
right now (e.g., because noux relies on them). However, the patch moves
those bad apples to legacy.h and legacy.cc, which highlights the
deprecation of those functions.
Issue #3497
2019-09-18 20:19:10 +02:00
|
|
|
/*
|
|
|
|
* \brief Libc-internal timer handling
|
|
|
|
* \author Norman Feske
|
|
|
|
* \date 2019-09-16
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2019 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _LIBC__INTERNAL__TIMER_H_
|
|
|
|
#define _LIBC__INTERNAL__TIMER_H_
|
|
|
|
|
|
|
|
/* Genode includes */
|
|
|
|
#include <timer_session/connection.h>
|
|
|
|
|
2020-02-05 15:47:09 +01:00
|
|
|
/* libc-internal includes */
|
|
|
|
#include <internal/types.h>
|
|
|
|
|
libc: split task.cc into multiple files
This patch is the first step of re-organizing the internal structure of
the libc. The original version involved many direct calls of global
functions (often with side effects) across compilation units, which
made the control flow (e.g., the initialization sequence) hard to
follow.
The new version replaces those ad-hoc interactions with dedicated
interfaces (like suspend.h, resume.h, select.h, current_time.h). The
underlying facilities are provided by the central Libc::Kernel and
selectively propagated to the various compilation units. The latter is
done by a sequence of 'init_*' calls, which eventually will be replaced
by constructor calls.
The addition of new headers increases the chance for name clashes with
existing (public) headers. To disambiguate libc-internal header files
from public headers, this patch moves the former into a new 'internal/'
subdirectory. This makes the include directives easier to follow and the
libc's source-tree structure more tidy.
There are still a few legacies left, which cannot easily be removed
right now (e.g., because noux relies on them). However, the patch moves
those bad apples to legacy.h and legacy.cc, which highlights the
deprecation of those functions.
Issue #3497
2019-09-18 20:19:10 +02:00
|
|
|
namespace Libc {
|
|
|
|
class Timer;
|
|
|
|
class Timer_accessor;
|
|
|
|
class Timeout;
|
|
|
|
class Timeout_handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct Libc::Timer
|
|
|
|
{
|
|
|
|
::Timer::Connection _timer;
|
|
|
|
|
|
|
|
Timer(Genode::Env &env) : _timer(env) { }
|
|
|
|
|
2019-09-19 20:37:17 +02:00
|
|
|
Duration curr_time()
|
libc: split task.cc into multiple files
This patch is the first step of re-organizing the internal structure of
the libc. The original version involved many direct calls of global
functions (often with side effects) across compilation units, which
made the control flow (e.g., the initialization sequence) hard to
follow.
The new version replaces those ad-hoc interactions with dedicated
interfaces (like suspend.h, resume.h, select.h, current_time.h). The
underlying facilities are provided by the central Libc::Kernel and
selectively propagated to the various compilation units. The latter is
done by a sequence of 'init_*' calls, which eventually will be replaced
by constructor calls.
The addition of new headers increases the chance for name clashes with
existing (public) headers. To disambiguate libc-internal header files
from public headers, this patch moves the former into a new 'internal/'
subdirectory. This makes the include directives easier to follow and the
libc's source-tree structure more tidy.
There are still a few legacies left, which cannot easily be removed
right now (e.g., because noux relies on them). However, the patch moves
those bad apples to legacy.h and legacy.cc, which highlights the
deprecation of those functions.
Issue #3497
2019-09-18 20:19:10 +02:00
|
|
|
{
|
|
|
|
return _timer.curr_time();
|
|
|
|
}
|
|
|
|
|
2019-09-19 20:37:17 +02:00
|
|
|
static Microseconds microseconds(uint64_t timeout_ms)
|
libc: split task.cc into multiple files
This patch is the first step of re-organizing the internal structure of
the libc. The original version involved many direct calls of global
functions (often with side effects) across compilation units, which
made the control flow (e.g., the initialization sequence) hard to
follow.
The new version replaces those ad-hoc interactions with dedicated
interfaces (like suspend.h, resume.h, select.h, current_time.h). The
underlying facilities are provided by the central Libc::Kernel and
selectively propagated to the various compilation units. The latter is
done by a sequence of 'init_*' calls, which eventually will be replaced
by constructor calls.
The addition of new headers increases the chance for name clashes with
existing (public) headers. To disambiguate libc-internal header files
from public headers, this patch moves the former into a new 'internal/'
subdirectory. This makes the include directives easier to follow and the
libc's source-tree structure more tidy.
There are still a few legacies left, which cannot easily be removed
right now (e.g., because noux relies on them). However, the patch moves
those bad apples to legacy.h and legacy.cc, which highlights the
deprecation of those functions.
Issue #3497
2019-09-18 20:19:10 +02:00
|
|
|
{
|
|
|
|
return Microseconds(1000*timeout_ms);
|
|
|
|
}
|
|
|
|
|
2019-09-19 20:37:17 +02:00
|
|
|
static uint64_t max_timeout()
|
libc: split task.cc into multiple files
This patch is the first step of re-organizing the internal structure of
the libc. The original version involved many direct calls of global
functions (often with side effects) across compilation units, which
made the control flow (e.g., the initialization sequence) hard to
follow.
The new version replaces those ad-hoc interactions with dedicated
interfaces (like suspend.h, resume.h, select.h, current_time.h). The
underlying facilities are provided by the central Libc::Kernel and
selectively propagated to the various compilation units. The latter is
done by a sequence of 'init_*' calls, which eventually will be replaced
by constructor calls.
The addition of new headers increases the chance for name clashes with
existing (public) headers. To disambiguate libc-internal header files
from public headers, this patch moves the former into a new 'internal/'
subdirectory. This makes the include directives easier to follow and the
libc's source-tree structure more tidy.
There are still a few legacies left, which cannot easily be removed
right now (e.g., because noux relies on them). However, the patch moves
those bad apples to legacy.h and legacy.cc, which highlights the
deprecation of those functions.
Issue #3497
2019-09-18 20:19:10 +02:00
|
|
|
{
|
|
|
|
return ~0UL/1000;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Interface for obtaining the libc-global timer instance
|
|
|
|
*
|
|
|
|
* The 'Timer' is instantiated on demand whenever the 'Timer_accessor::timer'
|
|
|
|
* method is first called. This way, libc-using components do not depend of a
|
|
|
|
* timer connection unless they actually use time-related functionality.
|
|
|
|
*/
|
|
|
|
struct Libc::Timer_accessor
|
|
|
|
{
|
|
|
|
virtual Timer &timer() = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct Libc::Timeout_handler
|
|
|
|
{
|
|
|
|
virtual void handle_timeout() = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* TODO curr_time wrapping
|
|
|
|
*/
|
|
|
|
struct Libc::Timeout
|
|
|
|
{
|
2019-09-19 20:37:17 +02:00
|
|
|
Timer_accessor &_timer_accessor;
|
libc: split task.cc into multiple files
This patch is the first step of re-organizing the internal structure of
the libc. The original version involved many direct calls of global
functions (often with side effects) across compilation units, which
made the control flow (e.g., the initialization sequence) hard to
follow.
The new version replaces those ad-hoc interactions with dedicated
interfaces (like suspend.h, resume.h, select.h, current_time.h). The
underlying facilities are provided by the central Libc::Kernel and
selectively propagated to the various compilation units. The latter is
done by a sequence of 'init_*' calls, which eventually will be replaced
by constructor calls.
The addition of new headers increases the chance for name clashes with
existing (public) headers. To disambiguate libc-internal header files
from public headers, this patch moves the former into a new 'internal/'
subdirectory. This makes the include directives easier to follow and the
libc's source-tree structure more tidy.
There are still a few legacies left, which cannot easily be removed
right now (e.g., because noux relies on them). However, the patch moves
those bad apples to legacy.h and legacy.cc, which highlights the
deprecation of those functions.
Issue #3497
2019-09-18 20:19:10 +02:00
|
|
|
Timeout_handler &_handler;
|
|
|
|
::Timer::One_shot_timeout<Timeout> _timeout;
|
|
|
|
|
2019-09-19 20:37:17 +02:00
|
|
|
bool _expired = true;
|
|
|
|
uint64_t _absolute_timeout_ms = 0;
|
libc: split task.cc into multiple files
This patch is the first step of re-organizing the internal structure of
the libc. The original version involved many direct calls of global
functions (often with side effects) across compilation units, which
made the control flow (e.g., the initialization sequence) hard to
follow.
The new version replaces those ad-hoc interactions with dedicated
interfaces (like suspend.h, resume.h, select.h, current_time.h). The
underlying facilities are provided by the central Libc::Kernel and
selectively propagated to the various compilation units. The latter is
done by a sequence of 'init_*' calls, which eventually will be replaced
by constructor calls.
The addition of new headers increases the chance for name clashes with
existing (public) headers. To disambiguate libc-internal header files
from public headers, this patch moves the former into a new 'internal/'
subdirectory. This makes the include directives easier to follow and the
libc's source-tree structure more tidy.
There are still a few legacies left, which cannot easily be removed
right now (e.g., because noux relies on them). However, the patch moves
those bad apples to legacy.h and legacy.cc, which highlights the
deprecation of those functions.
Issue #3497
2019-09-18 20:19:10 +02:00
|
|
|
|
|
|
|
void _handle(Duration now)
|
|
|
|
{
|
|
|
|
_expired = true;
|
|
|
|
_absolute_timeout_ms = 0;
|
|
|
|
_handler.handle_timeout();
|
|
|
|
}
|
|
|
|
|
|
|
|
Timeout(Timer_accessor &timer_accessor, Timeout_handler &handler)
|
|
|
|
:
|
|
|
|
_timer_accessor(timer_accessor),
|
|
|
|
_handler(handler),
|
|
|
|
_timeout(_timer_accessor.timer()._timer, *this, &Timeout::_handle)
|
|
|
|
{ }
|
|
|
|
|
2019-09-19 20:37:17 +02:00
|
|
|
void start(uint64_t timeout_ms)
|
libc: split task.cc into multiple files
This patch is the first step of re-organizing the internal structure of
the libc. The original version involved many direct calls of global
functions (often with side effects) across compilation units, which
made the control flow (e.g., the initialization sequence) hard to
follow.
The new version replaces those ad-hoc interactions with dedicated
interfaces (like suspend.h, resume.h, select.h, current_time.h). The
underlying facilities are provided by the central Libc::Kernel and
selectively propagated to the various compilation units. The latter is
done by a sequence of 'init_*' calls, which eventually will be replaced
by constructor calls.
The addition of new headers increases the chance for name clashes with
existing (public) headers. To disambiguate libc-internal header files
from public headers, this patch moves the former into a new 'internal/'
subdirectory. This makes the include directives easier to follow and the
libc's source-tree structure more tidy.
There are still a few legacies left, which cannot easily be removed
right now (e.g., because noux relies on them). However, the patch moves
those bad apples to legacy.h and legacy.cc, which highlights the
deprecation of those functions.
Issue #3497
2019-09-18 20:19:10 +02:00
|
|
|
{
|
|
|
|
Milliseconds const now = _timer_accessor.timer().curr_time().trunc_to_plain_ms();
|
|
|
|
|
|
|
|
_expired = false;
|
|
|
|
_absolute_timeout_ms = now.value + timeout_ms;
|
|
|
|
|
|
|
|
_timeout.schedule(_timer_accessor.timer().microseconds(timeout_ms));
|
|
|
|
}
|
|
|
|
|
2019-09-19 20:37:17 +02:00
|
|
|
uint64_t duration_left() const
|
libc: split task.cc into multiple files
This patch is the first step of re-organizing the internal structure of
the libc. The original version involved many direct calls of global
functions (often with side effects) across compilation units, which
made the control flow (e.g., the initialization sequence) hard to
follow.
The new version replaces those ad-hoc interactions with dedicated
interfaces (like suspend.h, resume.h, select.h, current_time.h). The
underlying facilities are provided by the central Libc::Kernel and
selectively propagated to the various compilation units. The latter is
done by a sequence of 'init_*' calls, which eventually will be replaced
by constructor calls.
The addition of new headers increases the chance for name clashes with
existing (public) headers. To disambiguate libc-internal header files
from public headers, this patch moves the former into a new 'internal/'
subdirectory. This makes the include directives easier to follow and the
libc's source-tree structure more tidy.
There are still a few legacies left, which cannot easily be removed
right now (e.g., because noux relies on them). However, the patch moves
those bad apples to legacy.h and legacy.cc, which highlights the
deprecation of those functions.
Issue #3497
2019-09-18 20:19:10 +02:00
|
|
|
{
|
|
|
|
Milliseconds const now = _timer_accessor.timer().curr_time().trunc_to_plain_ms();
|
|
|
|
|
|
|
|
if (_expired || _absolute_timeout_ms < now.value)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return _absolute_timeout_ms - now.value;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* _LIBC__INTERNAL__TIMER_H_ */
|