Debian Codegraph @

time/+linux/functions.ha

raw
1 // SPDX-License-Identifier: MPL-2.0
2 // (c) Hare authors <https://harelang.org>
3
4 use errors;
5 use linux::vdso;
6 use rt;
7
8 // Converts a [[duration]] to an [[rt::timespec]]. This function is
9 // non-portable.
10 export fn duration_to_timespec(d: duration) rt::timespec = rt::timespec {
11 tv_sec = d / SECOND,
12 tv_nsec = d % SECOND,
13 };
14
15 // Converts a [[rt::timespec]] to a [[duration]]. This function is non-portable.
16 export fn timespec_to_duration(spec: rt::timespec) duration = {
17 return (spec.tv_sec: i64 * SECOND) + spec.tv_nsec: i64;
18 };
19
20 // Converts a [[duration]] to an [[rt::timeval]]. This function is
21 // non-portable.
22 export fn duration_to_timeval(d: duration) rt::timeval = rt::timeval {
23 tv_sec = d / SECOND,
24 tv_usec = d % SECOND / 1000,
25 };
26
27 // Converts an [[instant]] to an [[rt::timespec]]. This function is
28 // non-portable.
29 export fn instant_to_timespec(t: instant) rt::timespec = rt::timespec {
30 tv_sec = t.sec,
31 tv_nsec = t.nsec,
32 };
33
34 // Converts a [[rt::timespec]] to an [[instant]]. This function is 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 = 0,
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 = 1,
94
95 // Measures CPU time consumed by the calling process.
96 PROCESS_CPU = 2,
97
98 // Time since the system was booted. Increases monotonically and, unlike
99 // [[MONOTONIC]], continues to tick while the system is suspended.
100 BOOT = 7,
101
102 // This clock is like [[REALTIME]], but will wake the system if it is suspended.
103 REALTIME_ALARM = 8,
104 // This clock is like [[BOOT]], but will wake the system if it is suspended.
105 BOOT_ALARM = 9,
106
107 // A system-wide clock derived from wall-clock time but ignoring leap seconds.
108 TAI = 11,
109 };
110
111 fn cgt_vdso() nullable *fn(_: int, _: *rt::timespec) int = {
112 static let vdso_checked: bool = false;
113 static let cgt_vdso: nullable *fn(_: int, _: *rt::timespec) int = null;
114 if (vdso_checked) {
115 return cgt_vdso;
116 };
117 vdso_checked = true;
118 cgt_vdso = vdso::getsym(VDSO_CGT_SYM, VDSO_CGT_VER):
119 nullable *fn(_: int, _: *rt::timespec) int;
120 return cgt_vdso;
121 };
122
123 fn now_vdso(clock: clock, tp: *rt::timespec) (void | rt::errno) = {
124 const vfn = match (cgt_vdso()) {
125 case null =>
126 return rt::ENOSYS;
127 case let vfn: *fn(_: int, _: *rt::timespec) int =>
128 yield vfn;
129 };
130 const ret = vfn(clock, tp);
131 if (ret == 0) {
132 return;
133 };
134 return ret;
135 };
136
137 // Returns the current time for a given clock.
138 export fn now(clock: clock) instant = {
139 let tp = rt::timespec { ... };
140 let err = match (now_vdso(clock, &tp)) {
141 case void =>
142 return timespec_to_instant(tp);
143 case let err: rt::errno =>
144 yield err;
145 };
146 if (err != rt::ENOSYS) {
147 abort("Unexpected error from clock_gettime");
148 };
149 match (rt::clock_gettime(clock, &tp)) {
150 case void =>
151 return timespec_to_instant(tp);
152 case let err: rt::errno =>
153 abort("Unexpected error from clock_gettime");
154 };
155 };
156
157 // Sets system clock to given time.
158 export fn set(clock: clock, t: instant) (void | errors::noaccess) = {
159 let tp = instant_to_timespec(t);
160 let err = match (rt::clock_settime(clock, &tp)) {
161 case void => return;
162 case let err: rt::errno =>
163 yield err;
164 };
165 if (err == rt::EPERM) {
166 return errors::noaccess;
167 };
168 abort("Unexpected error from clock_settime");
169 };