From 12e1ae9d72b46620249fe077351958038dddf506 Mon Sep 17 00:00:00 2001 From: Christian Prochaska Date: Tue, 20 Mar 2012 16:57:58 +0100 Subject: [PATCH] Implement '_nanosleep()' This patch provides an implementation of the '_nanosleep()' libc function, which blocks on a timed semaphore for the given time, but at least 10ms. This should result in better performance than creating a timer connection on every call (for thread-safety), but could still be improved. Fixes #158. --- libports/lib/mk/libc.mk | 2 +- libports/src/lib/libc/dummies.cc | 1 - libports/src/lib/libc/nanosleep.cc | 41 ++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 libports/src/lib/libc/nanosleep.cc diff --git a/libports/lib/mk/libc.mk b/libports/lib/mk/libc.mk index f80892775..ed744e325 100644 --- a/libports/lib/mk/libc.mk +++ b/libports/lib/mk/libc.mk @@ -12,7 +12,7 @@ LIBS += timed_semaphore cxx SRC_CC = atexit.cc dummies.cc rlimit.cc sysctl.cc readlink.cc munmap.cc \ issetugid.cc errno.cc gai_strerror.cc clock_gettime.cc \ gettimeofday.cc malloc.cc progname.cc fd_alloc.cc file_operations.cc \ - plugin.cc plugin_registry.cc select.cc exit.cc environ.cc + plugin.cc plugin_registry.cc select.cc exit.cc environ.cc nanosleep.cc # # Files from string library that are not included in libc-raw_string because diff --git a/libports/src/lib/libc/dummies.cc b/libports/src/lib/libc/dummies.cc index 4ba13d106..60d4a39aa 100644 --- a/libports/src/lib/libc/dummies.cc +++ b/libports/src/lib/libc/dummies.cc @@ -86,7 +86,6 @@ DUMMY(-1, mkfifo) DUMMY(-1, mknod) DUMMY(-1, mprotect) DUMMY(-1, nanosleep) -DUMMY(-1, _nanosleep) DUMMY(-1, __nsdefaultsrc) DUMMY(-1, _nsdispatch) DUMMY(-1, _openat) diff --git a/libports/src/lib/libc/nanosleep.cc b/libports/src/lib/libc/nanosleep.cc new file mode 100644 index 000000000..a28984606 --- /dev/null +++ b/libports/src/lib/libc/nanosleep.cc @@ -0,0 +1,41 @@ +/* + * \brief C-library back end + * \author Christian Prochaska + * \date 2012-03-20 + */ + +/* + * Copyright (C) 2008-2012 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. + */ + +#include + +#include + +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); + + /* 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) { + } + + if (rem) { + rem->tv_sec = 0; + rem->tv_nsec = 0; + } + + return 0; +}