| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2008 The Android Open Source Project | 
|  | 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 | *  * Redistributions of source code must retain the above copyright | 
|  | 9 | *    notice, this list of conditions and the following disclaimer. | 
|  | 10 | *  * Redistributions in binary form must reproduce the above copyright | 
|  | 11 | *    notice, this list of conditions and the following disclaimer in | 
|  | 12 | *    the documentation and/or other materials provided with the | 
|  | 13 | *    distribution. | 
|  | 14 | * | 
|  | 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 
|  | 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 
|  | 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | 
|  | 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | 
|  | 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | 
|  | 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | 
|  | 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS | 
|  | 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | 
|  | 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | 
|  | 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | 
|  | 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 
|  | 26 | * SUCH DAMAGE. | 
|  | 27 | */ | 
|  | 28 |  | 
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 29 | #include "private/kernel_sigset_t.h" | 
|  | 30 |  | 
|  | 31 | #include <errno.h> | 
| Elliott Hughes | 05fc1d7 | 2015-01-28 18:02:33 -0800 | [diff] [blame] | 32 | #include <malloc.h> | 
| Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 33 | #include <pthread.h> | 
|  | 34 | #include <stdatomic.h> | 
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 35 | #include <stdio.h> | 
|  | 36 | #include <string.h> | 
| Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 37 | #include <time.h> | 
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 38 |  | 
|  | 39 | // System calls. | 
| Elliott Hughes | 8963dd3 | 2016-05-23 11:11:12 -0700 | [diff] [blame] | 40 | extern "C" int __rt_sigtimedwait(const sigset_t*, siginfo_t*, const timespec*, size_t); | 
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 41 | extern "C" int __timer_create(clockid_t, sigevent*, __kernel_timer_t*); | 
|  | 42 | extern "C" int __timer_delete(__kernel_timer_t); | 
|  | 43 | extern "C" int __timer_getoverrun(__kernel_timer_t); | 
|  | 44 | extern "C" int __timer_gettime(__kernel_timer_t, itimerspec*); | 
|  | 45 | extern "C" int __timer_settime(__kernel_timer_t, int, const itimerspec*, itimerspec*); | 
|  | 46 |  | 
|  | 47 | // Most POSIX timers are handled directly by the kernel. We translate SIGEV_THREAD timers | 
|  | 48 | // into SIGEV_THREAD_ID timers so the kernel handles all the time-related stuff and we just | 
|  | 49 | // need to worry about running user code on a thread. | 
|  | 50 |  | 
|  | 51 | // We can't use SIGALRM because too many other C library functions throw that around, and since | 
|  | 52 | // they don't send to a specific thread, all threads are eligible to handle the signal and we can | 
|  | 53 | // end up with one of our POSIX timer threads handling it (meaning that the intended recipient | 
|  | 54 | // doesn't). glibc uses SIGRTMIN for its POSIX timer implementation, so in the absence of any | 
|  | 55 | // reason to use anything else, we use that too. | 
| Elliott Hughes | 0990d4f | 2014-04-30 09:45:40 -0700 | [diff] [blame] | 56 | static const int TIMER_SIGNAL = (__SIGRTMIN + 0); | 
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 57 |  | 
|  | 58 | struct PosixTimer { | 
|  | 59 | __kernel_timer_t kernel_timer_id; | 
|  | 60 |  | 
|  | 61 | int sigev_notify; | 
|  | 62 |  | 
| Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 63 | // The fields below are only needed for a SIGEV_THREAD timer. | 
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 64 | pthread_t callback_thread; | 
|  | 65 | void (*callback)(sigval_t); | 
|  | 66 | sigval_t callback_argument; | 
| Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 67 | atomic_bool deleted;  // Set when the timer is deleted, to prevent further calling of callback. | 
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 68 | }; | 
|  | 69 |  | 
|  | 70 | static __kernel_timer_t to_kernel_timer_id(timer_t timer) { | 
|  | 71 | return reinterpret_cast<PosixTimer*>(timer)->kernel_timer_id; | 
|  | 72 | } | 
|  | 73 |  | 
|  | 74 | static void* __timer_thread_start(void* arg) { | 
|  | 75 | PosixTimer* timer = reinterpret_cast<PosixTimer*>(arg); | 
|  | 76 |  | 
|  | 77 | kernel_sigset_t sigset; | 
|  | 78 | sigaddset(sigset.get(), TIMER_SIGNAL); | 
|  | 79 |  | 
|  | 80 | while (true) { | 
|  | 81 | // Wait for a signal... | 
|  | 82 | siginfo_t si; | 
|  | 83 | memset(&si, 0, sizeof(si)); | 
|  | 84 | int rc = __rt_sigtimedwait(sigset.get(), &si, NULL, sizeof(sigset)); | 
|  | 85 | if (rc == -1) { | 
|  | 86 | continue; | 
|  | 87 | } | 
|  | 88 |  | 
| Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 89 | if (si.si_code == SI_TIMER) { | 
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 90 | // This signal was sent because a timer fired, so call the callback. | 
| Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 91 |  | 
|  | 92 | // All events to the callback will be ignored when the timer is deleted. | 
|  | 93 | if (atomic_load(&timer->deleted) == true) { | 
|  | 94 | continue; | 
|  | 95 | } | 
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 96 | timer->callback(timer->callback_argument); | 
|  | 97 | } else if (si.si_code == SI_TKILL) { | 
|  | 98 | // This signal was sent because someone wants us to exit. | 
| Elliott Hughes | 473d067 | 2014-04-01 19:07:52 -0700 | [diff] [blame] | 99 | free(timer); | 
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 100 | return NULL; | 
|  | 101 | } | 
|  | 102 | } | 
|  | 103 | } | 
|  | 104 |  | 
|  | 105 | static void __timer_thread_stop(PosixTimer* timer) { | 
| Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 106 | atomic_store(&timer->deleted, true); | 
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 107 | pthread_kill(timer->callback_thread, TIMER_SIGNAL); | 
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 108 | } | 
|  | 109 |  | 
|  | 110 | // http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_create.html | 
|  | 111 | int timer_create(clockid_t clock_id, sigevent* evp, timer_t* timer_id) { | 
| Elliott Hughes | 473d067 | 2014-04-01 19:07:52 -0700 | [diff] [blame] | 112 | PosixTimer* timer = reinterpret_cast<PosixTimer*>(malloc(sizeof(PosixTimer))); | 
|  | 113 | if (timer == NULL) { | 
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 114 | return -1; | 
|  | 115 | } | 
|  | 116 |  | 
| Elliott Hughes | 473d067 | 2014-04-01 19:07:52 -0700 | [diff] [blame] | 117 | timer->sigev_notify = (evp == NULL) ? SIGEV_SIGNAL : evp->sigev_notify; | 
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 118 |  | 
|  | 119 | // If not a SIGEV_THREAD timer, the kernel can handle it without our help. | 
| Elliott Hughes | 473d067 | 2014-04-01 19:07:52 -0700 | [diff] [blame] | 120 | if (timer->sigev_notify != SIGEV_THREAD) { | 
|  | 121 | if (__timer_create(clock_id, evp, &timer->kernel_timer_id) == -1) { | 
|  | 122 | free(timer); | 
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 123 | return -1; | 
|  | 124 | } | 
|  | 125 |  | 
| Elliott Hughes | 473d067 | 2014-04-01 19:07:52 -0700 | [diff] [blame] | 126 | *timer_id = timer; | 
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 127 | return 0; | 
|  | 128 | } | 
|  | 129 |  | 
|  | 130 | // Otherwise, this must be SIGEV_THREAD timer... | 
| Elliott Hughes | 473d067 | 2014-04-01 19:07:52 -0700 | [diff] [blame] | 131 | timer->callback = evp->sigev_notify_function; | 
|  | 132 | timer->callback_argument = evp->sigev_value; | 
| Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 133 | atomic_init(&timer->deleted, false); | 
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 134 |  | 
|  | 135 | // Check arguments that the kernel doesn't care about but we do. | 
| Elliott Hughes | 473d067 | 2014-04-01 19:07:52 -0700 | [diff] [blame] | 136 | if (timer->callback == NULL) { | 
|  | 137 | free(timer); | 
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 138 | errno = EINVAL; | 
|  | 139 | return -1; | 
|  | 140 | } | 
|  | 141 |  | 
|  | 142 | // Create this timer's thread. | 
|  | 143 | pthread_attr_t thread_attributes; | 
|  | 144 | if (evp->sigev_notify_attributes == NULL) { | 
|  | 145 | pthread_attr_init(&thread_attributes); | 
|  | 146 | } else { | 
|  | 147 | thread_attributes = *reinterpret_cast<pthread_attr_t*>(evp->sigev_notify_attributes); | 
|  | 148 | } | 
|  | 149 | pthread_attr_setdetachstate(&thread_attributes, PTHREAD_CREATE_DETACHED); | 
|  | 150 |  | 
|  | 151 | // We start the thread with TIMER_SIGNAL blocked by blocking the signal here and letting it | 
|  | 152 | // inherit. If it tried to block the signal itself, there would be a race. | 
|  | 153 | kernel_sigset_t sigset; | 
|  | 154 | sigaddset(sigset.get(), TIMER_SIGNAL); | 
|  | 155 | kernel_sigset_t old_sigset; | 
|  | 156 | pthread_sigmask(SIG_BLOCK, sigset.get(), old_sigset.get()); | 
|  | 157 |  | 
| Elliott Hughes | 473d067 | 2014-04-01 19:07:52 -0700 | [diff] [blame] | 158 | int rc = pthread_create(&timer->callback_thread, &thread_attributes, __timer_thread_start, timer); | 
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 159 |  | 
|  | 160 | pthread_sigmask(SIG_SETMASK, old_sigset.get(), NULL); | 
|  | 161 |  | 
|  | 162 | if (rc != 0) { | 
| Elliott Hughes | 473d067 | 2014-04-01 19:07:52 -0700 | [diff] [blame] | 163 | free(timer); | 
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 164 | errno = rc; | 
|  | 165 | return -1; | 
|  | 166 | } | 
|  | 167 |  | 
|  | 168 | sigevent se = *evp; | 
|  | 169 | se.sigev_signo = TIMER_SIGNAL; | 
|  | 170 | se.sigev_notify = SIGEV_THREAD_ID; | 
| Elliott Hughes | a4831cb | 2014-09-11 16:11:43 -0700 | [diff] [blame] | 171 | se.sigev_notify_thread_id = pthread_gettid_np(timer->callback_thread); | 
| Elliott Hughes | 473d067 | 2014-04-01 19:07:52 -0700 | [diff] [blame] | 172 | if (__timer_create(clock_id, &se, &timer->kernel_timer_id) == -1) { | 
|  | 173 | __timer_thread_stop(timer); | 
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 174 | return -1; | 
|  | 175 | } | 
|  | 176 |  | 
| Elliott Hughes | d1aea30 | 2015-04-25 10:05:24 -0700 | [diff] [blame] | 177 | // Give the thread a specific meaningful name. | 
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 178 | // It can't do this itself because the kernel timer isn't created until after it's running. | 
| Elliott Hughes | d1aea30 | 2015-04-25 10:05:24 -0700 | [diff] [blame] | 179 | char name[16]; // 16 is the kernel-imposed limit. | 
|  | 180 | snprintf(name, sizeof(name), "POSIX timer %d", to_kernel_timer_id(timer)); | 
| Elliott Hughes | 473d067 | 2014-04-01 19:07:52 -0700 | [diff] [blame] | 181 | pthread_setname_np(timer->callback_thread, name); | 
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 182 |  | 
| Elliott Hughes | 473d067 | 2014-04-01 19:07:52 -0700 | [diff] [blame] | 183 | *timer_id = timer; | 
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 184 | return 0; | 
|  | 185 | } | 
|  | 186 |  | 
|  | 187 | // http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_delete.html | 
|  | 188 | int timer_delete(timer_t id) { | 
|  | 189 | int rc = __timer_delete(to_kernel_timer_id(id)); | 
|  | 190 | if (rc == -1) { | 
|  | 191 | return -1; | 
|  | 192 | } | 
|  | 193 |  | 
|  | 194 | PosixTimer* timer = reinterpret_cast<PosixTimer*>(id); | 
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 195 | if (timer->sigev_notify == SIGEV_THREAD) { | 
| Elliott Hughes | 473d067 | 2014-04-01 19:07:52 -0700 | [diff] [blame] | 196 | // Stopping the timer's thread frees the timer data when it's safe. | 
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 197 | __timer_thread_stop(timer); | 
| Elliott Hughes | 473d067 | 2014-04-01 19:07:52 -0700 | [diff] [blame] | 198 | } else { | 
|  | 199 | // For timers without threads, we can just free right away. | 
|  | 200 | free(timer); | 
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 201 | } | 
|  | 202 |  | 
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 203 | return 0; | 
|  | 204 | } | 
|  | 205 |  | 
| Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 206 | // http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_gettime.html | 
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 207 | int timer_gettime(timer_t id, itimerspec* ts) { | 
|  | 208 | return __timer_gettime(to_kernel_timer_id(id), ts); | 
|  | 209 | } | 
|  | 210 |  | 
| Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 211 | // http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_settime.html | 
|  | 212 | // When using timer_settime to disarm a repeatable SIGEV_THREAD timer with a very small | 
|  | 213 | // period (like below 1ms), the kernel may continue to send events to the callback thread | 
|  | 214 | // for a few extra times. This behavior is fine because in POSIX standard: The effect of | 
|  | 215 | // disarming or resetting a timer with pending expiration notifications is unspecified. | 
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 216 | int timer_settime(timer_t id, int flags, const itimerspec* ts, itimerspec* ots) { | 
| Christopher Ferris | 62d84b1 | 2014-10-20 19:09:19 -0700 | [diff] [blame] | 217 | PosixTimer* timer= reinterpret_cast<PosixTimer*>(id); | 
| Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 218 | return __timer_settime(timer->kernel_timer_id, flags, ts, ots); | 
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 219 | } | 
|  | 220 |  | 
|  | 221 | // http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_getoverrun.html | 
|  | 222 | int timer_getoverrun(timer_t id) { | 
|  | 223 | return __timer_getoverrun(to_kernel_timer_id(id)); | 
|  | 224 | } |