Deleted Added
full compact
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: head/lib/librt/timer.c 156267 2006-03-04 00:18:19Z davidxu $
26 * $FreeBSD: head/lib/librt/timer.c 156383 2006-03-07 08:28:07Z davidxu $
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 <pthread.h>
36#include <stddef.h>
37#include <signal.h>
38#include <stdlib.h>
39#include <time.h>
41#include <unistd.h>
40#include "sigev_thread.h"
41#include "un-namespace.h"
42
43extern int __sys_ktimer_create(clockid_t, struct sigevent *__restrict,
44 int *__restrict);
45extern int __sys_ktimer_delete(int);
46extern int __sys_ktimer_gettime(int, struct itimerspec *);
47extern int __sys_ktimer_getoverrun(int);
48extern int __sys_ktimer_settime(int, int,
49 const struct itimerspec *__restrict, struct itimerspec *__restrict);
50
51struct __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
67typedef void (*timer_func)(union sigval val, int overrun);
68
69static void
70timer_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
79int
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 errno = EINVAL;
106 return (-1);
107 }
108
109 sn = __sigev_alloc(SI_TIMER, evp, NULL, 0);
110 if (sn == NULL) {
111 errno = EAGAIN;
112 return (-1);
113 }
114
115 __sigev_get_sigevent(sn, &ev, sn->sn_gen);
116 ret = __sys_ktimer_create(clockid, &ev, &timer->oshandle);
117 if (ret != 0) {
118 err = errno;
119 __sigev_free(sn);
120 free(timer);
121 errno = err;
122 return (-1);
123 }
124 sn->sn_dispatch = timer_dispatch;
125 sn->sn_id = timer->oshandle;
126 timer->node = sn;
127 __sigev_list_lock();
128 __sigev_register(sn);
129 __sigev_list_unlock();
130 *timerid = timer;
131 return (0);
132}
133
134int
135__timer_delete(timer_t timerid)
136{
137 int ret, err;
138
139 if (timerid->node != NULL) {
140 __sigev_list_lock();
141 __sigev_delete_node(timerid->node);
142 __sigev_list_unlock();
143 }
144 ret = __sys_ktimer_delete(timerid->oshandle);
145 err = errno;
146 free(timerid);
147 errno = err;
148 return (ret);
149}
150
151int
152__timer_gettime(timer_t timerid, struct itimerspec *value)
153{
154
155 return __sys_ktimer_gettime(timerid->oshandle, value);
156}
157
158int
159__timer_getoverrun(timer_t timerid)
160{
161
162 return __sys_ktimer_getoverrun(timerid->oshandle);
163}
164
165int
166__timer_settime(timer_t timerid, int flags,
167 const struct itimerspec *__restrict value,
168 struct itimerspec *__restrict ovalue)
169{
170
171 return __sys_ktimer_settime(timerid->oshandle,
172 flags, value, ovalue);
173}
174
175int
176__timer_oshandle(timer_t timerid)
177{
178
179 return (timerid->oshandle);
180}