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
7
#include "
user/common/syscalls.h
"
8
#include "
user/common/thread.h
"
9
10
int
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
{
16
clock_t
start
=
uptime
();
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
}
CLOCKS_PER_SEC
#define CLOCKS_PER_SEC
Definition
clock_t.h:15
uptime
clock_t uptime(void)
System call for retreving the time since boot.
Definition
uptime.c:6
nanosleep
uint64_t nanosleep(clock_t timeout)
System call for sleeping.
Definition
nanosleep.c:6
NULL
#define NULL
Pointer error value.
Definition
NULL.h:23
clock_t
__UINT64_TYPE__ clock_t
A nanosecond time.
Definition
clock_t.h:13
proc.h
start
static void start()
Definition
main.c:542
syscalls.h
thread.h
stdatomic.h
stdbool.h
uint64_t
__UINT64_TYPE__ uint64_t
Definition
stdint.h:17
stdio.h
timespec
Definition
timespec.h:7
timespec::tv_nsec
long tv_nsec
Definition
timespec.h:9
timespec::tv_sec
time_t tv_sec
Definition
timespec.h:8
thrd_sleep
int thrd_sleep(const struct timespec *duration, struct timespec *remaining)
Definition
thrd_sleep.c:10
threads.h
src
libstd
user
functions
thread
thrd_sleep.c
Generated by
1.9.8