mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
Merge pull request #839 from mehlis/posix-sleep
posix: added sleep and usleep
This commit is contained in:
commit
1cc5fa57c1
@ -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)))
|
||||
|
@ -1,3 +1,3 @@
|
||||
MODULE =posix
|
||||
MODULE = posix
|
||||
|
||||
include $(RIOTBASE)/Makefile.base
|
||||
|
@ -24,6 +24,11 @@
|
||||
#ifndef _UNISTD_H
|
||||
#define _UNISTD_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#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 <a href="http://pubs.opengroup.org/onlinepubs/7908799/xsh/usleep.html">
|
||||
* The Open Group Base Specification Issue 2, usleep
|
||||
* </a>
|
||||
*
|
||||
* @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 <a href="http://pubs.opengroup.org/onlinepubs/009695399/functions/sleep.html">
|
||||
* The Open Group Base Specification Issue 6, sleep
|
||||
* </a>
|
||||
*
|
||||
* @param seconds time to sleep in seconds
|
||||
* @return 0 on success
|
||||
*/
|
||||
unsigned int sleep(unsigned int seconds);
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
@ -11,6 +11,7 @@
|
||||
* @file fd.c
|
||||
* @brief Providing implementation for close for fds defined in fd.h.
|
||||
* @author Martin Lenders <mlenders@inf.fu-berlin.de>
|
||||
* @author Christian Mehlis <mehlis@inf.fu-berlin.de>
|
||||
*/
|
||||
#include <errno.h>
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
6
tests/test_posix_sleep/Makefile
Normal file
6
tests/test_posix_sleep/Makefile
Normal file
@ -0,0 +1,6 @@
|
||||
export PROJECT = test_posix_sleep
|
||||
include ../Makefile.tests_common
|
||||
|
||||
USEMODULE += posix
|
||||
|
||||
include $(RIOTBASE)/Makefile.include
|
43
tests/test_posix_sleep/main.c
Normal file
43
tests/test_posix_sleep/main.c
Normal file
@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright (C) 2014 Freie Universität Berlin
|
||||
*
|
||||
* This file is subject to the terms and conditions of the GNU Lesser General
|
||||
* Public License. See the file LICENSE in the top level directory for more
|
||||
* details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @ingroup tests
|
||||
* @{
|
||||
*
|
||||
* @file
|
||||
* @brief Posix sleep test application
|
||||
*
|
||||
* @author Christian Mehlis <mehlis@inf.fu-berlin.de>
|
||||
*
|
||||
* @}
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
puts("usleep 1 x 1000*1000");
|
||||
for (int i = 0; i < 10; i++) {
|
||||
useconds_t us = i*1000*1000;
|
||||
printf("calling usleep(%u)\n", (unsigned int) us);
|
||||
usleep(us);
|
||||
puts("wake up");
|
||||
}
|
||||
|
||||
puts("sleep 1");
|
||||
for (int i = 0; i < 10; i++) {
|
||||
unsigned int s = i;
|
||||
printf("calling sleep(%d)\n", s);
|
||||
sleep(s);
|
||||
puts("wake up");
|
||||
}
|
||||
|
||||
puts("done");
|
||||
}
|
Loading…
Reference in New Issue
Block a user