summaryrefslogtreecommitdiff
path: root/libhw/host_alarmclock.c
blob: 4f43fc7f4192d8bf3fdef2a6b0f84f42b1f144f4 (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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* libhw/host_alarmclock.c - <libhw/generic/alarmclock.h> implementation for POSIX hosts
 *
 * Copyright (C) 2024  Luke T. Shumaker <lukeshu@lukeshu.com>
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

#include <errno.h>
#include <error.h>
#include <signal.h>
#include <time.h>

#include <libcr/coroutine.h>
#include <libmisc/assert.h>
#include <libmisc/vcall.h>

#define IMPLEMENTATION_FOR_LIBHW_GENERIC_ALARMCLOCK_H YES
#include <libhw/generic/alarmclock.h>

#include "host_util.h" /* for host_sigrt_alloc(), ns_to_host_ns_time() */

/* Types **********************************************************************/

struct hostclock {
	implements_alarmclock;
	bool                             initialized;
	clockid_t                        clock_id;
	timer_t                          timer_id;
	struct alarmclock_trigger       *queue;
};

/* Globals ********************************************************************/

static uint64_t hostclock_get_time_ns(implements_alarmclock *self);
static bool     hostclock_add_trigger(implements_alarmclock *self,
                                      struct alarmclock_trigger *trigger,
                                      uint64_t   fire_at_ns,
                                      void       (*cb)(void *),
                                      void      *cb_arg);
static void     hostclock_del_trigger(implements_alarmclock *self,
                                      struct alarmclock_trigger *trigger);

static 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 ********************************************************/

#define UNUSED(name) /* name __attribute__ ((unused)) */

static uint64_t hostclock_get_time_ns(implements_alarmclock *_alarmclock) {
	struct hostclock *alarmclock =
		VCALL_SELF(struct hostclock, implements_alarmclock, _alarmclock);
	assert(alarmclock);

	struct timespec ts;

	if (clock_gettime(alarmclock->clock_id, &ts) != 0)
		error(1, errno, "clock_gettime(%d)", (int)alarmclock->clock_id);

	return ns_from_host_ns_time(ts);
}

static void hostclock_handle_sig_alarm(int UNUSED(sig), siginfo_t *info, void *UNUSED(ucontext)) {
	struct hostclock *alarmclock = info->si_value.sival_ptr;
	assert(alarmclock);

	while (alarmclock->queue &&
	       alarmclock->queue->fire_at_ns <= hostclock_get_time_ns(alarmclock)) {
		struct alarmclock_trigger *trigger = alarmclock->queue;
		trigger->cb(trigger->cb_arg);
		alarmclock->queue = trigger->next;
		trigger->alarmclock = NULL;
		trigger->next = NULL;
		trigger->prev = NULL;
	}

	if (alarmclock->queue) {
		struct itimerspec alarmspec = {
			.it_value    = ns_to_host_ns_time(alarmclock->queue->fire_at_ns),
			.it_interval = {0},
		};
		if (timer_settime(alarmclock->timer_id, TIMER_ABSTIME, &alarmspec, NULL) != 0)
			error(1, errno, "timer_settime");
	}
}

static bool hostclock_add_trigger(implements_alarmclock *_alarmclock,
                                      struct alarmclock_trigger *trigger,
                                      uint64_t   fire_at_ns,
                                      void       (*cb)(void *),
                                      void      *cb_arg) {
	struct hostclock *alarmclock =
		VCALL_SELF(struct hostclock, implements_alarmclock, _alarmclock);
	assert(alarmclock);
	assert(trigger);
	assert(fire_at_ns);
	assert(cb);

	trigger->alarmclock = alarmclock;
	trigger->fire_at_ns = fire_at_ns;
	trigger->cb         = cb;
	trigger->cb_arg     = cb_arg;

	cr_disable_interrupts();
	struct alarmclock_trigger **dst = &alarmclock->queue;
	while (*dst && fire_at_ns >= (*dst)->fire_at_ns)
		dst = &(*dst)->next;
	trigger->next = *dst;
	trigger->prev = *dst ? (*dst)->prev : NULL;
	if (*dst)
		(*dst)->prev = trigger;
	*dst = trigger;
	if (!alarmclock->initialized) {
		struct sigevent how_to_notify = {
			.sigev_notify = SIGEV_SIGNAL,
			.sigev_signo  = host_sigrt_alloc(),
			.sigev_value  = {
				.sival_ptr = alarmclock,
			},
		};
		struct sigaction action = {
			.sa_flags = SA_SIGINFO,
			.sa_sigaction = hostclock_handle_sig_alarm,
		};
		if (sigaction(how_to_notify.sigev_signo, &action, NULL) != 0)
			error(1, errno, "sigaction");
		if (timer_create(alarmclock->clock_id, &how_to_notify, &alarmclock->timer_id) != 0)
			error(1, errno, "timer_create(%d)", (int)alarmclock->clock_id);
		alarmclock->initialized = true;
	}
	if (alarmclock->queue == trigger) {
		struct itimerspec alarmspec = {
			.it_value    = ns_to_host_ns_time(trigger->fire_at_ns),
			.it_interval = {0},
		};
		if (timer_settime(alarmclock->timer_id, TIMER_ABSTIME, &alarmspec, NULL) != 0)
			error(1, errno, "timer_settime");
	}
	cr_enable_interrupts();

	return false;
}

static void hostclock_del_trigger(implements_alarmclock *_alarmclock,
                                      struct alarmclock_trigger *trigger) {
	struct hostclock *alarmclock =
		VCALL_SELF(struct hostclock, implements_alarmclock, _alarmclock);

	assert(alarmclock);
	assert(trigger);

	cr_disable_interrupts();
	if (trigger->alarmclock == alarmclock) {
		if (!trigger->prev)
			alarmclock->queue = trigger->next;
		else
			trigger->prev->next = trigger->next;
		if (trigger->next)
			trigger->next->prev = trigger->prev;
		trigger->alarmclock = NULL;
		trigger->prev = NULL;
		trigger->next = NULL;
	}
	cr_enable_interrupts();
}