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 <errno.h> |
Elliott Hughes | 05fc1d7 | 2015-01-28 18:02:33 -0800 | [diff] [blame] | 30 | #include <malloc.h> |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 31 | #include <pthread.h> |
| 32 | #include <stdatomic.h> |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 33 | #include <stdio.h> |
| 34 | #include <string.h> |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 35 | #include <time.h> |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 36 | |
Elliott Hughes | c793bc0 | 2024-05-21 21:35:49 +0000 | [diff] [blame^] | 37 | #include "private/bionic_lock.h" |
| 38 | |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 39 | // System calls. |
Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 40 | extern "C" int __rt_sigprocmask(int, const sigset64_t*, sigset64_t*, size_t); |
| 41 | extern "C" int __rt_sigtimedwait(const sigset64_t*, siginfo_t*, const timespec*, size_t); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 42 | extern "C" int __timer_create(clockid_t, sigevent*, __kernel_timer_t*); |
| 43 | extern "C" int __timer_delete(__kernel_timer_t); |
| 44 | extern "C" int __timer_getoverrun(__kernel_timer_t); |
| 45 | extern "C" int __timer_gettime(__kernel_timer_t, itimerspec*); |
| 46 | extern "C" int __timer_settime(__kernel_timer_t, int, const itimerspec*, itimerspec*); |
| 47 | |
| 48 | // Most POSIX timers are handled directly by the kernel. We translate SIGEV_THREAD timers |
| 49 | // into SIGEV_THREAD_ID timers so the kernel handles all the time-related stuff and we just |
| 50 | // need to worry about running user code on a thread. |
| 51 | |
| 52 | // We can't use SIGALRM because too many other C library functions throw that around, and since |
| 53 | // they don't send to a specific thread, all threads are eligible to handle the signal and we can |
| 54 | // end up with one of our POSIX timer threads handling it (meaning that the intended recipient |
| 55 | // doesn't). glibc uses SIGRTMIN for its POSIX timer implementation, so in the absence of any |
| 56 | // reason to use anything else, we use that too. |
Elliott Hughes | 0990d4f | 2014-04-30 09:45:40 -0700 | [diff] [blame] | 57 | static const int TIMER_SIGNAL = (__SIGRTMIN + 0); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 58 | |
| 59 | struct PosixTimer { |
| 60 | __kernel_timer_t kernel_timer_id; |
| 61 | |
| 62 | int sigev_notify; |
| 63 | |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 64 | // The fields below are only needed for a SIGEV_THREAD timer. |
Elliott Hughes | c793bc0 | 2024-05-21 21:35:49 +0000 | [diff] [blame^] | 65 | Lock startup_handshake_lock; |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 66 | pthread_t callback_thread; |
| 67 | void (*callback)(sigval_t); |
| 68 | sigval_t callback_argument; |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 69 | 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] | 70 | }; |
| 71 | |
| 72 | static __kernel_timer_t to_kernel_timer_id(timer_t timer) { |
| 73 | return reinterpret_cast<PosixTimer*>(timer)->kernel_timer_id; |
| 74 | } |
| 75 | |
| 76 | static void* __timer_thread_start(void* arg) { |
| 77 | PosixTimer* timer = reinterpret_cast<PosixTimer*>(arg); |
| 78 | |
Elliott Hughes | c793bc0 | 2024-05-21 21:35:49 +0000 | [diff] [blame^] | 79 | // Check that our parent managed to create the kernel timer and bail if not... |
| 80 | timer->startup_handshake_lock.lock(); |
| 81 | if (timer->kernel_timer_id == -1) { |
| 82 | free(timer); |
| 83 | return nullptr; |
| 84 | } |
| 85 | |
| 86 | // Give ourselves a specific meaningful name now we have a kernel timer. |
| 87 | char name[16]; // 16 is the kernel-imposed limit. |
| 88 | snprintf(name, sizeof(name), "POSIX timer %d", to_kernel_timer_id(timer)); |
| 89 | pthread_setname_np(timer->callback_thread, name); |
| 90 | |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 91 | sigset64_t sigset = {}; |
| 92 | sigaddset64(&sigset, TIMER_SIGNAL); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 93 | |
| 94 | while (true) { |
| 95 | // Wait for a signal... |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 96 | siginfo_t si = {}; |
Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 97 | if (__rt_sigtimedwait(&sigset, &si, nullptr, sizeof(sigset)) == -1) continue; |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 98 | |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 99 | if (si.si_code == SI_TIMER) { |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 100 | // This signal was sent because a timer fired, so call the callback. |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 101 | |
| 102 | // All events to the callback will be ignored when the timer is deleted. |
| 103 | if (atomic_load(&timer->deleted) == true) { |
| 104 | continue; |
| 105 | } |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 106 | timer->callback(timer->callback_argument); |
| 107 | } else if (si.si_code == SI_TKILL) { |
| 108 | // This signal was sent because someone wants us to exit. |
Elliott Hughes | 473d067 | 2014-04-01 19:07:52 -0700 | [diff] [blame] | 109 | free(timer); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 110 | return nullptr; |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | static void __timer_thread_stop(PosixTimer* timer) { |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 116 | atomic_store(&timer->deleted, true); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 117 | pthread_kill(timer->callback_thread, TIMER_SIGNAL); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | // http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_create.html |
| 121 | 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] | 122 | PosixTimer* timer = reinterpret_cast<PosixTimer*>(malloc(sizeof(PosixTimer))); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 123 | if (timer == nullptr) { |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 124 | return -1; |
| 125 | } |
| 126 | |
Elliott Hughes | c793bc0 | 2024-05-21 21:35:49 +0000 | [diff] [blame^] | 127 | timer->kernel_timer_id = -1; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 128 | timer->sigev_notify = (evp == nullptr) ? SIGEV_SIGNAL : evp->sigev_notify; |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 129 | |
| 130 | // 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] | 131 | if (timer->sigev_notify != SIGEV_THREAD) { |
| 132 | if (__timer_create(clock_id, evp, &timer->kernel_timer_id) == -1) { |
| 133 | free(timer); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 134 | return -1; |
| 135 | } |
| 136 | |
Elliott Hughes | 473d067 | 2014-04-01 19:07:52 -0700 | [diff] [blame] | 137 | *timer_id = timer; |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | // Otherwise, this must be SIGEV_THREAD timer... |
Elliott Hughes | 473d067 | 2014-04-01 19:07:52 -0700 | [diff] [blame] | 142 | timer->callback = evp->sigev_notify_function; |
| 143 | timer->callback_argument = evp->sigev_value; |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 144 | atomic_init(&timer->deleted, false); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 145 | |
| 146 | // Check arguments that the kernel doesn't care about but we do. |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 147 | if (timer->callback == nullptr) { |
Elliott Hughes | 473d067 | 2014-04-01 19:07:52 -0700 | [diff] [blame] | 148 | free(timer); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 149 | errno = EINVAL; |
| 150 | return -1; |
| 151 | } |
| 152 | |
| 153 | // Create this timer's thread. |
| 154 | pthread_attr_t thread_attributes; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 155 | if (evp->sigev_notify_attributes == nullptr) { |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 156 | pthread_attr_init(&thread_attributes); |
| 157 | } else { |
| 158 | thread_attributes = *reinterpret_cast<pthread_attr_t*>(evp->sigev_notify_attributes); |
| 159 | } |
| 160 | pthread_attr_setdetachstate(&thread_attributes, PTHREAD_CREATE_DETACHED); |
| 161 | |
| 162 | // We start the thread with TIMER_SIGNAL blocked by blocking the signal here and letting it |
| 163 | // inherit. If it tried to block the signal itself, there would be a race. |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 164 | sigset64_t sigset = {}; |
| 165 | sigaddset64(&sigset, TIMER_SIGNAL); |
| 166 | sigset64_t old_sigset; |
Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 167 | |
Elliott Hughes | c793bc0 | 2024-05-21 21:35:49 +0000 | [diff] [blame^] | 168 | // Prevent the child thread from running until the timer has been created. |
| 169 | timer->startup_handshake_lock.init(false); |
| 170 | timer->startup_handshake_lock.lock(); |
| 171 | |
Josh Gao | 6fcba93 | 2018-02-09 13:38:32 -0800 | [diff] [blame] | 172 | // Use __rt_sigprocmask instead of sigprocmask64 to avoid filtering out TIMER_SIGNAL. |
| 173 | __rt_sigprocmask(SIG_BLOCK, &sigset, &old_sigset, sizeof(sigset)); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 174 | |
Elliott Hughes | 473d067 | 2014-04-01 19:07:52 -0700 | [diff] [blame] | 175 | 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] | 176 | |
Elliott Hughes | 48a6974 | 2018-03-20 17:58:11 -0700 | [diff] [blame] | 177 | __rt_sigprocmask(SIG_SETMASK, &old_sigset, nullptr, sizeof(old_sigset)); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 178 | |
| 179 | if (rc != 0) { |
Elliott Hughes | 473d067 | 2014-04-01 19:07:52 -0700 | [diff] [blame] | 180 | free(timer); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 181 | errno = rc; |
| 182 | return -1; |
| 183 | } |
| 184 | |
Elliott Hughes | c793bc0 | 2024-05-21 21:35:49 +0000 | [diff] [blame^] | 185 | // Try to create the kernel timer. |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 186 | sigevent se = *evp; |
| 187 | se.sigev_signo = TIMER_SIGNAL; |
| 188 | se.sigev_notify = SIGEV_THREAD_ID; |
Elliott Hughes | a4831cb | 2014-09-11 16:11:43 -0700 | [diff] [blame] | 189 | se.sigev_notify_thread_id = pthread_gettid_np(timer->callback_thread); |
Elliott Hughes | c793bc0 | 2024-05-21 21:35:49 +0000 | [diff] [blame^] | 190 | rc = __timer_create(clock_id, &se, &timer->kernel_timer_id); |
| 191 | |
| 192 | // Let the child run (whether we created the kernel timer or not). |
| 193 | timer->startup_handshake_lock.unlock(); |
| 194 | // If __timer_create(2) failed, the child will kill itself and free the |
| 195 | // timer struct, so we just need to exit. |
| 196 | if (rc == -1) { |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 197 | return -1; |
| 198 | } |
| 199 | |
Elliott Hughes | 473d067 | 2014-04-01 19:07:52 -0700 | [diff] [blame] | 200 | *timer_id = timer; |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | // http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_delete.html |
| 205 | int timer_delete(timer_t id) { |
| 206 | int rc = __timer_delete(to_kernel_timer_id(id)); |
| 207 | if (rc == -1) { |
| 208 | return -1; |
| 209 | } |
| 210 | |
| 211 | PosixTimer* timer = reinterpret_cast<PosixTimer*>(id); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 212 | if (timer->sigev_notify == SIGEV_THREAD) { |
Elliott Hughes | 473d067 | 2014-04-01 19:07:52 -0700 | [diff] [blame] | 213 | // 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] | 214 | __timer_thread_stop(timer); |
Elliott Hughes | 473d067 | 2014-04-01 19:07:52 -0700 | [diff] [blame] | 215 | } else { |
| 216 | // For timers without threads, we can just free right away. |
| 217 | free(timer); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 218 | } |
| 219 | |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 220 | return 0; |
| 221 | } |
| 222 | |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 223 | // http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_gettime.html |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 224 | int timer_gettime(timer_t id, itimerspec* ts) { |
| 225 | return __timer_gettime(to_kernel_timer_id(id), ts); |
| 226 | } |
| 227 | |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 228 | // http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_settime.html |
| 229 | // When using timer_settime to disarm a repeatable SIGEV_THREAD timer with a very small |
| 230 | // period (like below 1ms), the kernel may continue to send events to the callback thread |
| 231 | // for a few extra times. This behavior is fine because in POSIX standard: The effect of |
| 232 | // disarming or resetting a timer with pending expiration notifications is unspecified. |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 233 | 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] | 234 | PosixTimer* timer= reinterpret_cast<PosixTimer*>(id); |
Yabin Cui | 95f1ee2 | 2015-01-13 19:53:15 -0800 | [diff] [blame] | 235 | return __timer_settime(timer->kernel_timer_id, flags, ts, ots); |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | // http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_getoverrun.html |
| 239 | int timer_getoverrun(timer_t id) { |
| 240 | return __timer_getoverrun(to_kernel_timer_id(id)); |
| 241 | } |