blob: 999157371fc5c1808b33d0fce409360b19a9efa2 [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
29#include "pthread_internal.h"
30#include "private/bionic_futex.h"
Elliott Hughes4b558f52014-03-04 15:58:02 -080031#include "private/kernel_sigset_t.h"
32
33#include <errno.h>
Elliott Hughes05fc1d72015-01-28 18:02:33 -080034#include <malloc.h>
Elliott Hughes4b558f52014-03-04 15:58:02 -080035#include <stdio.h>
36#include <string.h>
37
38// System calls.
39extern "C" int __rt_sigtimedwait(const sigset_t*, siginfo_t*, const struct timespec*, size_t);
40extern "C" int __timer_create(clockid_t, sigevent*, __kernel_timer_t*);
41extern "C" int __timer_delete(__kernel_timer_t);
42extern "C" int __timer_getoverrun(__kernel_timer_t);
43extern "C" int __timer_gettime(__kernel_timer_t, itimerspec*);
44extern "C" int __timer_settime(__kernel_timer_t, int, const itimerspec*, itimerspec*);
45
46// Most POSIX timers are handled directly by the kernel. We translate SIGEV_THREAD timers
47// into SIGEV_THREAD_ID timers so the kernel handles all the time-related stuff and we just
48// need to worry about running user code on a thread.
49
50// We can't use SIGALRM because too many other C library functions throw that around, and since
51// they don't send to a specific thread, all threads are eligible to handle the signal and we can
52// end up with one of our POSIX timer threads handling it (meaning that the intended recipient
53// doesn't). glibc uses SIGRTMIN for its POSIX timer implementation, so in the absence of any
54// reason to use anything else, we use that too.
Elliott Hughes0990d4f2014-04-30 09:45:40 -070055static const int TIMER_SIGNAL = (__SIGRTMIN + 0);
Elliott Hughes4b558f52014-03-04 15:58:02 -080056
57struct PosixTimer {
58 __kernel_timer_t kernel_timer_id;
59
60 int sigev_notify;
61
62 // These fields are only needed for a SIGEV_THREAD timer.
63 pthread_t callback_thread;
64 void (*callback)(sigval_t);
65 sigval_t callback_argument;
Christopher Ferris62d84b12014-10-20 19:09:19 -070066 volatile bool armed;
Elliott Hughes4b558f52014-03-04 15:58:02 -080067};
68
69static __kernel_timer_t to_kernel_timer_id(timer_t timer) {
70 return reinterpret_cast<PosixTimer*>(timer)->kernel_timer_id;
71}
72
73static void* __timer_thread_start(void* arg) {
74 PosixTimer* timer = reinterpret_cast<PosixTimer*>(arg);
75
76 kernel_sigset_t sigset;
77 sigaddset(sigset.get(), TIMER_SIGNAL);
78
79 while (true) {
80 // Wait for a signal...
81 siginfo_t si;
82 memset(&si, 0, sizeof(si));
83 int rc = __rt_sigtimedwait(sigset.get(), &si, NULL, sizeof(sigset));
84 if (rc == -1) {
85 continue;
86 }
87
Christopher Ferris62d84b12014-10-20 19:09:19 -070088 if (si.si_code == SI_TIMER && timer->armed) {
Elliott Hughes4b558f52014-03-04 15:58:02 -080089 // This signal was sent because a timer fired, so call the callback.
90 timer->callback(timer->callback_argument);
91 } else if (si.si_code == SI_TKILL) {
92 // This signal was sent because someone wants us to exit.
Elliott Hughes473d0672014-04-01 19:07:52 -070093 free(timer);
Elliott Hughes4b558f52014-03-04 15:58:02 -080094 return NULL;
95 }
96 }
97}
98
99static void __timer_thread_stop(PosixTimer* timer) {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700100 // Immediately mark the timer as disarmed so even if some events
101 // continue to happen, the callback won't be called.
102 timer->armed = false;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800103 pthread_kill(timer->callback_thread, TIMER_SIGNAL);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800104}
105
106// http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_create.html
107int timer_create(clockid_t clock_id, sigevent* evp, timer_t* timer_id) {
Elliott Hughes473d0672014-04-01 19:07:52 -0700108 PosixTimer* timer = reinterpret_cast<PosixTimer*>(malloc(sizeof(PosixTimer)));
109 if (timer == NULL) {
Elliott Hughes4b558f52014-03-04 15:58:02 -0800110 return -1;
111 }
112
Elliott Hughes473d0672014-04-01 19:07:52 -0700113 timer->sigev_notify = (evp == NULL) ? SIGEV_SIGNAL : evp->sigev_notify;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800114
115 // If not a SIGEV_THREAD timer, the kernel can handle it without our help.
Elliott Hughes473d0672014-04-01 19:07:52 -0700116 if (timer->sigev_notify != SIGEV_THREAD) {
117 if (__timer_create(clock_id, evp, &timer->kernel_timer_id) == -1) {
118 free(timer);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800119 return -1;
120 }
121
Elliott Hughes473d0672014-04-01 19:07:52 -0700122 *timer_id = timer;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800123 return 0;
124 }
125
126 // Otherwise, this must be SIGEV_THREAD timer...
Elliott Hughes473d0672014-04-01 19:07:52 -0700127 timer->callback = evp->sigev_notify_function;
128 timer->callback_argument = evp->sigev_value;
Christopher Ferris62d84b12014-10-20 19:09:19 -0700129 timer->armed = false;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800130
131 // Check arguments that the kernel doesn't care about but we do.
Elliott Hughes473d0672014-04-01 19:07:52 -0700132 if (timer->callback == NULL) {
133 free(timer);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800134 errno = EINVAL;
135 return -1;
136 }
137
138 // Create this timer's thread.
139 pthread_attr_t thread_attributes;
140 if (evp->sigev_notify_attributes == NULL) {
141 pthread_attr_init(&thread_attributes);
142 } else {
143 thread_attributes = *reinterpret_cast<pthread_attr_t*>(evp->sigev_notify_attributes);
144 }
145 pthread_attr_setdetachstate(&thread_attributes, PTHREAD_CREATE_DETACHED);
146
147 // We start the thread with TIMER_SIGNAL blocked by blocking the signal here and letting it
148 // inherit. If it tried to block the signal itself, there would be a race.
149 kernel_sigset_t sigset;
150 sigaddset(sigset.get(), TIMER_SIGNAL);
151 kernel_sigset_t old_sigset;
152 pthread_sigmask(SIG_BLOCK, sigset.get(), old_sigset.get());
153
Elliott Hughes473d0672014-04-01 19:07:52 -0700154 int rc = pthread_create(&timer->callback_thread, &thread_attributes, __timer_thread_start, timer);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800155
156 pthread_sigmask(SIG_SETMASK, old_sigset.get(), NULL);
157
158 if (rc != 0) {
Elliott Hughes473d0672014-04-01 19:07:52 -0700159 free(timer);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800160 errno = rc;
161 return -1;
162 }
163
164 sigevent se = *evp;
165 se.sigev_signo = TIMER_SIGNAL;
166 se.sigev_notify = SIGEV_THREAD_ID;
Elliott Hughesa4831cb2014-09-11 16:11:43 -0700167 se.sigev_notify_thread_id = pthread_gettid_np(timer->callback_thread);
Elliott Hughes473d0672014-04-01 19:07:52 -0700168 if (__timer_create(clock_id, &se, &timer->kernel_timer_id) == -1) {
169 __timer_thread_stop(timer);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800170 return -1;
171 }
172
173 // Give the thread a meaningful name.
174 // It can't do this itself because the kernel timer isn't created until after it's running.
175 char name[32];
Elliott Hughes473d0672014-04-01 19:07:52 -0700176 snprintf(name, sizeof(name), "POSIX interval timer %d", to_kernel_timer_id(timer));
177 pthread_setname_np(timer->callback_thread, name);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800178
Elliott Hughes473d0672014-04-01 19:07:52 -0700179 *timer_id = timer;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800180 return 0;
181}
182
183// http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_delete.html
184int timer_delete(timer_t id) {
185 int rc = __timer_delete(to_kernel_timer_id(id));
186 if (rc == -1) {
187 return -1;
188 }
189
190 PosixTimer* timer = reinterpret_cast<PosixTimer*>(id);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800191 if (timer->sigev_notify == SIGEV_THREAD) {
Elliott Hughes473d0672014-04-01 19:07:52 -0700192 // Stopping the timer's thread frees the timer data when it's safe.
Elliott Hughes4b558f52014-03-04 15:58:02 -0800193 __timer_thread_stop(timer);
Elliott Hughes473d0672014-04-01 19:07:52 -0700194 } else {
195 // For timers without threads, we can just free right away.
196 free(timer);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800197 }
198
Elliott Hughes4b558f52014-03-04 15:58:02 -0800199 return 0;
200}
201
202// http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_getoverrun.html
203int timer_gettime(timer_t id, itimerspec* ts) {
204 return __timer_gettime(to_kernel_timer_id(id), ts);
205}
206
207// http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_getoverrun.html
208int timer_settime(timer_t id, int flags, const itimerspec* ts, itimerspec* ots) {
Christopher Ferris62d84b12014-10-20 19:09:19 -0700209 PosixTimer* timer= reinterpret_cast<PosixTimer*>(id);
210 int rc = __timer_settime(timer->kernel_timer_id, flags, ts, ots);
211 if (rc == 0) {
212 // Mark the timer as either being armed or disarmed. This avoids the
213 // callback being called after the disarm for SIGEV_THREAD timers only.
214 if (ts->it_value.tv_sec != 0 || ts->it_value.tv_nsec != 0) {
215 timer->armed = true;
216 } else {
217 timer->armed = false;
218 }
219 }
220 return rc;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800221}
222
223// http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_getoverrun.html
224int timer_getoverrun(timer_t id) {
225 return __timer_getoverrun(to_kernel_timer_id(id));
226}