Line data Source code
1 : /*
2 : MIT License
3 :
4 : Copyright (c) 2026 - A bunch of nerds
5 :
6 : Permission is hereby granted, free of charge, to any person obtaining a copy
7 : of this software and associated documentation files (the "Software"), to deal
8 : in the Software without restriction, including without limitation the rights
9 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 : copies of the Software, and to permit persons to whom the Software is
11 : furnished to do so, subject to the following conditions:
12 :
13 : The above copyright notice and this permission notice shall be included in all
14 : copies or substantial portions of the Software.
15 :
16 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 : SOFTWARE.
23 : */
24 :
25 : #pragma once
26 :
27 : // Internal only. Turns a public timeout (a uint64_t count of microseconds)
28 : // into an absolute CLOCK_MONOTONIC deadline, or into the coarser units that
29 : // system calls take. Every module goes through these helpers so they all
30 : // saturate the same way: A value too large to represent becomes the largest
31 : // deadline we can represent, and never wraps into the past, which would turn a
32 : // very long wait into one that ends at once. All the arithmetic is uint64_t, so
33 : // ILP32 and LP64 give the same results. Everything is static inline and the
34 : // header is never installed.
35 :
36 : #ifndef CCOL_CDEADLINE_H
37 : #define CCOL_CDEADLINE_H
38 :
39 : #include <limits.h>
40 : // C23 has bool, true and false as keywords.
41 : #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 202311L
42 : #include <stdbool.h>
43 : #endif
44 : #include <stdint.h>
45 : #include <time.h>
46 :
47 : // The largest value of time_t, which the C library has no macro for and the
48 : // saturation of ccol_timespec_add_us() needs. The width comes from sizeof, so
49 : // it's right for a 4-byte and for an 8-byte time_t.
50 : #define CCOL_DEADLINE_TIME_T_LIMIT \
51 : ((time_t)(~(uintmax_t)0 >> \
52 : (sizeof(uintmax_t) * CHAR_BIT - sizeof(time_t) * CHAR_BIT + 1)))
53 :
54 : // Adds us microseconds to *ts; ccol_deadline_after_us() and the modules that
55 : // extend a deadline use it. *ts must hold a tv_nsec in [0, 999999999] and a
56 : // tv_sec that isn't negative (what clock_gettime() gives). A sum past the
57 : // largest time_t saturates at the largest time we can represent.
58 19404 : static inline void ccol_timespec_add_us(struct timespec *ts, uint64_t us) {
59 19406 : uint64_t secs = us / UINT64_C(1000000);
60 19406 : long nsec = (long)(us % UINT64_C(1000000)) * 1000L;
61 19406 : ts->tv_nsec += nsec;
62 19404 : if (ts->tv_nsec >= 1000000000L) {
63 900 : ts->tv_nsec -= 1000000000L;
64 898 : secs++;
65 : }
66 38810 : uint64_t headroom = ts->tv_sec >= 0
67 19404 : ? (uint64_t)(CCOL_DEADLINE_TIME_T_LIMIT - ts->tv_sec)
68 19404 : : (uint64_t)CCOL_DEADLINE_TIME_T_LIMIT;
69 19404 : if (secs > headroom) {
70 1 : ts->tv_sec = CCOL_DEADLINE_TIME_T_LIMIT;
71 0 : ts->tv_nsec = 999999999L;
72 : } else {
73 19404 : ts->tv_sec = (time_t)((uint64_t)ts->tv_sec + secs);
74 : }
75 19404 : }
76 :
77 : // Sets *abs to now (CLOCK_MONOTONIC) plus us microseconds, saturating like
78 : // ccol_timespec_add_us(). Every timed wait of the library computes its
79 : // deadline here. Gives false only when clock_gettime() fails.
80 19395 : static inline bool ccol_deadline_after_us(uint64_t us, struct timespec *abs) {
81 19395 : if (clock_gettime(CLOCK_MONOTONIC, abs) != 0) return false;
82 19395 : ccol_timespec_add_us(abs, us);
83 19395 : return true;
84 : }
85 :
86 : // Microseconds to whole milliseconds, rounded up, for calls that take
87 : // milliseconds, so a wait of the result never ends early and a positive input
88 : // never becomes 0.
89 1933 : static inline uint64_t ccol_us_to_ms_ceil(uint64_t us) {
90 1285 : return us / UINT64_C(1000) + (us % UINT64_C(1000) != 0);
91 : }
92 :
93 : // Microseconds to whole seconds, rounded up like ccol_us_to_ms_ceil(), for
94 : // settings that count whole seconds.
95 : static inline uint64_t ccol_us_to_s_ceil(uint64_t us) {
96 : return us / UINT64_C(1000000) + (us % UINT64_C(1000000) != 0);
97 : }
98 :
99 : // Gives the time left until *deadline in milliseconds, rounded up and capped
100 : // at INT_MAX, for poll(2) or epoll_wait(2). A deadline that has passed gives 0,
101 : // so the caller still polls once without blocking instead of reporting a
102 : // timeout without a look.
103 4469 : static inline int ccol_deadline_remaining_ms_ceil(
104 : const struct timespec *deadline) {
105 4469 : struct timespec now;
106 4469 : clock_gettime(CLOCK_MONOTONIC, &now);
107 4473 : if (deadline->tv_sec < now.tv_sec ||
108 71 : (deadline->tv_sec == now.tv_sec && deadline->tv_nsec <= now.tv_nsec))
109 : return 0;
110 4448 : uint64_t secs = (uint64_t)deadline->tv_sec - (uint64_t)now.tv_sec;
111 4448 : if (secs > (uint64_t)INT_MAX / 1000u) return INT_MAX;
112 4437 : int64_t ns = (int64_t)secs * 1000000000LL +
113 4437 : ((int64_t)deadline->tv_nsec - (int64_t)now.tv_nsec);
114 4437 : int64_t ms = (ns + 999999LL) / 1000000LL;
115 4437 : return ms > INT_MAX ? INT_MAX : (int)ms;
116 : }
117 :
118 : #endif
|