]>
Commit | Line | Data |
---|---|---|
320054e8 DG |
1 | // Copyright (c) 2015-2016 Nuxi, https://nuxi.nl/ |
2 | // | |
3 | // SPDX-License-Identifier: BSD-2-Clause | |
4 | ||
5 | #include <common/clock.h> | |
6 | #include <common/time.h> | |
7 | ||
8 | #include <assert.h> | |
446cb3f1 | 9 | #include <wasi/api.h> |
320054e8 DG |
10 | #include <errno.h> |
11 | #include <time.h> | |
12 | ||
446cb3f1 | 13 | static_assert(TIMER_ABSTIME == __WASI_SUBCLOCKFLAGS_SUBSCRIPTION_CLOCK_ABSTIME, |
320054e8 DG |
14 | "Value mismatch"); |
15 | ||
320054e8 DG |
16 | int clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *rqtp, |
17 | struct timespec *rmtp) { | |
320054e8 DG |
18 | if ((flags & ~TIMER_ABSTIME) != 0) |
19 | return EINVAL; | |
20 | ||
21 | // Prepare polling subscription. | |
22 | __wasi_subscription_t sub = { | |
c2ae180d PH |
23 | .u.tag = __WASI_EVENTTYPE_CLOCK, |
24 | .u.u.clock.id = clock_id->id, | |
25 | .u.u.clock.flags = flags, | |
320054e8 | 26 | }; |
c2ae180d | 27 | if (!timespec_to_timestamp_clamp(rqtp, &sub.u.u.clock.timeout)) |
320054e8 DG |
28 | return EINVAL; |
29 | ||
30 | // Block until polling event is triggered. | |
31 | size_t nevents; | |
32 | __wasi_event_t ev; | |
320054e8 | 33 | __wasi_errno_t error = __wasi_poll_oneoff(&sub, &ev, 1, &nevents); |
320054e8 DG |
34 | return error == 0 && ev.error == 0 ? 0 : ENOTSUP; |
35 | } | |
dcd28cf8 AB |
36 | |
37 | weak_alias(clock_nanosleep, __clock_nanosleep); |