-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsleep.c
55 lines (44 loc) · 1.05 KB
/
sleep.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <stdio.h>
#ifdef _MSC_VER
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#else
#include <time.h>
#include <errno.h>
#endif
#include "sleep.h"
// https://docs.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-sleep
void c_sleep(const int milliseconds)
{
#ifdef _MSC_VER
Sleep(milliseconds);
#else
// https://linux.die.net/man/3/usleep
if (milliseconds <= 0){
fprintf(stderr, "ERROR:sleep: milliseconds must be strictly positive\n");
return;
}
//int ierr = usleep(*milliseconds * 1000);
struct timespec t;
t.tv_sec = milliseconds / 1000;
t.tv_nsec = (milliseconds % 1000) * 1000000;
int ierr = nanosleep(&t, NULL);
if (ierr == 0)
return;
switch(errno){
case EINTR:
fprintf(stderr, "nanosleep() interrupted\n");
break;
case EINVAL:
case EFAULT:
fprintf(stderr, "nanosleep() bad milliseconds value\n");
break;
case ENOSYS:
fprintf(stderr, "nanosleep() not supported on this system\n");
break;
default:
fprintf(stderr, "nanosleep() error\n");
break;
}
#endif
}