Debian Codegraph @

time/+openbsd/functions.ha

raw
1 // SPDX-License-Identifier: MPL-2.0
2 // (c) Hare authors <https://harelang.org>
3
4 use errors;
5 use rt;
6
7 // Converts a [[duration]] to an [[rt::timespec]]. This function is
8 // non-portable.
9 export fn duration_to_timespec(n: duration) rt::timespec = rt::timespec {
10 tv_sec = n / SECOND,
11 tv_nsec = n % SECOND,
12 };
13
14 // Converts a [[rt::timespec]] to a [[duration]]. This function is non-portable.
15 export fn timespec_to_duration(spec: rt::timespec) duration = {
16 return (spec.tv_sec: i64 * SECOND) + spec.tv_nsec: i64;
17 };
18
19 // Converts a [[duration]] to an [[rt::timeval]]. This function is
20 // non-portable.
21 export fn duration_to_timeval(d: duration) rt::timeval = rt::timeval {
22 tv_sec = d / SECOND,
23 tv_usec = d % SECOND / 1000,
24 };
25
26 // Converts an [[instant]] to an [[rt::timespec]]. This function is
27 // non-portable.
28 export fn instant_to_timespec(t: instant) rt::timespec = rt::timespec {
29 tv_sec = t.sec,
30 tv_nsec = t.nsec,
31 };
32
33 // Converts a [[rt::timespec]] to an [[instant]]. This function is
34 // non-portable.
35 export fn timespec_to_instant(ts: rt::timespec) instant = instant {
36 sec = ts.tv_sec,
37 nsec = ts.tv_nsec,
38 };
39
40 // Yields the process to the kernel and returns after the requested duration.
41 export fn sleep(d: duration) void = {
42 let req = duration_to_timespec(d);
43
44 for (true) {
45 let res = rt::timespec { ... };
46 match (rt::nanosleep(&req, &res)) {
47 case void =>
48 return;
49 case let err: rt::errno =>
50 switch (err) {
51 case rt::EINTR =>
52 req = res;
53 case =>
54 abort("Unexpected error from nanosleep");
55 };
56 };
57 };
58 };
59
60 // Yields the process to the kernel and returns when [[clock]] c reaches
61 // [[instant]] i. WARNING: In openbsd, this function has a flaw, which
62 // occasionally causes it to sleep for much longer than intended. This is
63 // unavoidable because openbsd is missing clock_nanosleep() and TIMER_ABSTIME.
64 // This is a temporary version until this function is implemented in openbsd.
65 export fn sleep_until(i: instant, c: clock = clock::MONOTONIC) void =
66 sleep(diff(now(c), i));
67
68 // An enumeration of clocks available on this system. Different clocks represent
69 // times from different epochs, and have different characteristics with regards
70 // to leap seconds, NTP adjustments, and so on. All systems provide the REALTIME
71 // and MONOTONIC clocks at least; use of other clocks is not guaranteed to be
72 // portable.
73 export type clock = enum {
74 // The current wall-clock time. This may jump forwards or backwards in
75 // time to account for leap seconds, NTP adjustments, etc.
76 REALTIME = rt::CLOCK_REALTIME,
77
78 // The current monotonic time. This clock measures from some undefined
79 // epoch and is not affected by leap seconds, NTP adjustments, and
80 // changes to the system time: it always increases by one second per
81 // second.
82 MONOTONIC = rt::CLOCK_MONOTONIC,
83
84 // The uptime clock. It is the time that has elapsed since the system
85 // booted. Begins at zero.
86 BOOT = rt::CLOCK_BOOTTIME,
87
88 // The runtime clock. It only advances while the system is not suspended
89 // and begins when the system is booted. Begins at zero.
90 UPTIME = rt::CLOCK_UPTIME,
91
92 // The process CPU clock. It begins at zero and is advanced while the
93 // calling process is running in user or kernel mode.
94 PROCESS_CPU = rt::CLOCK_PROCESS_CPUTIME_ID,
95
96 // The thread CPU clock. It begins at zero and is advanced while the
97 // calling thread is running in user or kernel mode.
98 THREAD_CPU = rt::CLOCK_THREAD_CPUTIME_ID,
99 };
100
101 // Returns the current time for a given clock.
102 export fn now(clock: clock) instant = {
103 let ts = rt::timespec { ... };
104 match (rt::clock_gettime(clock, &ts)) {
105 case void =>
106 return timespec_to_instant(ts);
107 case let err: rt::errno =>
108 abort("Unexpected error from clock_gettime");
109 };
110 };
111
112 // Sets system clock to given time.
113 export fn set(clock: clock, t: instant) (void | errors::noaccess) = {
114 let tp = instant_to_timespec(t);
115 let err = match (rt::clock_settime(clock, &tp)) {
116 case void => return;
117 case let err: rt::errno =>
118 yield err;
119 };
120 if (err == rt::EPERM) {
121 return errors::noaccess;
122 };
123 abort("Unexpected error from clock_settime");
124 };