Skip to content

Commit 71facb3

Browse files
committed
use nanosleep rather than usleep
1 parent f9bf304 commit 71facb3

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

src/stdlib_system_subprocess.c

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <sys/types.h>
22
#include <stdio.h>
3+
#include <stdlib.h>
34
#include <stdbool.h>
45
#include <stdint.h>
56
#include <string.h>
@@ -9,6 +10,8 @@
910
#else
1011
#include <sys/wait.h>
1112
#include <unistd.h>
13+
#include <time.h>
14+
#include <errno.h>
1215
#endif // _WIN32
1316

1417
// Typedefs
@@ -248,7 +251,34 @@ void process_wait(float seconds)
248251
Sleep(dwMilliseconds);
249252
#else
250253
int uSeconds = (int) 1.0e6*seconds;
251-
usleep(uSeconds);
254+
255+
struct timespec t;
256+
257+
t.tv_sec = seconds;
258+
t.tv_nsec = seconds * 1000000;
259+
260+
int ierr = nanosleep(&t, NULL);
261+
262+
if (ierr != 0){
263+
switch(errno){
264+
case EINTR:
265+
fprintf(stderr, "nanosleep() interrupted\n");
266+
break;
267+
case EINVAL:
268+
fprintf(stderr, "nanosleep() bad milliseconds value\n");
269+
exit(EINVAL);
270+
case EFAULT:
271+
fprintf(stderr, "nanosleep() bad milliseconds value\n");
272+
exit(EFAULT);
273+
case ENOSYS:
274+
fprintf(stderr, "nanosleep() not supported on this system\n");
275+
exit(ENOSYS);
276+
default:
277+
fprintf(stderr, "nanosleep() error\n");
278+
exit(1);
279+
}
280+
}
281+
252282
#endif // _WIN32
253283
}
254284

0 commit comments

Comments
 (0)