xref: /freebsd/lib/librt/timer.c (revision 3494f7c0)
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  */
29 
30 #include <sys/types.h>
31 #include <sys/syscall.h>
32 
33 #include "namespace.h"
34 #include <errno.h>
35 #include <stddef.h>
36 #include <signal.h>
37 #include <stdlib.h>
38 #include <time.h>
39 #include "sigev_thread.h"
40 #include "un-namespace.h"
41 
42 extern int __sys_ktimer_create(clockid_t, struct sigevent *__restrict,
43 	int *__restrict);
44 extern int __sys_ktimer_delete(int);
45 extern int __sys_ktimer_gettime(int, struct itimerspec *);
46 extern int __sys_ktimer_getoverrun(int);
47 extern int __sys_ktimer_settime(int, int,
48 	const struct itimerspec *__restrict, struct itimerspec *__restrict);
49 
50 struct __timer {
51 	int oshandle;
52 	struct sigev_node *node;
53 };
54 
55 __weak_reference(__timer_create, timer_create);
56 __weak_reference(__timer_create, _timer_create);
57 __weak_reference(__timer_delete, timer_delete);
58 __weak_reference(__timer_delete, _timer_delete);
59 __weak_reference(__timer_gettime, timer_gettime);
60 __weak_reference(__timer_gettime, _timer_gettime);
61 __weak_reference(__timer_settime, timer_settime);
62 __weak_reference(__timer_settime, _timer_settime);
63 __weak_reference(__timer_getoverrun, timer_getoverrun);
64 __weak_reference(__timer_getoverrun, _timer_getoverrun);
65 
66 typedef void (*timer_func)(union sigval val, int overrun);
67 
68 static void
69 timer_dispatch(struct sigev_node *sn)
70 {
71 	timer_func f = sn->sn_func;
72 
73 	/* I want to avoid expired notification. */
74 	if (sn->sn_info.si_value.sival_int == sn->sn_gen)
75 		f(sn->sn_value, sn->sn_info.si_overrun);
76 }
77 
78 int
79 __timer_create(clockid_t clockid, struct sigevent *evp, timer_t *timerid)
80 {
81 	struct __timer *timer;
82 	struct sigevent ev;
83 	struct sigev_node *sn;
84 	int ret, err;
85 
86 	timer = malloc(sizeof(struct __timer));
87 	if (timer == NULL)
88 		return (-1);
89 
90 	if (evp == NULL || evp->sigev_notify != SIGEV_THREAD) {
91 		ret = __sys_ktimer_create(clockid, evp, &timer->oshandle);
92 		if (ret == -1) {
93 			err = errno;
94 			free(timer);
95 			errno = err;
96 			return (ret);
97 		}
98 		timer->node = NULL;
99 		*timerid = timer;
100 		return (0);
101 	}
102 
103 	if (__sigev_check_init()) {
104 		free(timer);
105 		errno = EINVAL;
106 		return (-1);
107 	}
108 
109 	sn = __sigev_alloc(SI_TIMER, evp, NULL, 0);
110 	if (sn == NULL) {
111 		free(timer);
112 		errno = EAGAIN;
113 		return (-1);
114 	}
115 
116 	__sigev_get_sigevent(sn, &ev, sn->sn_gen);
117 	ret = __sys_ktimer_create(clockid, &ev, &timer->oshandle);
118 	if (ret != 0) {
119 		err = errno;
120 		__sigev_free(sn);
121 		free(timer);
122 		errno = err;
123 		return (-1);
124 	}
125 	sn->sn_flags |= SNF_SYNC;
126 	sn->sn_dispatch = timer_dispatch;
127 	sn->sn_id = timer->oshandle;
128 	timer->node = sn;
129 	__sigev_list_lock();
130 	__sigev_register(sn);
131 	__sigev_list_unlock();
132 	*timerid = timer;
133 	return (0);
134 }
135 
136 int
137 __timer_delete(timer_t timerid)
138 {
139 	int ret, err;
140 
141 	if (timerid->node != NULL) {
142 		__sigev_list_lock();
143 		__sigev_delete_node(timerid->node);
144 		__sigev_list_unlock();
145 	}
146 	ret = __sys_ktimer_delete(timerid->oshandle);
147 	err = errno;
148 	free(timerid);
149 	errno = err;
150 	return (ret);
151 }
152 
153 int
154 __timer_gettime(timer_t timerid, struct itimerspec *value)
155 {
156 
157 	return __sys_ktimer_gettime(timerid->oshandle, value);
158 }
159 
160 int
161 __timer_getoverrun(timer_t timerid)
162 {
163 
164 	return __sys_ktimer_getoverrun(timerid->oshandle);
165 }
166 
167 int
168 __timer_settime(timer_t timerid, int flags,
169 	const struct itimerspec *__restrict value,
170 	struct itimerspec *__restrict ovalue)
171 {
172 
173 	return __sys_ktimer_settime(timerid->oshandle,
174 		flags, value, ovalue);
175 }
176 
177 #pragma weak timer_oshandle_np
178 int
179 timer_oshandle_np(timer_t timerid)
180 {
181 
182 	return (timerid->oshandle);
183 }
184