timer.c revision 156136
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 156136 2006-03-01 06:37:34Z 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); 70 71static void 72timer_dispatch(struct sigev_node *sn, siginfo_t *si) 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); 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); 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 __sigev_list_lock(); 129 __sigev_register(sn); 130 __sigev_list_unlock(); 131 *timerid = timer; 132 return (0); 133} 134 135int 136__timer_delete(timer_t timerid) 137{ 138 int ret, err; 139 140 if (timerid->node != NULL) { 141 __sigev_list_lock(); 142 __sigev_delete_node(timerid->node); 143 __sigev_list_unlock(); 144 } 145 ret = __sys_ktimer_delete(timerid->oshandle); 146 err = errno; 147 free(timerid); 148 errno = err; 149 return (ret); 150} 151 152int 153__timer_gettime(timer_t timerid, struct itimerspec *value) 154{ 155 156 return __sys_ktimer_gettime(timerid->oshandle, value); 157} 158 159int 160__timer_getoverrun(timer_t timerid) 161{ 162 163 return __sys_ktimer_getoverrun(timerid->oshandle); 164} 165 166int 167__timer_settime(timer_t timerid, int flags, 168 const struct itimerspec *__restrict value, 169 struct itimerspec *__restrict ovalue) 170{ 171 172 return __sys_ktimer_settime(timerid->oshandle, 173 flags, value, ovalue); 174} 175 176int 177__timer_oshandle(timer_t timerid) 178{ 179 180 return (timerid->oshandle); 181} 182