2 * Copyright (c) 2006 David Xu <davidxu@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
31 #include <sys/types.h>
32 #include <sys/syscall.h>
34 #include "namespace.h"
40 #include "sigev_thread.h"
41 #include "un-namespace.h"
43 extern int __sys_ktimer_create(clockid_t, struct sigevent *__restrict,
45 extern int __sys_ktimer_delete(int);
46 extern int __sys_ktimer_gettime(int, struct itimerspec *);
47 extern int __sys_ktimer_getoverrun(int);
48 extern int __sys_ktimer_settime(int, int,
49 const struct itimerspec *__restrict, struct itimerspec *__restrict);
53 struct sigev_node *node;
56 __weak_reference(__timer_create, timer_create);
57 __weak_reference(__timer_create, _timer_create);
58 __weak_reference(__timer_delete, timer_delete);
59 __weak_reference(__timer_delete, _timer_delete);
60 __weak_reference(__timer_gettime, timer_gettime);
61 __weak_reference(__timer_gettime, _timer_gettime);
62 __weak_reference(__timer_settime, timer_settime);
63 __weak_reference(__timer_settime, _timer_settime);
64 __weak_reference(__timer_getoverrun, timer_getoverrun);
65 __weak_reference(__timer_getoverrun, _timer_getoverrun);
67 typedef void (*timer_func)(union sigval val, int overrun);
70 timer_dispatch(struct sigev_node *sn)
72 timer_func f = sn->sn_func;
74 /* I want to avoid expired notification. */
75 if (sn->sn_info.si_value.sival_int == sn->sn_gen)
76 f(sn->sn_value, sn->sn_info.si_overrun);
80 __timer_create(clockid_t clockid, struct sigevent *evp, timer_t *timerid)
82 struct __timer *timer;
84 struct sigev_node *sn;
87 timer = malloc(sizeof(struct __timer));
91 if (evp == NULL || evp->sigev_notify != SIGEV_THREAD) {
92 ret = __sys_ktimer_create(clockid, evp, &timer->oshandle);
104 if (__sigev_check_init()) {
110 sn = __sigev_alloc(SI_TIMER, evp, NULL, 0);
117 __sigev_get_sigevent(sn, &ev, sn->sn_gen);
118 ret = __sys_ktimer_create(clockid, &ev, &timer->oshandle);
126 sn->sn_flags |= SNF_SYNC;
127 sn->sn_dispatch = timer_dispatch;
128 sn->sn_id = timer->oshandle;
131 __sigev_register(sn);
132 __sigev_list_unlock();
138 __timer_delete(timer_t timerid)
142 if (timerid->node != NULL) {
144 __sigev_delete_node(timerid->node);
145 __sigev_list_unlock();
147 ret = __sys_ktimer_delete(timerid->oshandle);
155 __timer_gettime(timer_t timerid, struct itimerspec *value)
158 return __sys_ktimer_gettime(timerid->oshandle, value);
162 __timer_getoverrun(timer_t timerid)
165 return __sys_ktimer_getoverrun(timerid->oshandle);
169 __timer_settime(timer_t timerid, int flags,
170 const struct itimerspec *__restrict value,
171 struct itimerspec *__restrict ovalue)
174 return __sys_ktimer_settime(timerid->oshandle,
175 flags, value, ovalue);
179 __timer_oshandle(timer_t timerid)
182 return (timerid->oshandle);