Debian Codegraph @

time/+freebsd/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 = 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 = 4,
94
95 // TODO: Document these
96 VIRTUAL = 1,
97 PROF = 2,
98 UPTIME = 5,
99 UPTIME_PRECISE = 7,
100 UPTIME_FAST = 8,
101 REALTIME_PRECISE = 9,
102 REALTIME_FAST = 10,
103 MONOTONIC_PRECISE = 11,
104 MONOTONIC_FAST = 12,
105 SECOND = 13,
106 THREAD_CPUTIME_ID = 14,
107 PROCESS_CPUTIME_ID = 15,
108 };
109
110 // Returns the current time for a given clock.
111 export fn now(clock: clock) instant = {
112 let tp = rt::timespec { ... };
113 match (rt::clock_gettime(clock, &tp)) {
114 case void =>
115 return timespec_to_instant(tp);
116 case let err: rt::errno =>
117 abort("Unexpected error from clock_gettime");
118 };
119 };
120
121 // Sets system clock to given time.
122 export fn set(clock: clock, t: instant) (void | errors::noaccess) = {
123 let tp = instant_to_timespec(t);
124 let err = match (rt::clock_settime(clock, &tp)) {
125 case void => return;
126 case let err: rt::errno =>
127 yield err;
128 };
129 if (err == rt::EPERM) {
130 return errors::noaccess;
131 };
132 abort("Unexpected error from clock_settime");
133 };