]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - lib/librt/timer.c
MFC r227661:
[FreeBSD/releng/9.0.git] / lib / librt / timer.c
1 /*
2  * Copyright (c) 2006 David Xu <davidxu@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
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.
14  *
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.
25  *
26  * $FreeBSD$
27  *
28  */
29
30 #include <sys/cdefs.h>
31 #include <sys/types.h>
32 #include <sys/syscall.h>
33
34 #include "namespace.h"
35 #include <errno.h>
36 #include <stddef.h>
37 #include <signal.h>
38 #include <stdlib.h>
39 #include <time.h>
40 #include "sigev_thread.h"
41 #include "un-namespace.h"
42
43 extern int __sys_ktimer_create(clockid_t, struct sigevent *__restrict,
44         int *__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);
50
51 struct __timer {
52         int oshandle;
53         struct sigev_node *node;
54 };
55
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);
66
67 typedef void (*timer_func)(union sigval val, int overrun);
68
69 static void
70 timer_dispatch(struct sigev_node *sn)
71 {
72         timer_func f = sn->sn_func;
73
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);
77 }
78
79 int
80 __timer_create(clockid_t clockid, struct sigevent *evp, timer_t *timerid)
81 {
82         struct __timer *timer;
83         struct sigevent ev;
84         struct sigev_node *sn;
85         int ret, err;
86
87         timer = malloc(sizeof(struct __timer));
88         if (timer == NULL)
89                 return (-1);
90
91         if (evp == NULL || evp->sigev_notify != SIGEV_THREAD) {
92                 ret = __sys_ktimer_create(clockid, evp, &timer->oshandle);
93                 if (ret == -1) {
94                         err = errno;
95                         free(timer);
96                         errno = err;
97                         return (ret);
98                 }
99                 timer->node = NULL;
100                 *timerid = timer;
101                 return (0);
102         }
103
104         if (__sigev_check_init()) {
105                 free(timer);
106                 errno = EINVAL;
107                 return (-1);
108         }
109
110         sn = __sigev_alloc(SI_TIMER, evp, NULL, 0);
111         if (sn == NULL) {
112                 free(timer);
113                 errno = EAGAIN;
114                 return (-1);
115         }
116
117         __sigev_get_sigevent(sn, &ev, sn->sn_gen);
118         ret = __sys_ktimer_create(clockid, &ev, &timer->oshandle);
119         if (ret != 0) {
120                 err = errno;
121                 __sigev_free(sn);
122                 free(timer);
123                 errno = err;
124                 return (-1);
125         }
126         sn->sn_flags |= SNF_SYNC;
127         sn->sn_dispatch = timer_dispatch;
128         sn->sn_id = timer->oshandle;
129         timer->node = sn;
130         __sigev_list_lock();
131         __sigev_register(sn);
132         __sigev_list_unlock();
133         *timerid = timer;
134         return (0);
135 }
136
137 int
138 __timer_delete(timer_t timerid)
139 {
140         int ret, err;
141
142         if (timerid->node != NULL) {
143                 __sigev_list_lock();
144                 __sigev_delete_node(timerid->node);
145                 __sigev_list_unlock();
146         }
147         ret = __sys_ktimer_delete(timerid->oshandle);
148         err = errno;
149         free(timerid);
150         errno = err;
151         return (ret);   
152 }
153
154 int
155 __timer_gettime(timer_t timerid, struct itimerspec *value)
156 {
157
158         return __sys_ktimer_gettime(timerid->oshandle, value);
159 }
160
161 int
162 __timer_getoverrun(timer_t timerid)
163 {
164
165         return __sys_ktimer_getoverrun(timerid->oshandle);
166 }
167
168 int
169 __timer_settime(timer_t timerid, int flags,
170         const struct itimerspec *__restrict value,
171         struct itimerspec *__restrict ovalue)
172 {
173
174         return __sys_ktimer_settime(timerid->oshandle,
175                 flags, value, ovalue);
176 }
177
178 int
179 __timer_oshandle(timer_t timerid)
180 {
181
182         return (timerid->oshandle);
183 }