2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2006 David Xu <davidxu@freebsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
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.
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.
32 #include <sys/cdefs.h>
33 #include <sys/types.h>
34 #include <sys/syscall.h>
36 #include "namespace.h"
42 #include "sigev_thread.h"
43 #include "un-namespace.h"
45 extern int __sys_ktimer_create(clockid_t, struct sigevent *__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);
55 struct sigev_node *node;
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);
69 typedef void (*timer_func)(union sigval val, int overrun);
72 timer_dispatch(struct sigev_node *sn)
74 timer_func f = sn->sn_func;
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);
82 __timer_create(clockid_t clockid, struct sigevent *evp, timer_t *timerid)
84 struct __timer *timer;
86 struct sigev_node *sn;
89 timer = malloc(sizeof(struct __timer));
93 if (evp == NULL || evp->sigev_notify != SIGEV_THREAD) {
94 ret = __sys_ktimer_create(clockid, evp, &timer->oshandle);
106 if (__sigev_check_init()) {
112 sn = __sigev_alloc(SI_TIMER, evp, NULL, 0);
119 __sigev_get_sigevent(sn, &ev, sn->sn_gen);
120 ret = __sys_ktimer_create(clockid, &ev, &timer->oshandle);
128 sn->sn_flags |= SNF_SYNC;
129 sn->sn_dispatch = timer_dispatch;
130 sn->sn_id = timer->oshandle;
133 __sigev_register(sn);
134 __sigev_list_unlock();
140 __timer_delete(timer_t timerid)
144 if (timerid->node != NULL) {
146 __sigev_delete_node(timerid->node);
147 __sigev_list_unlock();
149 ret = __sys_ktimer_delete(timerid->oshandle);
157 __timer_gettime(timer_t timerid, struct itimerspec *value)
160 return __sys_ktimer_gettime(timerid->oshandle, value);
164 __timer_getoverrun(timer_t timerid)
167 return __sys_ktimer_getoverrun(timerid->oshandle);
171 __timer_settime(timer_t timerid, int flags,
172 const struct itimerspec *__restrict value,
173 struct itimerspec *__restrict ovalue)
176 return __sys_ktimer_settime(timerid->oshandle,
177 flags, value, ovalue);
180 #pragma weak timer_oshandle_np
182 timer_oshandle_np(timer_t timerid)
185 return (timerid->oshandle);