diff options
Diffstat (limited to 'libhw/common_include')
-rw-r--r-- | libhw/common_include/libhw/generic/alarmclock.h | 98 | ||||
-rw-r--r-- | libhw/common_include/libhw/generic/bootclock.h | 22 | ||||
-rw-r--r-- | libhw/common_include/libhw/generic/net.h | 2 |
3 files changed, 99 insertions, 23 deletions
diff --git a/libhw/common_include/libhw/generic/alarmclock.h b/libhw/common_include/libhw/generic/alarmclock.h new file mode 100644 index 0000000..bf0d42f --- /dev/null +++ b/libhw/common_include/libhw/generic/alarmclock.h @@ -0,0 +1,98 @@ +/* libhw/generic/alarmclock.h - Device-independent alarmclock definitions + * + * Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com> + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +#ifndef _LIBHW_GENERIC_ALARMCLOCK_H_ +#define _LIBHW_GENERIC_ALARMCLOCK_H_ + +#include <stdbool.h> /* for bool */ +#include <stdint.h> /* for uint{n}_t and UINT{n}_C */ + +#include <libmisc/private.h> +#include <libmisc/vcall.h> + +/* Useful constants ***********************************************************/ + +#define NS_PER_S UINT64_C(1000000000) /* needs at least log₂(10⁹) ≈ 29.9 bits */ +#define US_PER_S UINT64_C(1000000) /* needs at least log₂(10⁶) ≈ 19.9 bits */ +#define MS_PER_S UINT64_C(1000) /* needs at least log₂(10³) ≈ 9.9 bits */ + +/* Structs ********************************************************************/ + +struct alarmclock_trigger; +struct alarmclock_trigger { + BEGIN_PRIVATE(LIBHW_GENERIC_ALARMCLOCK_H) + void *alarmclock; + struct alarmclock_trigger *prev, *next; + + uint64_t fire_at_ns; + void (*cb)(void *); + void *cb_arg; + END_PRIVATE(LIBHW_GENERIC_ALARMCLOCK_H) +}; + +/* Interface ******************************************************************/ + +struct alarmclock_vtable; + +typedef struct { + struct alarmclock_vtable *vtable; +} implements_alarmclock; + +struct alarmclock_vtable { + /** + * (2⁶⁴-1 nanoseconds is more than 500 years; there is little + * risk of this overflowing) + */ + uint64_t (*get_time_ns)(implements_alarmclock *self); + + /** + * Returns true on error. + * + * Implementations may return an error if fire_at_ns is more + * than UINT32_MAX µs (72 minutes) in the future. + * + * If fire_at_ns is in the past, then it will fire + * immediately. + */ + bool (*add_trigger)(implements_alarmclock *self, struct alarmclock_trigger *trigger, + uint64_t fire_at_ns, + void (*cb)(void *), + void *cb_arg); + + void (*del_trigger)(implements_alarmclock *self, struct alarmclock_trigger *trigger); +}; + +/* Utilities ******************************************************************/ + +void alarmclock_sleep_until_ns(implements_alarmclock *clock, uint64_t abstime_ns); + +static inline void alarmclock_sleep_for_ns(implements_alarmclock *clock, uint64_t delta_ns) { + alarmclock_sleep_until_ns(clock, VCALL(clock, get_time_ns) + delta_ns); +} + +static inline void alarmclock_sleep_for_us(implements_alarmclock *clock, uint64_t delta_us) { + alarmclock_sleep_for_ns(clock, delta_us * (NS_PER_S/US_PER_S)); +} + +static inline void alarmclock_sleep_for_ms(implements_alarmclock *clock, uint64_t delta_ms) { + alarmclock_sleep_for_ns(clock, delta_ms * (NS_PER_S/MS_PER_S)); +} + +static inline void alarmclock_sleep_for_s(implements_alarmclock *clock, uint64_t delta_s) { + alarmclock_sleep_for_ns(clock, delta_s * NS_PER_S); +} + +/* Globals ********************************************************************/ + +extern const implements_alarmclock *bootclock; + +#define sleep_until_ns(t) alarmclock_sleep_until_ns(bootclock, t) +#define sleep_for_ns(t) alarmclock_sleep_for_ns(bootclock, t) +#define sleep_for_us(t) alarmclock_sleep_for_us(bootclock, t) +#define sleep_for_ms(t) alarmclock_sleep_for_ms(bootclock, t) +#define sleep_for_s(t) alarmclock_sleep_for_s(bootclock, t) + +#endif /* _LIBHW_GENERIC_ALARMCLOCK_H_ */ diff --git a/libhw/common_include/libhw/generic/bootclock.h b/libhw/common_include/libhw/generic/bootclock.h deleted file mode 100644 index bea5338..0000000 --- a/libhw/common_include/libhw/generic/bootclock.h +++ /dev/null @@ -1,22 +0,0 @@ -/* libhw/generic/bootclock.h - Device-independent clock definitions - * - * Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com> - * SPDX-License-Identifier: AGPL-3.0-or-later - */ - -#ifndef _LIBHW_GENERIC_BOOTCLOCK_H_ -#define _LIBHW_GENERIC_BOOTCLOCK_H_ - -#include <stdint.h> /* for uint{n}_t and UINT{n}_C */ - -#define NS_PER_S UINT64_C(1000000000) /* needs at least log₂(10⁹) ≈ 29.9 bits */ -#define US_PER_S UINT64_C(1000000) /* needs at least log₂(10⁶) ≈ 19.9 bits */ -#define MS_PER_S UINT64_C(1000) /* needs at least log₂(10³) ≈ 9.9 bits */ - -/** - * (2⁶⁴-1 nanoseconds is more than 500 years; there is little - * risk of this overflowing) - */ -uint64_t bootclock_get_ns(void); - -#endif /* _LIBHW_GENERIC_BOOTCLOCK_H_ */ diff --git a/libhw/common_include/libhw/generic/net.h b/libhw/common_include/libhw/generic/net.h index af8844f..08b411f 100644 --- a/libhw/common_include/libhw/generic/net.h +++ b/libhw/common_include/libhw/generic/net.h @@ -34,7 +34,7 @@ struct net_eth_addr { /* Streams (e.g. TCP) *********************************************************/ struct net_stream_listener_vtable; -struct net_stream_conn_btable; +struct net_stream_conn_vtable; typedef struct { struct net_stream_listener_vtable *vtable; |