kernel: POSIX: Compatibility layer for POSIX sleep APIs.

This patch provides POSIX sleep APIs for POSIX 1003.1 PSE52 standard.
sleep(n) is implemented using Zephyr k_sleep API.
uleep(n) is implemented using Zephyr k_sleep/k_busy_Wait API.

Signed-off-by: Youvedeep Singh <youvedeep.singh@intel.com>
This commit is contained in:
Youvedeep Singh 2018-01-13 21:54:23 +05:30 committed by Anas Nashif
commit 1f2e126d4d
4 changed files with 56 additions and 0 deletions

View file

@ -95,6 +95,10 @@
#define sched_get_priority_min(...) zap_sched_get_priority_min(__VA_ARGS__) #define sched_get_priority_min(...) zap_sched_get_priority_min(__VA_ARGS__)
#define sched_get_priority_max(...) zap_sched_get_priority_max(__VA_ARGS__) #define sched_get_priority_max(...) zap_sched_get_priority_max(__VA_ARGS__)
/* Sleep */
#define sleep(...) zap_sleep(__VA_ARGS__)
#define usleep(...) zap_usleep(__VA_ARGS__)
#endif /* CONFIG_ARCH_POSIX */ #endif /* CONFIG_ARCH_POSIX */
#endif #endif

View file

@ -22,6 +22,7 @@ struct timespec {
#include "sys/types.h" #include "sys/types.h"
#include "posix_sched.h" #include "posix_sched.h"
#include "unistd.h"
enum pthread_state { enum pthread_state {
/* The thread is running and joinable. */ /* The thread is running and joinable. */

View file

@ -59,6 +59,9 @@ typedef struct pthread_barrierattr {
int unused; int unused;
} pthread_barrierattr_t; } pthread_barrierattr_t;
/* time related attributes */
typedef unsigned long useconds_t;
#endif /* CONFIG_PTHREAD_IPC */ #endif /* CONFIG_PTHREAD_IPC */
#ifdef __cplusplus #ifdef __cplusplus

48
include/posix/unistd.h Normal file
View file

@ -0,0 +1,48 @@
/*
* Copyright (c) 2018 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef __POSIX_UNISTD_H__
#define __POSIX_UNISTD_H__
#ifdef __cplusplus
extern "C" {
#endif
#include_next <unistd.h>
#ifdef CONFIG_PTHREAD_IPC
#include "sys/types.h"
/**
* @brief Sleep for a specified number of seconds.
*
* See IEEE 1003.1
*/
static inline int sleep(unsigned int seconds)
{
k_sleep(K_SECONDS(seconds));
return 0;
}
/**
* @brief Suspend execution for microsecond intervals.
*
* See IEEE 1003.1
*/
static inline int usleep(useconds_t useconds)
{
if (useconds < USEC_PER_MSEC) {
k_busy_wait(useconds);
} else {
k_sleep(useconds / USEC_PER_MSEC);
}
return 0;
}
#endif
#ifdef __cplusplus
}
#endif
#endif /* __POSIX_UNISTD_H__ */