Debian Codegraph @

time/+netbsd/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, c: clock = clock::MONOTONIC) void = {
42 let req = duration_to_timespec(d);
43
44 for (true) {
45 let res = rt::timespec { ... };
46 match (rt::clock_nanosleep(c: int, 0, &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 clock_nanosleep");
55 };
56 };
57 };
58 };
59
60 // Yields the process to the kernel and returns when [[clock]] c reaches [[instant]] i.
61 export fn sleep_until(i: instant, c: clock = clock::MONOTONIC) void = {
62 const requested = instant_to_timespec(i);
63
64 for (true) {
65 match (rt::clock_nanosleep(c: int, rt::TIMER_ABSTIME, &requested, null)) {
66 case void =>
67 return;
68 case let err: rt::errno =>
69 switch (err) {
70 case rt::EINTR =>
71 continue;
72 case =>
73 abort("Unexpected error from clock_nanosleep");
74 };
75 };
76 };
77 };
78
79 // An enumeration of clocks available on this system. Different clocks represent
80 // times from different epochs, and have different characteristics with regards
81 // to leap seconds, NTP adjustments, and so on. All systems provide the REALTIME
82 // and MONOTONIC clocks at least; use of other clocks is not guaranteed to be
83 // portable.
84 export type clock = enum {
85 // The current wall-clock time. This may jump forwards or backwards in
86 // time to account for leap seconds, NTP adjustments, etc.
87 REALTIME = rt::CLOCK_REALTIME,
88
89 // The current monotonic time. This clock measures from some undefined
90 // epoch and is not affected by leap seconds, NTP adjustments, and
91 // changes to the system time: it always increases by one second per
92 // second.
93 MONOTONIC = rt::CLOCK_MONOTONIC,
94
95 // Increments only when the CPU is running in user mode on behalf of the
96 // calling process.
97 VIRTUAL = rt::CLOCK_VIRTUAL,
98
99 // Increments when the CPU is running in user or kernel mode.
100 PROF = rt::CLOCK_PROF,
101
102 // The thread CPU clock. It begins at zero and is advanced while the
103 // calling thread is running in user or kernel mode.
104 THREAD_CPUTIME_ID = rt::CLOCK_THREAD_CPUTIME_ID,
105
106 // The process CPU clock. It begins at zero and is advanced while the
107 // calling process is running in user or kernel mode.
108 PROCESS_CPUTIME_ID = rt::CLOCK_PROCESS_CPUTIME_ID,
109 };
110
111 // Returns the current time for a given clock.
112 export fn now(clock: clock) instant = {
113 let tp = rt::timespec { ... };
114 match (rt::clock_gettime(clock, &tp)) {
115 case void =>
116 return timespec_to_instant(tp);
117 case let err: rt::errno =>
118 abort("Unexpected error from clock_gettime");
119 };
120 };
121
122 // Sets system clock to given time.
123 export fn set(clock: clock, t: instant) (void | errors::noaccess) = {
124 let tp = instant_to_timespec(t);
125 let err = match (rt::clock_settime(clock, &tp)) {
126 case void => return;
127 case let err: rt::errno =>
128 yield err;
129 };
130 if (err == rt::EPERM) {
131 return errors::noaccess;
132 };
133 abort("Unexpected error from clock_settime");
134 };