libc: replace Timed_semaphore by suspend with timeout

This commit is contained in:
Christian Helmuth 2017-02-10 22:38:37 +01:00 committed by Norman Feske
parent 3048017d90
commit 8185a49b4c
6 changed files with 53 additions and 40 deletions

View File

@ -11,32 +11,34 @@
* under the terms of the GNU General Public License version 2.
*/
#include <base/printf.h>
#include <os/timed_semaphore.h>
/* Libc includes */
#include <sys/time.h>
namespace Libc {
extern time_t read_rtc();
}
#include "task.h"
namespace Libc { time_t read_rtc(); }
extern "C" __attribute__((weak))
int clock_gettime(clockid_t clk_id, struct timespec *tp)
{
if (!tp) return 0;
static bool read_rtc = false;
static time_t rtc = 0;
static unsigned long t0 = 0;
if (!read_rtc) {
rtc = Libc::read_rtc();
read_rtc = true;
t0 = Libc::current_time();
}
Genode::Alarm::Time time = Genode::Timeout_thread::alarm_timer()->time();
unsigned long time = Libc::current_time() - t0;
if (tp) {
tp->tv_sec = rtc + time / 1000;
tp->tv_nsec = (time % 1000) * (1000 * 1000);
}
tp->tv_sec = rtc + time/1000;
tp->tv_nsec = (time % 1000) * (1000*1000);
return 0;
}

View File

@ -11,31 +11,34 @@
* under the terms of the GNU General Public License version 2.
*/
#include <os/timed_semaphore.h>
/* Libc includes */
#include <sys/time.h>
namespace Libc {
extern time_t read_rtc();
}
#include "task.h"
namespace Libc { time_t read_rtc(); }
extern "C" __attribute__((weak))
int gettimeofday(struct timeval *tv, struct timezone *tz)
int gettimeofday(struct timeval *tv, struct timezone *)
{
if (!tv) return 0;
static bool read_rtc = false;
static time_t rtc = 0;
static unsigned long t0 = 0;
if (!read_rtc) {
rtc = Libc::read_rtc();
read_rtc = true;
t0 = Libc::current_time();
}
Genode::Alarm::Time time = Genode::Timeout_thread::alarm_timer()->time();
unsigned long time = Libc::current_time() - t0;
if (tv) {
tv->tv_sec = rtc + time / 1000;
tv->tv_usec = (time % 1000) * 1000;
}
tv->tv_sec = rtc + time/1000;
tv->tv_usec = (time % 1000) * 1000;
return 0;
}

View File

@ -11,26 +11,19 @@
* under the terms of the GNU General Public License version 2.
*/
#include <os/timed_semaphore.h>
/* Libc includes */
#include <sys/time.h>
#include "task.h"
using namespace Genode;
extern "C" __attribute__((weak))
int _nanosleep(const struct timespec *req, struct timespec *rem)
{
Genode::Alarm::Time sleep_msec = (req->tv_sec * 1000) + (req->tv_nsec / 1000000);
unsigned long sleep_ms = req->tv_sec*1000 + req->tv_nsec/1000000;
/* Timed_semaphore does not support timeouts < 10ms */
if (sleep_msec < 10)
sleep_msec = 10;
Timed_semaphore sem(0);
try {
sem.down(sleep_msec);
} catch(Timeout_exception) {
}
do { sleep_ms = Libc::suspend(sleep_ms); } while (sleep_ms);
if (rem) {
rem->tv_sec = 0;

View File

@ -14,12 +14,9 @@
* under the terms of the GNU General Public License version 2.
*/
#include <base/printf.h>
#include <os/timed_semaphore.h>
/* Libc includes */
#include <libc-plugin/plugin_registry.h>
#include <libc-plugin/plugin.h>
#include <sys/select.h>
#include <sys/poll.h>
#include <stdlib.h>

View File

@ -539,17 +539,24 @@ struct Libc::Kernel
*/
unsigned long suspend(unsigned long timeout_ms)
{
if (timeout_ms > _timer_accessor.timer().max_timeout())
if (timeout_ms > 0
&& timeout_ms > _timer_accessor.timer().max_timeout()) {
Genode::warning("libc: limiting exceeding timeout of ",
timeout_ms, " ms to maximum of ",
_timer_accessor.timer().max_timeout(), " ms");
timeout_ms = min(timeout_ms, _timer_accessor.timer().max_timeout());
timeout_ms = min(timeout_ms, _timer_accessor.timer().max_timeout());
}
return _main_context() ? _suspend_main(timeout_ms)
: _pthreads.suspend_myself(timeout_ms);
}
unsigned long current_time()
{
return _timer_accessor.timer().curr_time();
}
/**
* Called from the main context (by fork)
*/
@ -634,6 +641,12 @@ unsigned long Libc::suspend(unsigned long timeout_ms)
}
unsigned long Libc::current_time()
{
return kernel->current_time();
}
void Libc::schedule_suspend(void (*suspended) ())
{
if (!kernel) {

View File

@ -49,6 +49,11 @@ namespace Libc {
*/
unsigned long suspend(unsigned long timeout_ms = 0UL);
/**
* Get time since startup in ms
*/
unsigned long current_time();
/**
* Suspend main user context and the component entrypoint
*