blob: 2edfe97019e129dbe6fc74a76798804a9f12d49b [file] [log] [blame]
Elliott Hughes4b558f52014-03-04 15:58:02 -08001/*
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 Hughes4b558f52014-03-04 15:58:02 -080029#include <errno.h>
Elliott Hughes05fc1d72015-01-28 18:02:33 -080030#include <malloc.h>
Yabin Cui95f1ee22015-01-13 19:53:15 -080031#include <pthread.h>
32#include <stdatomic.h>
Elliott Hughes4b558f52014-03-04 15:58:02 -080033#include <stdio.h>
34#include <string.h>
Yabin Cui95f1ee22015-01-13 19:53:15 -080035#include <time.h>
Elliott Hughes4b558f52014-03-04 15:58:02 -080036
37// System calls.
Elliott Hughes4b558f52014-03-04 15:58:02 -080038extern "C" int __timer_create(clockid_t, sigevent*, __kernel_timer_t*);
39extern "C" int __timer_delete(__kernel_timer_t);
40extern "C" int __timer_getoverrun(__kernel_timer_t);
41extern "C" int __timer_gettime(__kernel_timer_t, itimerspec*);
42extern "C" int __timer_settime(__kernel_timer_t, int, const itimerspec*, itimerspec*);
43
44// Most POSIX timers are handled directly by the kernel. We translate SIGEV_THREAD timers
45// into SIGEV_THREAD_ID timers so the kernel handles all the time-related stuff and we just
46// need to worry about running user code on a thread.
47
48// We can't use SIGALRM because too many other C library functions throw that around, and since
49// they don't send to a specific thread, all threads are eligible to handle the signal and we can
50// end up with one of our POSIX timer threads handling it (meaning that the intended recipient
51// doesn't). glibc uses SIGRTMIN for its POSIX timer implementation, so in the absence of any
52// reason to use anything else, we use that too.
Elliott Hughes0990d4f2014-04-30 09:45:40 -070053static const int TIMER_SIGNAL = (__SIGRTMIN + 0);
Elliott Hughes4b558f52014-03-04 15:58:02 -080054
55struct PosixTimer {
56 __kernel_timer_t kernel_timer_id;
57
58 int sigev_notify;
59
Yabin Cui95f1ee22015-01-13 19:53:15 -080060 // The fields below are only needed for a SIGEV_THREAD timer.
Elliott Hughes4b558f52014-03-04 15:58:02 -080061 pthread_t callback_thread;
62 void (*callback)(sigval_t);
63 sigval_t callback_argument;
Yabin Cui95f1ee22015-01-13 19:53:15 -080064 atomic_bool deleted; // Set when the timer is deleted, to prevent further calling of callback.
Elliott Hughes4b558f52014-03-04 15:58:02 -080065};
66
67static __kernel_timer_t to_kernel_timer_id(timer_t timer) {
68 return reinterpret_cast<PosixTimer*>(timer)->kernel_timer_id;
69}
70
71static void* __timer_thread_start(void* arg) {
72 PosixTimer* timer = reinterpret_cast<PosixTimer*>(arg);
73
Elliott Hughes5905d6f2018-01-30 15:09:51 -080074 sigset64_t sigset = {};
75 sigaddset64(&sigset, TIMER_SIGNAL);
Elliott Hughes4b558f52014-03-04 15:58:02 -080076
77 while (true) {
78 // Wait for a signal...
Elliott Hughes5905d6f2018-01-30 15:09:51 -080079 siginfo_t si = {};
80 if (sigtimedwait64(&sigset, &si, nullptr) == -1) continue;
Elliott Hughes4b558f52014-03-04 15:58:02 -080081
Yabin Cui95f1ee22015-01-13 19:53:15 -080082 if (si.si_code == SI_TIMER) {
Elliott Hughes4b558f52014-03-04 15:58:02 -080083 // This signal was sent because a timer fired, so call the callback.
Yabin Cui95f1ee22015-01-13 19:53:15 -080084
85 // All events to the callback will be ignored when the timer is deleted.
86 if (atomic_load(&timer->deleted) == true) {
87 continue;
88 }
Elliott Hughes4b558f52014-03-04 15:58:02 -080089 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 Hughes473d0672014-04-01 19:07:52 -070092 free(timer);
Elliott Hughes4b558f52014-03-04 15:58:02 -080093 return NULL;
94 }
95 }
96}
97
98static void __timer_thread_stop(PosixTimer* timer) {
Yabin Cui95f1ee22015-01-13 19:53:15 -080099 atomic_store(&timer->deleted, true);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800100 pthread_kill(timer->callback_thread, TIMER_SIGNAL);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800101}
102
103// http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_create.html
104int timer_create(clockid_t clock_id, sigevent* evp, timer_t* timer_id) {
Elliott Hughes473d0672014-04-01 19:07:52 -0700105 PosixTimer* timer = reinterpret_cast<PosixTimer*>(malloc(sizeof(PosixTimer)));
106 if (timer == NULL) {
Elliott Hughes4b558f52014-03-04 15:58:02 -0800107 return -1;
108 }
109
Elliott Hughes473d0672014-04-01 19:07:52 -0700110 timer->sigev_notify = (evp == NULL) ? SIGEV_SIGNAL : evp->sigev_notify;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800111
112 // If not a SIGEV_THREAD timer, the kernel can handle it without our help.
Elliott Hughes473d0672014-04-01 19:07:52 -0700113 if (timer->sigev_notify != SIGEV_THREAD) {
114 if (__timer_create(clock_id, evp, &timer->kernel_timer_id) == -1) {
115 free(timer);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800116 return -1;
117 }
118
Elliott Hughes473d0672014-04-01 19:07:52 -0700119 *timer_id = timer;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800120 return 0;
121 }
122
123 // Otherwise, this must be SIGEV_THREAD timer...
Elliott Hughes473d0672014-04-01 19:07:52 -0700124 timer->callback = evp->sigev_notify_function;
125 timer->callback_argument = evp->sigev_value;
Yabin Cui95f1ee22015-01-13 19:53:15 -0800126 atomic_init(&timer->deleted, false);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800127
128 // Check arguments that the kernel doesn't care about but we do.
Elliott Hughes473d0672014-04-01 19:07:52 -0700129 if (timer->callback == NULL) {
130 free(timer);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800131 errno = EINVAL;
132 return -1;
133 }
134
135 // Create this timer's thread.
136 pthread_attr_t thread_attributes;
137 if (evp->sigev_notify_attributes == NULL) {
138 pthread_attr_init(&thread_attributes);
139 } else {
140 thread_attributes = *reinterpret_cast<pthread_attr_t*>(evp->sigev_notify_attributes);
141 }
142 pthread_attr_setdetachstate(&thread_attributes, PTHREAD_CREATE_DETACHED);
143
144 // We start the thread with TIMER_SIGNAL blocked by blocking the signal here and letting it
145 // inherit. If it tried to block the signal itself, there would be a race.
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800146 sigset64_t sigset = {};
147 sigaddset64(&sigset, TIMER_SIGNAL);
148 sigset64_t old_sigset;
149 sigprocmask64(SIG_BLOCK, &sigset, &old_sigset);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800150
Elliott Hughes473d0672014-04-01 19:07:52 -0700151 int rc = pthread_create(&timer->callback_thread, &thread_attributes, __timer_thread_start, timer);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800152
Elliott Hughes5905d6f2018-01-30 15:09:51 -0800153 sigprocmask64(SIG_SETMASK, &old_sigset, nullptr);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800154
155 if (rc != 0) {
Elliott Hughes473d0672014-04-01 19:07:52 -0700156 free(timer);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800157 errno = rc;
158 return -1;
159 }
160
161 sigevent se = *evp;
162 se.sigev_signo = TIMER_SIGNAL;
163 se.sigev_notify = SIGEV_THREAD_ID;
Elliott Hughesa4831cb2014-09-11 16:11:43 -0700164 se.sigev_notify_thread_id = pthread_gettid_np(timer->callback_thread);
Elliott Hughes473d0672014-04-01 19:07:52 -0700165 if (__timer_create(clock_id, &se, &timer->kernel_timer_id) == -1) {
166 __timer_thread_stop(timer);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800167 return -1;
168 }
169
Elliott Hughesd1aea302015-04-25 10:05:24 -0700170 // Give the thread a specific meaningful name.
Elliott Hughes4b558f52014-03-04 15:58:02 -0800171 // It can't do this itself because the kernel timer isn't created until after it's running.
Elliott Hughesd1aea302015-04-25 10:05:24 -0700172 char name[16]; // 16 is the kernel-imposed limit.
173 snprintf(name, sizeof(name), "POSIX timer %d", to_kernel_timer_id(timer));
Elliott Hughes473d0672014-04-01 19:07:52 -0700174 pthread_setname_np(timer->callback_thread, name);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800175
Elliott Hughes473d0672014-04-01 19:07:52 -0700176 *timer_id = timer;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800177 return 0;
178}
179
180// http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_delete.html
181int timer_delete(timer_t id) {
182 int rc = __timer_delete(to_kernel_timer_id(id));
183 if (rc == -1) {
184 return -1;
185 }
186
187 PosixTimer* timer = reinterpret_cast<PosixTimer*>(id);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800188 if (timer->sigev_notify == SIGEV_THREAD) {
Elliott Hughes473d0672014-04-01 19:07:52 -0700189 // Stopping the timer's thread frees the timer data when it's safe.
Elliott Hughes4b558f52014-03-04 15:58:02 -0800190 __timer_thread_stop(timer);
Elliott Hughes473d0672014-04-01 19:07:52 -0700191 } else {
192 // For timers without threads, we can just free right away.
193 free(timer);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800194 }
195
Elliott Hughes4b558f52014-03-04 15:58:02 -0800196 return 0;
197}
198
Yabin Cui95f1ee22015-01-13 19:53:15 -0800199// http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_gettime.html
Elliott Hughes4b558f52014-03-04 15:58:02 -0800200int timer_gettime(timer_t id, itimerspec* ts) {
201 return __timer_gettime(to_kernel_timer_id(id), ts);
202}
203
Yabin Cui95f1ee22015-01-13 19:53:15 -0800204// http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_settime.html
205// When using timer_settime to disarm a repeatable SIGEV_THREAD timer with a very small
206// period (like below 1ms), the kernel may continue to send events to the callback thread
207// for a few extra times. This behavior is fine because in POSIX standard: The effect of
208// disarming or resetting a timer with pending expiration notifications is unspecified.
Elliott Hughes4b558f52014-03-04 15:58:02 -0800209int timer_settime(timer_t id, int flags, const itimerspec* ts, itimerspec* ots) {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700210 PosixTimer* timer= reinterpret_cast<PosixTimer*>(id);
Yabin Cui95f1ee22015-01-13 19:53:15 -0800211 return __timer_settime(timer->kernel_timer_id, flags, ts, ots);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800212}
213
214// http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_getoverrun.html
215int timer_getoverrun(timer_t id) {
216 return __timer_getoverrun(to_kernel_timer_id(id));
217}