PatchworkOS  da8a090
A non-POSIX operating system.
Loading...
Searching...
No Matches
clock_t.h
Go to the documentation of this file.
1#ifndef _AUX_CLOCK_T_H
2#define _AUX_CLOCK_T_H 1
3
4/**
5 * @brief A nanosecond time.
6 * @ingroup libstd
7 *
8 * The `clock_t` type is extended in Patchwork to respresent any nanosecond time. The special value `CLOCKS_PER_SEC`
9 * is inherited from the C standard library but Patchwork also defines the special value `CLOCKS_NEVER` that all
10 * functions and system calls that take in a timeout are expected to handle.
11 *
12 */
13typedef __UINT64_TYPE__ clock_t;
14
15#define CLOCKS_PER_SEC ((clock_t)1000000000ULL)
16#define CLOCKS_NEVER ((clock_t)__UINT64_MAX__)
17
18/**
19 * @brief Safely calculate remaining time until deadline.
20 * @ingroup libstd
21 *
22 * Handles `CLOCKS_NEVER` and avoids unsigned integer underflow when deadline has passed.
23 *
24 * @param deadline The deadline timestamp.
25 * @param uptime The current uptime.
26 * @return The remaining time, `0` if deadline passed, or `CLOCKS_NEVER` if deadline is `CLOCKS_NEVER`.
27 */
28#define CLOCKS_REMAINING(deadline, uptime) \
29 ({ \
30 clock_t _deadline = (deadline); \
31 clock_t _uptime = (uptime); \
32 ((_deadline) == CLOCKS_NEVER ? CLOCKS_NEVER : ((_deadline) > (_uptime) ? (_deadline) - (_uptime) : 0)); \
33 })
34
35/**
36 * @brief Safely calculate deadline from timeout.
37 * @ingroup libstd
38 *
39 * Handles `CLOCKS_NEVER` and avoids unsigned integer overflow.
40 *
41 * @param timeout The timeout duration.
42 * @param uptime The current uptime.
43 * @return The deadline timestamp, or `CLOCKS_NEVER` if timeout is `CLOCKS_NEVER` or would overflow.
44 */
45#define CLOCKS_DEADLINE(timeout, uptime) \
46 ({ \
47 clock_t _timeout = (timeout); \
48 clock_t _uptime = (uptime); \
49 ((_timeout) == CLOCKS_NEVER \
50 ? CLOCKS_NEVER \
51 : ((_timeout) > CLOCKS_NEVER - (_uptime) ? CLOCKS_NEVER : (_uptime) + (_timeout))); \
52 })
53
54#endif
__UINT64_TYPE__ clock_t
A nanosecond time.
Definition clock_t.h:13