summaryrefslogtreecommitdiff
path: root/libhw_generic/alarmclock.c
blob: 7fd049e6bc0e06f6dd8716543d7f8db3832af187 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/* libhw_generic/alarmclock.c - Device-independent <libhw/generic/alarmclock.h> utilities
 *
 * Copyright (C) 2024-2025  Luke T. Shumaker <lukeshu@lukeshu.com>
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

#include <libcr/coroutine.h>

#include <libhw/generic/alarmclock.h>

lo_interface alarmclock bootclock = {0};

static void alarmclock_sleep_intrhandler(void *_arg) {
	cid_t cid = *(cid_t *)_arg;
	cr_unpause_from_intrhandler(cid);
}

void alarmclock_sleep_until_ns(lo_interface alarmclock clock, uint64_t abstime_ns) {
	bool saved = cr_save_and_disable_interrupts();
	cid_t cid = cr_getcid();
	struct alarmclock_trigger trigger;
	LO_CALL(clock, add_trigger, &trigger, abstime_ns, alarmclock_sleep_intrhandler, &cid);
	cr_pause_and_yield();
	cr_restore_interrupts(saved);
}