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 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_flags |= SNF_SYNC; 125 sn->sn_dispatch = timer_dispatch; 126 sn->sn_id = timer->oshandle; 127 timer->node = sn; 128 __sigev_list_lock(); 129 __sigev_register(sn); 130 __sigev_list_unlock(); 131 *timerid = timer; 132 return (0); 133 } 134 135 int 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 152 int 153 __timer_gettime(timer_t timerid, struct itimerspec *value) 154 { 155 156 return __sys_ktimer_gettime(timerid->oshandle, value); 157 } 158 159 int 160 __timer_getoverrun(timer_t timerid) 161 { 162 163 return __sys_ktimer_getoverrun(timerid->oshandle); 164 } 165 166 int 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 176 int 177 __timer_oshandle(timer_t timerid) 178 { 179 180 return (timerid->oshandle); 181 } 182