summaryrefslogtreecommitdiff
path: root/libhw_cr/host_util.c
blob: 8cacd576615feb8353c0f14430db863134cf4feb (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
/* libhw_cr/host_util.c - Utilities for GNU/Linux hosts
 *
 * Copyright (C) 2024-2025  Luke T. Shumaker <lukeshu@lukeshu.com>
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

#include <error.h>  /* for error(3gnu) */
#include <signal.h> /* for SIGRTMIN, SIGRTMAX */

#include <libhw/generic/alarmclock.h> /* for {X}S_PER_S */

#include "host_util.h"

int host_sigrt_alloc(void) {
	static int next = 0;

	if (!next)
		next = SIGRTMIN;
	int ret = next++;
	if (ret > SIGRTMAX)
		error(1, 0, "SIGRTMAX exceeded");
	return ret;
}

host_us_time_t ns_to_host_us_time(uint64_t time_ns) {
	host_us_time_t ret;
	ret.tv_sec = time_ns
		/NS_PER_S;
	ret.tv_usec = (time_ns - ((uint64_t)ret.tv_sec)*NS_PER_S)
		/(NS_PER_S/US_PER_S);
	return ret;
}

host_ns_time_t ns_to_host_ns_time(uint64_t time_ns) {
	host_ns_time_t ret;
	ret.tv_sec = time_ns
		/NS_PER_S;
	ret.tv_nsec = time_ns - ((uint64_t)ret.tv_sec)*NS_PER_S;
	return ret;
}

uint64_t ns_from_host_us_time(host_us_time_t host_time) {
	return (((uint64_t)host_time.tv_sec) * NS_PER_S) +
		((uint64_t)host_time.tv_usec * (NS_PER_S/US_PER_S));
}

uint64_t ns_from_host_ns_time(host_ns_time_t host_time) {
	return (((uint64_t)host_time.tv_sec) * NS_PER_S) +
		((uint64_t)host_time.tv_nsec);
}