2019-04-26 12:47:33 +02:00
|
|
|
/*
|
|
|
|
* \brief C-library back end
|
|
|
|
* \author Christian Prochaska
|
|
|
|
* \author Emery Hemingway
|
|
|
|
* \date 2012-03-20
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2008-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.
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
/* libc includes */
|
2019-04-26 12:47:33 +02:00
|
|
|
#include <sys/time.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
|
|
|
/* libc-internal includes */
|
|
|
|
#include <internal/suspend.h>
|
|
|
|
#include <internal/init.h>
|
2019-04-26 12:47:33 +02:00
|
|
|
|
2019-09-19 20:37:17 +02:00
|
|
|
using namespace Libc;
|
2019-04-26 12:47:33 +02:00
|
|
|
|
2019-09-19 20:37:17 +02:00
|
|
|
|
|
|
|
static Suspend *_suspend_ptr;
|
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 Libc::init_sleep(Suspend &suspend)
|
|
|
|
{
|
|
|
|
_suspend_ptr = &suspend;
|
|
|
|
}
|
|
|
|
|
2019-04-26 12:47:33 +02:00
|
|
|
|
|
|
|
static void millisleep(Genode::uint64_t timeout_ms)
|
|
|
|
{
|
|
|
|
Genode::uint64_t remaining_ms = timeout_ms;
|
|
|
|
|
2019-09-19 20:37:17 +02:00
|
|
|
struct Missing_call_of_init_sleep : Exception { };
|
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
|
|
|
if (!_suspend_ptr)
|
|
|
|
throw Missing_call_of_init_sleep();
|
|
|
|
|
2019-04-26 12:47:33 +02:00
|
|
|
struct Check : Libc::Suspend_functor
|
|
|
|
{
|
|
|
|
bool suspend() override { return true; }
|
|
|
|
|
|
|
|
} check;
|
|
|
|
|
|
|
|
while (remaining_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
|
|
|
remaining_ms = _suspend_ptr->suspend(check, remaining_ms);
|
2019-04-26 12:47:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
extern "C" __attribute__((weak))
|
|
|
|
int nanosleep(const struct timespec *req, struct timespec *rem)
|
|
|
|
{
|
2020-02-05 10:59:32 +01:00
|
|
|
/* XXX nanosleep({0,0}) may yield but is not required to do so */
|
|
|
|
if (!req->tv_sec && !req->tv_nsec) return 0;
|
|
|
|
|
2019-09-19 20:37:17 +02:00
|
|
|
Genode::uint64_t sleep_ms = (Genode::uint64_t)req->tv_sec*1000;
|
2019-04-26 12:47:33 +02:00
|
|
|
if (req->tv_nsec)
|
|
|
|
sleep_ms += req->tv_nsec / 1000000;
|
2020-02-05 10:59:32 +01:00
|
|
|
|
|
|
|
/* sleep at least 1 ms */
|
|
|
|
millisleep(sleep_ms ? : 1);
|
2019-04-26 12:47:33 +02:00
|
|
|
|
|
|
|
if (rem)
|
|
|
|
*rem = { 0, 0 };
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" __attribute__((alias("nanosleep")))
|
|
|
|
int __sys_nanosleep(const struct timespec *req, struct timespec *rem);
|
|
|
|
|
|
|
|
|
|
|
|
extern "C" __attribute__((weak))
|
|
|
|
int clock_nanosleep(clockid_t clock_id, int flags,
|
|
|
|
const struct timespec *rqt,
|
|
|
|
struct timespec *rmt)
|
|
|
|
{
|
|
|
|
if (flags & TIMER_ABSTIME) {
|
|
|
|
struct timespec now_ts { 0 };
|
|
|
|
if (clock_gettime(clock_id, &now_ts) != 0) {
|
2019-09-19 20:37:17 +02:00
|
|
|
error(__func__, ": RTC device not configured");
|
2019-04-26 12:47:33 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (now_ts.tv_sec <= rqt->tv_sec && now_ts.tv_nsec < rqt->tv_nsec) {
|
|
|
|
struct timespec new_ts = {
|
|
|
|
.tv_sec = rqt->tv_sec - now_ts.tv_sec,
|
|
|
|
.tv_nsec = rqt->tv_nsec - now_ts.tv_nsec,
|
|
|
|
};
|
|
|
|
return nanosleep(&new_ts, rmt);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return nanosleep(rqt, rmt);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" __attribute__((alias("clock_nanosleep")))
|
|
|
|
int __sys_clock_nanosleep(clockid_t clock_id, int flags,
|
|
|
|
const struct timespec *rqt,
|
|
|
|
struct timespec *rmt);
|
|
|
|
|
|
|
|
|
|
|
|
extern "C" __attribute__((weak))
|
|
|
|
unsigned int sleep(unsigned int seconds)
|
|
|
|
{
|
2020-02-05 10:59:32 +01:00
|
|
|
/* XXX sleep(0) may yield but is not required to do so */
|
|
|
|
if (!seconds) return 0;
|
|
|
|
|
|
|
|
Libc::uint64_t const sleep_ms = (Libc::uint64_t)seconds*1000;
|
|
|
|
|
|
|
|
/* sleep at least 1 ms */
|
|
|
|
millisleep(sleep_ms ? : 1);
|
2019-04-26 12:47:33 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
extern "C" __attribute__((weak))
|
|
|
|
int usleep(useconds_t useconds)
|
|
|
|
{
|
2020-02-05 10:59:32 +01:00
|
|
|
/* XXX usleep(0) may yield but is not required to do so */
|
|
|
|
if (!useconds) return 0;
|
|
|
|
|
|
|
|
Libc::uint64_t const sleep_ms = (Libc::uint64_t)useconds/1000;
|
|
|
|
|
|
|
|
/* sleep at least 1 ms */
|
|
|
|
millisleep(sleep_ms ? : 1);
|
2019-04-26 12:47:33 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" __attribute__((alias("usleep")))
|
|
|
|
int _usleep(useconds_t useconds);
|