From 4a6fbecab34616a7b16a859f9458a21b339bb6b0 Mon Sep 17 00:00:00 2001 From: "Luke T. Shumaker" Date: Sun, 2 Feb 2025 02:01:30 -0700 Subject: Have main() set `bootclock` --- cmd/sbc_harness/main.c | 3 +++ lib9p/tests/test_server/main.c | 6 ++++++ libhw/host_alarmclock.c | 25 +++++-------------------- libhw/host_include/libhw/host_alarmclock.h | 29 +++++++++++++++++++++++++++++ libhw/rp2040_hwtimer.c | 4 +--- libhw_generic/alarmclock.c | 4 +++- 6 files changed, 47 insertions(+), 24 deletions(-) create mode 100644 libhw/host_include/libhw/host_alarmclock.h diff --git a/cmd/sbc_harness/main.c b/cmd/sbc_harness/main.c index b17725d..4580acc 100644 --- a/cmd/sbc_harness/main.c +++ b/cmd/sbc_harness/main.c @@ -10,7 +10,9 @@ #include /* pico-sdk:hardware_flash: for flash_get_unique_id() */ #include +#include /* so we can set `bootclock` */ #include +#include #include #include #include @@ -131,6 +133,7 @@ COROUTINE init_cr(void *) { } int main() { + bootclock = rp2040_hwtimer(0); stdio_uart_init(); /* char *hdr = "=" * (80-strlen("info : MAIN: ")); */ infof("==================================================================="); diff --git a/lib9p/tests/test_server/main.c b/lib9p/tests/test_server/main.c index f4b21db..b979a4b 100644 --- a/lib9p/tests/test_server/main.c +++ b/lib9p/tests/test_server/main.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -193,6 +194,11 @@ static COROUTINE init_cr(void *) { } int main() { + struct hostclock clock_monotonic = { + .vtable = &hostclock_vtable, + .clock_id = CLOCK_MONOTONIC, + }; + bootclock = &clock_monotonic; coroutine_add("init", init_cr, NULL); coroutine_main(); return 0; diff --git a/libhw/host_alarmclock.c b/libhw/host_alarmclock.c index 5f7e494..bb1821a 100644 --- a/libhw/host_alarmclock.c +++ b/libhw/host_alarmclock.c @@ -1,13 +1,12 @@ /* libhw/host_alarmclock.c - implementation for POSIX hosts * - * Copyright (C) 2024 Luke T. Shumaker + * Copyright (C) 2024-2025 Luke T. Shumaker * SPDX-License-Identifier: AGPL-3.0-or-later */ #include #include #include -#include #include #include @@ -16,17 +15,10 @@ #define IMPLEMENTATION_FOR_LIBHW_GENERIC_ALARMCLOCK_H YES #include -#include "host_util.h" /* for host_sigrt_alloc(), ns_to_host_ns_time() */ - -/* Types **********************************************************************/ +#define IMPLEMENTATION_FOR_LIBHW_HOST_ALARMCLOCK_H YES +#include -struct hostclock { - implements_alarmclock; - bool initialized; - clockid_t clock_id; - timer_t timer_id; - struct alarmclock_trigger *queue; -}; +#include "host_util.h" /* for host_sigrt_alloc(), ns_to_host_ns_time() */ /* Globals ********************************************************************/ @@ -39,19 +31,12 @@ static bool hostclock_add_trigger(implements_alarmclock *self, static void hostclock_del_trigger(implements_alarmclock *self, struct alarmclock_trigger *trigger); -static struct alarmclock_vtable hostclock_vtable = { +struct alarmclock_vtable hostclock_vtable = { .get_time_ns = hostclock_get_time_ns, .add_trigger = hostclock_add_trigger, .del_trigger = hostclock_del_trigger, }; -static struct hostclock clock_monotonic = { - .vtable = &hostclock_vtable, - .clock_id = CLOCK_MONOTONIC, -}; - -implements_alarmclock *bootclock = &clock_monotonic; - /* Main implementation ********************************************************/ static uint64_t hostclock_get_time_ns(implements_alarmclock *_alarmclock) { diff --git a/libhw/host_include/libhw/host_alarmclock.h b/libhw/host_include/libhw/host_alarmclock.h new file mode 100644 index 0000000..163fb9f --- /dev/null +++ b/libhw/host_include/libhw/host_alarmclock.h @@ -0,0 +1,29 @@ +/* libhw/host_alarmclock.h - implementation for hosted glibc + * + * Copyright (C) 2024-2025 Luke T. Shumaker + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +#ifndef _LIBHW_HOST_ALARMCLOCK_H_ +#define _LIBHW_HOST_ALARMCLOCK_H_ + +#include /* for bool */ +#include /* for clockid_t, timer_t */ + +#include +#include + +struct hostclock { + implements_alarmclock; + clockid_t clock_id; + + BEGIN_PRIVATE(LIBHW_HOST_ALARMCLOCK_H) + bool initialized; + timer_t timer_id; + struct alarmclock_trigger *queue; + END_PRIVATE(LIBHW_HOST_ALARMCLOCK_H) +}; + +extern struct alarmclock_vtable hostclock_vtable; + +#endif /* _LIBHW_HOST_ALARMCLOCK_H_ */ diff --git a/libhw/rp2040_hwtimer.c b/libhw/rp2040_hwtimer.c index c8c281e..6c871a3 100644 --- a/libhw/rp2040_hwtimer.c +++ b/libhw/rp2040_hwtimer.c @@ -1,6 +1,6 @@ /* libhw/rp2040_hwtimer.c - implementation for the RP2040's hardware timer * - * Copyright (C) 2024 Luke T. Shumaker + * Copyright (C) 2024-2025 Luke T. Shumaker * SPDX-License-Identifier: AGPL-3.0-or-later */ @@ -54,8 +54,6 @@ static struct rp2040_hwtimer hwtimers[] = { }; static_assert(sizeof(hwtimers)/sizeof(hwtimers[0]) == _RP2040_HWALARM_NUM); -implements_alarmclock *bootclock = &hwtimers[0]; - /* Main implementation ********************************************************/ implements_alarmclock *rp2040_hwtimer(enum rp2040_hwalarm_instance alarm_num) { diff --git a/libhw_generic/alarmclock.c b/libhw_generic/alarmclock.c index a16f2f6..d501f51 100644 --- a/libhw_generic/alarmclock.c +++ b/libhw_generic/alarmclock.c @@ -1,6 +1,6 @@ /* libhw_generic/alarmclock.c - Device-independent utilities * - * Copyright (C) 2024 Luke T. Shumaker + * Copyright (C) 2024-2025 Luke T. Shumaker * SPDX-License-Identifier: AGPL-3.0-or-later */ @@ -9,6 +9,8 @@ #include +implements_alarmclock *bootclock = NULL; + static void alarmclock_sleep_intrhandler(void *_arg) { cid_t cid = *(cid_t *)_arg; cr_unpause_from_intrhandler(cid); -- cgit v1.2.3-2-g168b