From 1a438b64a56b328aa2437cfa734c0be0ada6cdbb Mon Sep 17 00:00:00 2001 From: Christian Mehlis Date: Fri, 28 Feb 2014 15:09:47 +0100 Subject: [PATCH] posix: added sleep and usleep --- Makefile.dep | 6 ++++++ sys/posix/Makefile | 2 +- sys/posix/include/unistd.h | 44 ++++++++++++++++++++++++++++++++++++++ sys/posix/unistd.c | 16 ++++++++++++++ 4 files changed, 67 insertions(+), 1 deletion(-) diff --git a/Makefile.dep b/Makefile.dep index f7b80ef62b..e1a8c08ca4 100644 --- a/Makefile.dep +++ b/Makefile.dep @@ -20,6 +20,12 @@ ifneq (,$(filter posix,$(USEMODULE))) ifeq (,$(filter uart0,$(USEMODULE))) USEMODULE += uart0 endif + ifeq (,$(filter timex,$(USEMODULE))) + USEMODULE += timex + endif + ifeq (,$(filter vtimer,$(USEMODULE))) + USEMODULE += vtimer + endif endif ifneq (,$(filter uart0,$(USEMODULE))) diff --git a/sys/posix/Makefile b/sys/posix/Makefile index f6159ae25e..844718cae3 100644 --- a/sys/posix/Makefile +++ b/sys/posix/Makefile @@ -1,3 +1,3 @@ -MODULE =posix +MODULE = posix include $(RIOTBASE)/Makefile.base diff --git a/sys/posix/include/unistd.h b/sys/posix/include/unistd.h index 5ec8ec9cf5..390cd265da 100644 --- a/sys/posix/include/unistd.h +++ b/sys/posix/include/unistd.h @@ -24,6 +24,11 @@ #ifndef _UNISTD_H #define _UNISTD_H +#include + +#include "timex.h" +#include "vtimer.h" + #define STDIN_FILENO 0 ///< stdin file descriptor #define STDOUT_FILENO 1 ///< stdout file descriptor #define STDERR_FILENO 2 ///< stderr file descriptor @@ -47,6 +52,45 @@ */ int close(int fildes); +typedef uint32_t useconds_t; + +/** + * @brief the caller will sleep for given amount of micro seconds + * @details The usleep() function will cause the calling thread to be + * suspended from execution until either the number of real-time microseconds + * specified by the argument useconds has elapsed or a signal is delivered to + * the calling thread and its action is to invoke a signal-catching function + * or to terminate the process. The suspension time may be longer than + * requested due to the scheduling of other activity by the system. + * + * @see + * The Open Group Base Specification Issue 2, usleep + * + * + * @param useconds time to sleep in micro seconds + * @return 0 on success + */ +int usleep(useconds_t useconds); + +/** + * @brief the caller will sleep for given amount of seconds + * @details The sleep() function shall cause the calling thread to be suspended + * from execution until either the number of realtime seconds + * specified by the argument seconds has elapsed or a signal is + * delivered to the calling thread and its action is to invoke a + * signal-catching function or to terminate the process. The + * suspension time may be longer than requested due to the scheduling + * of other activity by the system. + * + * @see + * The Open Group Base Specification Issue 6, sleep + * + * + * @param seconds time to sleep in seconds + * @return 0 on success + */ +unsigned int sleep(unsigned int seconds); + /** * @} */ diff --git a/sys/posix/unistd.c b/sys/posix/unistd.c index 0dfcc9b0ef..dfc484dd78 100644 --- a/sys/posix/unistd.c +++ b/sys/posix/unistd.c @@ -11,6 +11,7 @@ * @file fd.c * @brief Providing implementation for close for fds defined in fd.h. * @author Martin Lenders + * @author Christian Mehlis */ #include @@ -36,6 +37,21 @@ int close(int fildes) return 0; } +int usleep(useconds_t useconds) +{ + timex_t time = timex_set(0, useconds); + timex_normalize(&time); + vtimer_sleep(time); + return 0; +} + +unsigned int sleep(unsigned int seconds) +{ + timex_t time = timex_set(seconds, 0); + vtimer_sleep(time); + return 0; +} + /** * @} */