PatchworkOS  19e446b
A non-POSIX operating system.
Loading...
Searching...
No Matches
clock_t.h
Go to the documentation of this file.
1#ifndef _INTERNAL_CLOCK_T_H
2#define _INTERNAL_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_PER_MS ((clock_t)1000000ULL)
17#define CLOCKS_PER_US ((clock_t)1000ULL)
18#define CLOCKS_NEVER ((clock_t)__UINT64_MAX__)
19
20/**
21 * @brief Safely calculate remaining time until deadline.
22 * @ingroup libstd
23 *
24 * Handles `CLOCKS_NEVER` and avoids unsigned integer underflow when deadline has passed.
25 *
26 * @param deadline The deadline timestamp.
27 * @param uptime The current uptime.
28 * @return The remaining time, `0` if deadline passed, or `CLOCKS_NEVER` if deadline is `CLOCKS_NEVER`.
29 */
30#define CLOCKS_REMAINING(deadline, uptime) \
31 ({ \
32 clock_t _deadline = (deadline); \
33 clock_t _uptime = (uptime); \
34 ((_deadline) == CLOCKS_NEVER ? CLOCKS_NEVER : ((_deadline) > (_uptime) ? (_deadline) - (_uptime) : 0)); \
35 })
36
37/**
38 * @brief Safely calculate deadline from timeout.
39 * @ingroup libstd
40 *
41 * Handles `CLOCKS_NEVER` and avoids unsigned integer overflow.
42 *
43 * @param timeout The timeout duration.
44 * @param uptime The current uptime.
45 * @return The deadline timestamp, or `CLOCKS_NEVER` if timeout is `CLOCKS_NEVER` or would overflow.
46 */
47#define CLOCKS_DEADLINE(timeout, uptime) \
48 ({ \
49 clock_t _timeout = (timeout); \
50 clock_t _uptime = (uptime); \
51 ((_timeout) == CLOCKS_NEVER \
52 ? CLOCKS_NEVER \
53 : ((_timeout) > CLOCKS_NEVER - (_uptime) ? CLOCKS_NEVER : (_uptime) + (_timeout))); \
54 })
55
56#endif
__UINT64_TYPE__ clock_t
A nanosecond time.
Definition clock_t.h:13