mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-04 22:58:46 +00:00
time: wasi implementation
This commit is contained in:
@@ -10,9 +10,9 @@ filesize_t :: distinct u64
|
|||||||
timestamp_t :: distinct u64
|
timestamp_t :: distinct u64
|
||||||
|
|
||||||
clockid_t :: distinct u32
|
clockid_t :: distinct u32
|
||||||
CLOCK_MONOTONIC :: clockid_t(0)
|
CLOCK_REALTIME :: clockid_t(0)
|
||||||
CLOCK_PROCESS_CPUTIME_ID :: clockid_t(1)
|
CLOCK_MONOTONIC :: clockid_t(1)
|
||||||
CLOCK_REALTIME :: clockid_t(2)
|
CLOCK_PROCESS_CPUTIME_ID :: clockid_t(2)
|
||||||
CLOCK_THREAD_CPUTIME_ID :: clockid_t(3)
|
CLOCK_THREAD_CPUTIME_ID :: clockid_t(3)
|
||||||
|
|
||||||
errno_t :: enum u16 {
|
errno_t :: enum u16 {
|
||||||
@@ -715,7 +715,7 @@ subscription_t :: struct {
|
|||||||
* The type of the event to which to subscribe, and its contents
|
* The type of the event to which to subscribe, and its contents
|
||||||
*/
|
*/
|
||||||
using contents: struct {
|
using contents: struct {
|
||||||
tag: u8,
|
tag: eventtype_t,
|
||||||
using u: struct #raw_union {
|
using u: struct #raw_union {
|
||||||
clock: subscription_clock_t,
|
clock: subscription_clock_t,
|
||||||
fd_read: subscription_fd_readwrite_t,
|
fd_read: subscription_fd_readwrite_t,
|
||||||
|
|||||||
@@ -2,23 +2,53 @@
|
|||||||
//+build wasi
|
//+build wasi
|
||||||
package time
|
package time
|
||||||
|
|
||||||
_IS_SUPPORTED :: false
|
import "base:intrinsics"
|
||||||
|
|
||||||
|
import "core:sys/wasm/wasi"
|
||||||
|
|
||||||
|
_IS_SUPPORTED :: true
|
||||||
|
|
||||||
_now :: proc "contextless" () -> Time {
|
_now :: proc "contextless" () -> Time {
|
||||||
return {}
|
ts, err := wasi.clock_time_get(wasi.CLOCK_REALTIME, 0)
|
||||||
|
when !ODIN_DISABLE_ASSERT {
|
||||||
|
if err != nil {
|
||||||
|
intrinsics.trap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Time{_nsec=i64(ts)}
|
||||||
}
|
}
|
||||||
|
|
||||||
_sleep :: proc "contextless" (d: Duration) {
|
_sleep :: proc "contextless" (d: Duration) {
|
||||||
|
ev: wasi.event_t
|
||||||
|
n, err := wasi.poll_oneoff(
|
||||||
|
&{
|
||||||
|
tag = .CLOCK,
|
||||||
|
clock = {
|
||||||
|
id = wasi.CLOCK_MONOTONIC,
|
||||||
|
timeout = wasi.timestamp_t(d),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
&ev,
|
||||||
|
1,
|
||||||
|
)
|
||||||
|
|
||||||
|
when !ODIN_DISABLE_ASSERT {
|
||||||
|
if err != nil || n != 1 || ev.error != nil || ev.type != .CLOCK {
|
||||||
|
intrinsics.trap()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_tick_now :: proc "contextless" () -> Tick {
|
_tick_now :: proc "contextless" () -> Tick {
|
||||||
// mul_div_u64 :: proc "contextless" (val, num, den: i64) -> i64 {
|
ts, err := wasi.clock_time_get(wasi.CLOCK_MONOTONIC, 0)
|
||||||
// q := val / den
|
when !ODIN_DISABLE_ASSERT {
|
||||||
// r := val % den
|
if err != nil {
|
||||||
// return q * num + r * num / den
|
intrinsics.trap()
|
||||||
// }
|
}
|
||||||
return {}
|
}
|
||||||
|
return Tick{_nsec=i64(ts)}
|
||||||
}
|
}
|
||||||
|
|
||||||
_yield :: proc "contextless" () {
|
_yield :: proc "contextless" () {
|
||||||
|
wasi.sched_yield()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user