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 156265 2006-03-04 00:08:24Z davidxu $
|
26 * $FreeBSD: head/lib/librt/timer.c 156267 2006-03-04 00:18:19Z 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> 37#include <stddef.h> 38#include <signal.h> 39#include <stdlib.h> 40#include <time.h> 41#include <unistd.h> 42#include "sigev_thread.h" 43#include "un-namespace.h" 44 45extern int __sys_ktimer_create(clockid_t, struct sigevent *__restrict, 46 int *__restrict); 47extern int __sys_ktimer_delete(int); 48extern int __sys_ktimer_gettime(int, struct itimerspec *); 49extern int __sys_ktimer_getoverrun(int); 50extern int __sys_ktimer_settime(int, int, 51 const struct itimerspec *__restrict, struct itimerspec *__restrict); 52 53struct __timer { 54 int oshandle; 55 struct sigev_node *node; 56}; 57 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); 68
|
69typedef void (*timer_func)(union sigval val, int timerid, int overrun);
|
69typedef void (*timer_func)(union sigval val, int overrun); |
70 71static void
|
72timer_dispatch(struct sigev_node *sn, siginfo_t *si)
|
72timer_dispatch(struct sigev_node *sn) |
73{ 74 timer_func f = sn->sn_func; 75 76 /* I want to avoid expired notification. */
|
77 if (si->si_value.sival_int == sn->sn_gen)
78 f(sn->sn_value, si->si_timerid, si->si_overrun);
|
77 if (sn->sn_info.si_value.sival_int == sn->sn_gen) 78 f(sn->sn_value, sn->sn_info.si_overrun); |
79} 80 81int 82__timer_create(clockid_t clockid, struct sigevent *evp, timer_t *timerid) 83{ 84 struct __timer *timer; 85 struct sigevent ev; 86 struct sigev_node *sn; 87 int ret, err; 88 89 timer = malloc(sizeof(struct __timer)); 90 if (timer == NULL) 91 return (-1); 92 93 if (evp == NULL || evp->sigev_notify != SIGEV_THREAD) { 94 ret = __sys_ktimer_create(clockid, evp, &timer->oshandle); 95 if (ret == -1) { 96 err = errno; 97 free(timer); 98 errno = err; 99 return (ret); 100 } 101 timer->node = NULL; 102 *timerid = timer; 103 return (0); 104 } 105 106 if (__sigev_check_init()) { 107 errno = EINVAL; 108 return (-1); 109 } 110
|
111 sn = __sigev_alloc(SI_TIMER, evp);
|
111 sn = __sigev_alloc(SI_TIMER, evp, NULL, 0); |
112 if (sn == NULL) { 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_dispatch = timer_dispatch; 127 sn->sn_id = timer->oshandle; 128 timer->node = sn; 129 __sigev_list_lock(); 130 __sigev_register(sn); 131 __sigev_list_unlock(); 132 *timerid = timer; 133 return (0); 134} 135 136int 137__timer_delete(timer_t timerid) 138{ 139 int ret, err; 140 141 if (timerid->node != NULL) { 142 __sigev_list_lock(); 143 __sigev_delete_node(timerid->node); 144 __sigev_list_unlock(); 145 } 146 ret = __sys_ktimer_delete(timerid->oshandle); 147 err = errno; 148 free(timerid); 149 errno = err; 150 return (ret); 151} 152 153int 154__timer_gettime(timer_t timerid, struct itimerspec *value) 155{ 156 157 return __sys_ktimer_gettime(timerid->oshandle, value); 158} 159 160int 161__timer_getoverrun(timer_t timerid) 162{ 163 164 return __sys_ktimer_getoverrun(timerid->oshandle); 165} 166 167int 168__timer_settime(timer_t timerid, int flags, 169 const struct itimerspec *__restrict value, 170 struct itimerspec *__restrict ovalue) 171{ 172 173 return __sys_ktimer_settime(timerid->oshandle, 174 flags, value, ovalue); 175} 176 177int 178__timer_oshandle(timer_t timerid) 179{ 180 181 return (timerid->oshandle); 182}
|