xref: /freebsd/lib/librt/timer.c (revision bdd1243d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2006 David Xu <davidxu@freebsd.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice unmodified, this list of conditions, and the following
12  *    disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  *
30  */
31 
32 #include <sys/cdefs.h>
33 #include <sys/types.h>
34 #include <sys/syscall.h>
35 
36 #include "namespace.h"
37 #include <errno.h>
38 #include <stddef.h>
39 #include <signal.h>
40 #include <stdlib.h>
41 #include <time.h>
42 #include "sigev_thread.h"
43 #include "un-namespace.h"
44 
45 extern int __sys_ktimer_create(clockid_t, struct sigevent *__restrict,
46 	int *__restrict);
47 extern int __sys_ktimer_delete(int);
48 extern int __sys_ktimer_gettime(int, struct itimerspec *);
49 extern int __sys_ktimer_getoverrun(int);
50 extern int __sys_ktimer_settime(int, int,
51 	const struct itimerspec *__restrict, struct itimerspec *__restrict);
52 
53 struct __timer {
54 	int oshandle;
55 	struct sigev_node *node;
56 };
57 
58 __weak_reference(__timer_create, timer_create);
59 __weak_reference(__timer_create, _timer_create);
60 __weak_reference(__timer_delete, timer_delete);
61 __weak_reference(__timer_delete, _timer_delete);
62 __weak_reference(__timer_gettime, timer_gettime);
63 __weak_reference(__timer_gettime, _timer_gettime);
64 __weak_reference(__timer_settime, timer_settime);
65 __weak_reference(__timer_settime, _timer_settime);
66 __weak_reference(__timer_getoverrun, timer_getoverrun);
67 __weak_reference(__timer_getoverrun, _timer_getoverrun);
68 
69 typedef void (*timer_func)(union sigval val, int overrun);
70 
71 static void
72 timer_dispatch(struct sigev_node *sn)
73 {
74 	timer_func f = sn->sn_func;
75 
76 	/* I want to avoid expired notification. */
77 	if (sn->sn_info.si_value.sival_int == sn->sn_gen)
78 		f(sn->sn_value, sn->sn_info.si_overrun);
79 }
80 
81 int
82 __timer_create(clockid_t clockid, struct sigevent *evp, timer_t *timerid)
83 {
84 	struct __timer *timer;
85 	struct sigevent ev;
86 	struct sigev_node *sn;
87 	int ret, err;
88 
89 	timer = malloc(sizeof(struct __timer));
90 	if (timer == NULL)
91 		return (-1);
92 
93 	if (evp == NULL || evp->sigev_notify != SIGEV_THREAD) {
94 		ret = __sys_ktimer_create(clockid, evp, &timer->oshandle);
95 		if (ret == -1) {
96 			err = errno;
97 			free(timer);
98 			errno = err;
99 			return (ret);
100 		}
101 		timer->node = NULL;
102 		*timerid = timer;
103 		return (0);
104 	}
105 
106 	if (__sigev_check_init()) {
107 		free(timer);
108 		errno = EINVAL;
109 		return (-1);
110 	}
111 
112 	sn = __sigev_alloc(SI_TIMER, evp, NULL, 0);
113 	if (sn == NULL) {
114 		free(timer);
115 		errno = EAGAIN;
116 		return (-1);
117 	}
118 
119 	__sigev_get_sigevent(sn, &ev, sn->sn_gen);
120 	ret = __sys_ktimer_create(clockid, &ev, &timer->oshandle);
121 	if (ret != 0) {
122 		err = errno;
123 		__sigev_free(sn);
124 		free(timer);
125 		errno = err;
126 		return (-1);
127 	}
128 	sn->sn_flags |= SNF_SYNC;
129 	sn->sn_dispatch = timer_dispatch;
130 	sn->sn_id = timer->oshandle;
131 	timer->node = sn;
132 	__sigev_list_lock();
133 	__sigev_register(sn);
134 	__sigev_list_unlock();
135 	*timerid = timer;
136 	return (0);
137 }
138 
139 int
140 __timer_delete(timer_t timerid)
141 {
142 	int ret, err;
143 
144 	if (timerid->node != NULL) {
145 		__sigev_list_lock();
146 		__sigev_delete_node(timerid->node);
147 		__sigev_list_unlock();
148 	}
149 	ret = __sys_ktimer_delete(timerid->oshandle);
150 	err = errno;
151 	free(timerid);
152 	errno = err;
153 	return (ret);
154 }
155 
156 int
157 __timer_gettime(timer_t timerid, struct itimerspec *value)
158 {
159 
160 	return __sys_ktimer_gettime(timerid->oshandle, value);
161 }
162 
163 int
164 __timer_getoverrun(timer_t timerid)
165 {
166 
167 	return __sys_ktimer_getoverrun(timerid->oshandle);
168 }
169 
170 int
171 __timer_settime(timer_t timerid, int flags,
172 	const struct itimerspec *__restrict value,
173 	struct itimerspec *__restrict ovalue)
174 {
175 
176 	return __sys_ktimer_settime(timerid->oshandle,
177 		flags, value, ovalue);
178 }
179 
180 #pragma weak timer_oshandle_np
181 int
182 timer_oshandle_np(timer_t timerid)
183 {
184 
185 	return (timerid->oshandle);
186 }
187