1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
/*
* Copyright (c) 2006 David Xu <davidxu@freebsd.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice unmodified, this list of conditions, and the following
* disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $FreeBSD$
*
*/
#include <sys/cdefs.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include "namespace.h"
#include <errno.h>
#include <pthread.h>
#include <stddef.h>
#include <signal.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include "sigev_thread.h"
#include "un-namespace.h"
extern int __sys_ktimer_create(clockid_t, struct sigevent *__restrict,
int *__restrict);
extern int __sys_ktimer_delete(int);
extern int __sys_ktimer_gettime(int, struct itimerspec *);
extern int __sys_ktimer_getoverrun(int);
extern int __sys_ktimer_settime(int, int,
const struct itimerspec *__restrict, struct itimerspec *__restrict);
struct __timer {
int oshandle;
struct sigev_node *node;
};
__weak_reference(__timer_create, timer_create);
__weak_reference(__timer_create, _timer_create);
__weak_reference(__timer_delete, timer_delete);
__weak_reference(__timer_delete, _timer_delete);
__weak_reference(__timer_gettime, timer_gettime);
__weak_reference(__timer_gettime, _timer_gettime);
__weak_reference(__timer_settime, timer_settime);
__weak_reference(__timer_settime, _timer_settime);
__weak_reference(__timer_getoverrun, timer_getoverrun);
__weak_reference(__timer_getoverrun, _timer_getoverrun);
typedef void (*timer_func)(union sigval val, int timerid, int overrun);
static void
timer_dispatch(struct sigev_node *sn, siginfo_t *si)
{
timer_func f = sn->sn_func;
/* I want to avoid expired notification. */
if (si->si_value.sival_int == sn->sn_gen)
f(sn->sn_value, si->si_timerid, si->si_overrun);
}
int
__timer_create(clockid_t clockid, struct sigevent *evp, timer_t *timerid)
{
struct __timer *timer;
struct sigevent ev;
struct sigev_node *sn;
int ret, err;
timer = malloc(sizeof(struct __timer));
if (timer == NULL)
return (-1);
if (evp == NULL || evp->sigev_notify != SIGEV_THREAD) {
ret = __sys_ktimer_create(clockid, evp, &timer->oshandle);
if (ret == -1) {
err = errno;
free(timer);
errno = err;
return (ret);
}
timer->node = NULL;
*timerid = timer;
return (0);
}
if (__sigev_check_init()) {
errno = EINVAL;
return (-1);
}
sn = __sigev_alloc(SI_TIMER, evp);
if (sn == NULL) {
errno = EAGAIN;
return (-1);
}
__sigev_get_sigevent(sn, &ev, sn->sn_gen);
ret = __sys_ktimer_create(clockid, &ev, &timer->oshandle);
if (ret != 0) {
err = errno;
__sigev_free(sn);
free(timer);
errno = err;
return (-1);
}
sn->sn_dispatch = timer_dispatch;
sn->sn_id = timer->oshandle;
__sigev_list_lock();
__sigev_register(sn);
__sigev_list_unlock();
*timerid = timer;
return (0);
}
int
__timer_delete(timer_t timerid)
{
int ret, err;
if (timerid->node != NULL) {
__sigev_list_lock();
__sigev_delete_node(timerid->node);
__sigev_list_unlock();
}
ret = __sys_ktimer_delete(timerid->oshandle);
err = errno;
free(timerid);
errno = err;
return (ret);
}
int
__timer_gettime(timer_t timerid, struct itimerspec *value)
{
return __sys_ktimer_gettime(timerid->oshandle, value);
}
int
__timer_getoverrun(timer_t timerid)
{
return __sys_ktimer_getoverrun(timerid->oshandle);
}
int
__timer_settime(timer_t timerid, int flags,
const struct itimerspec *__restrict value,
struct itimerspec *__restrict ovalue)
{
return __sys_ktimer_settime(timerid->oshandle,
flags, value, ovalue);
}
int
__timer_oshandle(timer_t timerid)
{
return (timerid->oshandle);
}
|