PatchworkOS
Loading...
Searching...
No Matches
thrd_sleep.c
Go to the documentation of this file.
1#include <stdatomic.h>
2#include <stdbool.h>
3#include <stdio.h>
4#include <sys/proc.h>
5#include <threads.h>
6
9
10int thrd_sleep(const struct timespec* duration, struct timespec* remaining)
11{
12 uint64_t nanoseconds = (uint64_t)duration->tv_sec * CLOCKS_PER_SEC + (uint64_t)duration->tv_nsec;
13
14 if (remaining != NULL)
15 {
17 nanosleep(nanoseconds);
18 clock_t end = uptime();
19
20 clock_t timeLeft = nanoseconds - (end - start);
21 remaining->tv_sec = timeLeft / CLOCKS_PER_SEC;
22 remaining->tv_nsec = timeLeft % CLOCKS_PER_SEC;
23 }
24 else
25 {
26 nanosleep(nanoseconds);
27 }
28
29 return 0;
30}
#define CLOCKS_PER_SEC
Definition clock_t.h:15
clock_t uptime(void)
System call for retreving the time since boot.
Definition uptime.c:6
uint64_t nanosleep(clock_t timeout)
System call for sleeping.
Definition nanosleep.c:6
#define NULL
Pointer error value.
Definition NULL.h:23
__UINT64_TYPE__ clock_t
A nanosecond time.
Definition clock_t.h:13
static void start()
Definition main.c:542
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
long tv_nsec
Definition timespec.h:9
time_t tv_sec
Definition timespec.h:8
int thrd_sleep(const struct timespec *duration, struct timespec *remaining)
Definition thrd_sleep.c:10