Skip to content

Commit 1f2e126

Browse files
youvedeep-singhAnas Nashif
authored andcommitted
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 <[email protected]>
1 parent 7eabf10 commit 1f2e126

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

arch/posix/include/posix_cheats.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@
9595
#define sched_get_priority_min(...) zap_sched_get_priority_min(__VA_ARGS__)
9696
#define sched_get_priority_max(...) zap_sched_get_priority_max(__VA_ARGS__)
9797

98+
/* Sleep */
99+
#define sleep(...) zap_sleep(__VA_ARGS__)
100+
#define usleep(...) zap_usleep(__VA_ARGS__)
101+
98102
#endif /* CONFIG_ARCH_POSIX */
99103

100104
#endif

include/posix/pthread.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ struct timespec {
2222

2323
#include "sys/types.h"
2424
#include "posix_sched.h"
25+
#include "unistd.h"
2526

2627
enum pthread_state {
2728
/* The thread is running and joinable. */

include/posix/sys/types.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ typedef struct pthread_barrierattr {
5959
int unused;
6060
} pthread_barrierattr_t;
6161

62+
/* time related attributes */
63+
typedef unsigned long useconds_t;
64+
6265
#endif /* CONFIG_PTHREAD_IPC */
6366

6467
#ifdef __cplusplus

include/posix/unistd.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (c) 2018 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
#ifndef __POSIX_UNISTD_H__
7+
#define __POSIX_UNISTD_H__
8+
9+
#ifdef __cplusplus
10+
extern "C" {
11+
#endif
12+
13+
#include_next <unistd.h>
14+
15+
#ifdef CONFIG_PTHREAD_IPC
16+
#include "sys/types.h"
17+
18+
/**
19+
* @brief Sleep for a specified number of seconds.
20+
*
21+
* See IEEE 1003.1
22+
*/
23+
static inline int sleep(unsigned int seconds)
24+
{
25+
k_sleep(K_SECONDS(seconds));
26+
return 0;
27+
}
28+
/**
29+
* @brief Suspend execution for microsecond intervals.
30+
*
31+
* See IEEE 1003.1
32+
*/
33+
static inline int usleep(useconds_t useconds)
34+
{
35+
if (useconds < USEC_PER_MSEC) {
36+
k_busy_wait(useconds);
37+
} else {
38+
k_sleep(useconds / USEC_PER_MSEC);
39+
}
40+
41+
return 0;
42+
}
43+
44+
#endif
45+
#ifdef __cplusplus
46+
}
47+
#endif
48+
#endif /* __POSIX_UNISTD_H__ */

0 commit comments

Comments
 (0)