Debian Codegraph @

time/+dragonfly/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 // An enumeration of clocks available on this system. Different clocks represent
61 // times from different epochs, and have different characteristics with regards
62 // to leap seconds, NTP adjustments, and so on. All systems provide the REALTIME
63 // and MONOTONIC clocks at least; use of other clocks is not guaranteed to be
64 // portable.
65 export type clock = enum {
66 // The current wall-clock time. This may jump forwards or backwards in
67 // time to account for leap seconds, NTP adjustments, etc.
68 REALTIME = rt::CLOCK_REALTIME,
69
70 // The current monotonic time. This clock measures from some undefined
71 // epoch and is not affected by leap seconds, NTP adjustments, and
72 // changes to the system time: it always increases by one second per
73 // second.
74 MONOTONIC = rt::CLOCK_MONOTONIC,
75
76 // TODO: Document these
77 VIRTUAL = rt::CLOCK_VIRTUAL,
78 PROF = rt::CLOCK_PROF,
79 UPTIME = rt::CLOCK_UPTIME,
80 UPTIME_PRECISE = rt::CLOCK_UPTIME_PRECISE,
81 UPTIME_FAST = rt::CLOCK_UPTIME_FAST,
82 REALTIME_PRECISE = rt::CLOCK_REALTIME_PRECISE,
83 REALTIME_FAST = rt::CLOCK_REALTIME_FAST,
84 MONOTONIC_PRECISE = rt::CLOCK_MONOTONIC_PRECISE,
85 MONOTONIC_FAST = rt::CLOCK_MONOTONIC_FAST,
86 SECOND = rt::CLOCK_SECOND,
87 THREAD_CPUTIME_ID = rt::CLOCK_THREAD_CPUTIME_ID,
88 PROCESS_CPUTIME_ID = rt::CLOCK_PROCESS_CPUTIME_ID,
89 };
90
91 // Returns the current time for a given clock.
92 export fn now(clock: clock) instant = {
93 let tp = rt::timespec { ... };
94 match (rt::clock_gettime(clock, &tp)) {
95 case void =>
96 return timespec_to_instant(tp);
97 case let err: rt::errno =>
98 abort("Unexpected error from clock_gettime");
99 };
100 };
101
102 // Sets system clock to given time.
103 export fn set(clock: clock, t: instant) (void | errors::noaccess) = {
104 let tp = instant_to_timespec(t);
105 let err = match (rt::clock_settime(clock, &tp)) {
106 case void => return;
107 case let err: rt::errno =>
108 yield err;
109 };
110 if (err == rt::EPERM) {
111 return errors::noaccess;
112 };
113 abort("Unexpected error from clock_settime");
114 };