| 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 |  | 
|  | 29 | #include "pthread_internal.h" | 
|  | 30 | #include "private/bionic_futex.h" | 
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 31 | #include "private/kernel_sigset_t.h" | 
|  | 32 |  | 
|  | 33 | #include <errno.h> | 
|  | 34 | #include <stdio.h> | 
|  | 35 | #include <string.h> | 
|  | 36 |  | 
|  | 37 | // System calls. | 
|  | 38 | extern "C" int __rt_sigtimedwait(const sigset_t*, siginfo_t*, const struct timespec*, size_t); | 
|  | 39 | extern "C" int __timer_create(clockid_t, sigevent*, __kernel_timer_t*); | 
|  | 40 | extern "C" int __timer_delete(__kernel_timer_t); | 
|  | 41 | extern "C" int __timer_getoverrun(__kernel_timer_t); | 
|  | 42 | extern "C" int __timer_gettime(__kernel_timer_t, itimerspec*); | 
|  | 43 | extern "C" int __timer_settime(__kernel_timer_t, int, const itimerspec*, itimerspec*); | 
|  | 44 |  | 
|  | 45 | // Most POSIX timers are handled directly by the kernel. We translate SIGEV_THREAD timers | 
|  | 46 | // into SIGEV_THREAD_ID timers so the kernel handles all the time-related stuff and we just | 
|  | 47 | // need to worry about running user code on a thread. | 
|  | 48 |  | 
|  | 49 | // We can't use SIGALRM because too many other C library functions throw that around, and since | 
|  | 50 | // they don't send to a specific thread, all threads are eligible to handle the signal and we can | 
|  | 51 | // end up with one of our POSIX timer threads handling it (meaning that the intended recipient | 
|  | 52 | // doesn't). glibc uses SIGRTMIN for its POSIX timer implementation, so in the absence of any | 
|  | 53 | // reason to use anything else, we use that too. | 
| Elliott Hughes | 0990d4f | 2014-04-30 09:45:40 -0700 | [diff] [blame] | 54 | static const int TIMER_SIGNAL = (__SIGRTMIN + 0); | 
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 55 |  | 
|  | 56 | struct PosixTimer { | 
|  | 57 | __kernel_timer_t kernel_timer_id; | 
|  | 58 |  | 
|  | 59 | int sigev_notify; | 
|  | 60 |  | 
|  | 61 | // These fields are only needed for a SIGEV_THREAD timer. | 
|  | 62 | pthread_t callback_thread; | 
|  | 63 | void (*callback)(sigval_t); | 
|  | 64 | sigval_t callback_argument; | 
| Christopher Ferris | 62d84b1 | 2014-10-20 19:09:19 -0700 | [diff] [blame] | 65 | volatile bool armed; | 
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 66 | }; | 
|  | 67 |  | 
|  | 68 | static __kernel_timer_t to_kernel_timer_id(timer_t timer) { | 
|  | 69 | return reinterpret_cast<PosixTimer*>(timer)->kernel_timer_id; | 
|  | 70 | } | 
|  | 71 |  | 
|  | 72 | static void* __timer_thread_start(void* arg) { | 
|  | 73 | PosixTimer* timer = reinterpret_cast<PosixTimer*>(arg); | 
|  | 74 |  | 
|  | 75 | kernel_sigset_t sigset; | 
|  | 76 | sigaddset(sigset.get(), TIMER_SIGNAL); | 
|  | 77 |  | 
|  | 78 | while (true) { | 
|  | 79 | // Wait for a signal... | 
|  | 80 | siginfo_t si; | 
|  | 81 | memset(&si, 0, sizeof(si)); | 
|  | 82 | int rc = __rt_sigtimedwait(sigset.get(), &si, NULL, sizeof(sigset)); | 
|  | 83 | if (rc == -1) { | 
|  | 84 | continue; | 
|  | 85 | } | 
|  | 86 |  | 
| Christopher Ferris | 62d84b1 | 2014-10-20 19:09:19 -0700 | [diff] [blame] | 87 | if (si.si_code == SI_TIMER && timer->armed) { | 
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 88 | // This signal was sent because a timer fired, so call the callback. | 
|  | 89 | timer->callback(timer->callback_argument); | 
|  | 90 | } else if (si.si_code == SI_TKILL) { | 
|  | 91 | // This signal was sent because someone wants us to exit. | 
| Elliott Hughes | 473d067 | 2014-04-01 19:07:52 -0700 | [diff] [blame] | 92 | free(timer); | 
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 93 | return NULL; | 
|  | 94 | } | 
|  | 95 | } | 
|  | 96 | } | 
|  | 97 |  | 
|  | 98 | static void __timer_thread_stop(PosixTimer* timer) { | 
| Christopher Ferris | 62d84b1 | 2014-10-20 19:09:19 -0700 | [diff] [blame] | 99 | // Immediately mark the timer as disarmed so even if some events | 
|  | 100 | // continue to happen, the callback won't be called. | 
|  | 101 | timer->armed = false; | 
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 102 | pthread_kill(timer->callback_thread, TIMER_SIGNAL); | 
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 103 | } | 
|  | 104 |  | 
|  | 105 | // http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_create.html | 
|  | 106 | 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] | 107 | PosixTimer* timer = reinterpret_cast<PosixTimer*>(malloc(sizeof(PosixTimer))); | 
|  | 108 | if (timer == NULL) { | 
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 109 | return -1; | 
|  | 110 | } | 
|  | 111 |  | 
| Elliott Hughes | 473d067 | 2014-04-01 19:07:52 -0700 | [diff] [blame] | 112 | timer->sigev_notify = (evp == NULL) ? SIGEV_SIGNAL : evp->sigev_notify; | 
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 113 |  | 
|  | 114 | // 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] | 115 | if (timer->sigev_notify != SIGEV_THREAD) { | 
|  | 116 | if (__timer_create(clock_id, evp, &timer->kernel_timer_id) == -1) { | 
|  | 117 | free(timer); | 
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 118 | return -1; | 
|  | 119 | } | 
|  | 120 |  | 
| Elliott Hughes | 473d067 | 2014-04-01 19:07:52 -0700 | [diff] [blame] | 121 | *timer_id = timer; | 
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 122 | return 0; | 
|  | 123 | } | 
|  | 124 |  | 
|  | 125 | // Otherwise, this must be SIGEV_THREAD timer... | 
| Elliott Hughes | 473d067 | 2014-04-01 19:07:52 -0700 | [diff] [blame] | 126 | timer->callback = evp->sigev_notify_function; | 
|  | 127 | timer->callback_argument = evp->sigev_value; | 
| Christopher Ferris | 62d84b1 | 2014-10-20 19:09:19 -0700 | [diff] [blame] | 128 | timer->armed = false; | 
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 129 |  | 
|  | 130 | // Check arguments that the kernel doesn't care about but we do. | 
| Elliott Hughes | 473d067 | 2014-04-01 19:07:52 -0700 | [diff] [blame] | 131 | if (timer->callback == NULL) { | 
|  | 132 | free(timer); | 
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 133 | errno = EINVAL; | 
|  | 134 | return -1; | 
|  | 135 | } | 
|  | 136 |  | 
|  | 137 | // Create this timer's thread. | 
|  | 138 | pthread_attr_t thread_attributes; | 
|  | 139 | if (evp->sigev_notify_attributes == NULL) { | 
|  | 140 | pthread_attr_init(&thread_attributes); | 
|  | 141 | } else { | 
|  | 142 | thread_attributes = *reinterpret_cast<pthread_attr_t*>(evp->sigev_notify_attributes); | 
|  | 143 | } | 
|  | 144 | pthread_attr_setdetachstate(&thread_attributes, PTHREAD_CREATE_DETACHED); | 
|  | 145 |  | 
|  | 146 | // We start the thread with TIMER_SIGNAL blocked by blocking the signal here and letting it | 
|  | 147 | // inherit. If it tried to block the signal itself, there would be a race. | 
|  | 148 | kernel_sigset_t sigset; | 
|  | 149 | sigaddset(sigset.get(), TIMER_SIGNAL); | 
|  | 150 | kernel_sigset_t old_sigset; | 
|  | 151 | pthread_sigmask(SIG_BLOCK, sigset.get(), old_sigset.get()); | 
|  | 152 |  | 
| Elliott Hughes | 473d067 | 2014-04-01 19:07:52 -0700 | [diff] [blame] | 153 | 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] | 154 |  | 
|  | 155 | pthread_sigmask(SIG_SETMASK, old_sigset.get(), NULL); | 
|  | 156 |  | 
|  | 157 | if (rc != 0) { | 
| Elliott Hughes | 473d067 | 2014-04-01 19:07:52 -0700 | [diff] [blame] | 158 | free(timer); | 
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 159 | errno = rc; | 
|  | 160 | return -1; | 
|  | 161 | } | 
|  | 162 |  | 
|  | 163 | sigevent se = *evp; | 
|  | 164 | se.sigev_signo = TIMER_SIGNAL; | 
|  | 165 | se.sigev_notify = SIGEV_THREAD_ID; | 
| Elliott Hughes | a4831cb | 2014-09-11 16:11:43 -0700 | [diff] [blame] | 166 | se.sigev_notify_thread_id = pthread_gettid_np(timer->callback_thread); | 
| Elliott Hughes | 473d067 | 2014-04-01 19:07:52 -0700 | [diff] [blame] | 167 | if (__timer_create(clock_id, &se, &timer->kernel_timer_id) == -1) { | 
|  | 168 | __timer_thread_stop(timer); | 
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 169 | return -1; | 
|  | 170 | } | 
|  | 171 |  | 
|  | 172 | // Give the thread a meaningful name. | 
|  | 173 | // It can't do this itself because the kernel timer isn't created until after it's running. | 
|  | 174 | char name[32]; | 
| Elliott Hughes | 473d067 | 2014-04-01 19:07:52 -0700 | [diff] [blame] | 175 | snprintf(name, sizeof(name), "POSIX interval timer %d", to_kernel_timer_id(timer)); | 
|  | 176 | pthread_setname_np(timer->callback_thread, name); | 
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 177 |  | 
| Elliott Hughes | 473d067 | 2014-04-01 19:07:52 -0700 | [diff] [blame] | 178 | *timer_id = timer; | 
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 179 | return 0; | 
|  | 180 | } | 
|  | 181 |  | 
|  | 182 | // http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_delete.html | 
|  | 183 | int timer_delete(timer_t id) { | 
|  | 184 | int rc = __timer_delete(to_kernel_timer_id(id)); | 
|  | 185 | if (rc == -1) { | 
|  | 186 | return -1; | 
|  | 187 | } | 
|  | 188 |  | 
|  | 189 | PosixTimer* timer = reinterpret_cast<PosixTimer*>(id); | 
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 190 | if (timer->sigev_notify == SIGEV_THREAD) { | 
| Elliott Hughes | 473d067 | 2014-04-01 19:07:52 -0700 | [diff] [blame] | 191 | // 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] | 192 | __timer_thread_stop(timer); | 
| Elliott Hughes | 473d067 | 2014-04-01 19:07:52 -0700 | [diff] [blame] | 193 | } else { | 
|  | 194 | // For timers without threads, we can just free right away. | 
|  | 195 | free(timer); | 
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 196 | } | 
|  | 197 |  | 
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 198 | return 0; | 
|  | 199 | } | 
|  | 200 |  | 
|  | 201 | // http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_getoverrun.html | 
|  | 202 | int timer_gettime(timer_t id, itimerspec* ts) { | 
|  | 203 | return __timer_gettime(to_kernel_timer_id(id), ts); | 
|  | 204 | } | 
|  | 205 |  | 
|  | 206 | // http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_getoverrun.html | 
|  | 207 | 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] | 208 | PosixTimer* timer= reinterpret_cast<PosixTimer*>(id); | 
|  | 209 | int rc = __timer_settime(timer->kernel_timer_id, flags, ts, ots); | 
|  | 210 | if (rc == 0) { | 
|  | 211 | // Mark the timer as either being armed or disarmed. This avoids the | 
|  | 212 | // callback being called after the disarm for SIGEV_THREAD timers only. | 
|  | 213 | if (ts->it_value.tv_sec != 0 || ts->it_value.tv_nsec != 0) { | 
|  | 214 | timer->armed = true; | 
|  | 215 | } else { | 
|  | 216 | timer->armed = false; | 
|  | 217 | } | 
|  | 218 | } | 
|  | 219 | return rc; | 
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 220 | } | 
|  | 221 |  | 
|  | 222 | // http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_getoverrun.html | 
|  | 223 | int timer_getoverrun(timer_t id) { | 
|  | 224 | return __timer_getoverrun(to_kernel_timer_id(id)); | 
|  | 225 | } |