diff mbox series

[PATCHv2] os_sleep: Use nanosleep for POSIX versions 2008 and higher

Message ID 20190824220116.27624-1-rosenp@gmail.com
State Accepted
Headers show
Series [PATCHv2] os_sleep: Use nanosleep for POSIX versions 2008 and higher | expand

Commit Message

Rosen Penev Aug. 24, 2019, 10:01 p.m. UTC
uClibc-ng optionally disabled deprecated POSIX functions like usleep,
causing compilation failures. This switches to nanosleep while retaining
support for older libcs that do not support nanosleep.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
 v2: Changed approach to simplify the function.
 src/utils/os_internal.c | 5 +++++
 src/utils/os_unix.c     | 5 +++++
 2 files changed, 10 insertions(+)

Comments

Jouni Malinen Sept. 9, 2019, 3:35 p.m. UTC | #1
On Sat, Aug 24, 2019 at 03:01:16PM -0700, Rosen Penev wrote:
> uClibc-ng optionally disabled deprecated POSIX functions like usleep,
> causing compilation failures. This switches to nanosleep while retaining
> support for older libcs that do not support nanosleep.

Thanks, applied.
diff mbox series

Patch

diff --git a/src/utils/os_internal.c b/src/utils/os_internal.c
index 474c8a372..2c90f51c2 100644
--- a/src/utils/os_internal.c
+++ b/src/utils/os_internal.c
@@ -25,10 +25,15 @@ 
 
 void os_sleep(os_time_t sec, os_time_t usec)
 {
+#if defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200809L)
+	const struct timespec req = {sec, usec * 1000};
+	nanosleep (&req, NULL);
+#else
 	if (sec)
 		sleep(sec);
 	if (usec)
 		usleep(usec);
+#endif
 }
 
 
diff --git a/src/utils/os_unix.c b/src/utils/os_unix.c
index 800c50772..21fb8b3be 100644
--- a/src/utils/os_unix.c
+++ b/src/utils/os_unix.c
@@ -49,10 +49,15 @@  struct os_alloc_trace {
 
 void os_sleep(os_time_t sec, os_time_t usec)
 {
+#if defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200809L)
+	const struct timespec req = {sec, usec * 1000};
+	nanosleep (&req, NULL);
+#else
 	if (sec)
 		sleep(sec);
 	if (usec)
 		usleep(usec);
+#endif
 }