blob: ffe213c3302c1eea1ec0575e45860ac9ad7df52c [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"
31#include "private/bionic_pthread.h"
32#include "private/kernel_sigset_t.h"
33
34#include <errno.h>
35#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.
55static const int TIMER_SIGNAL = SIGRTMIN;
56
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;
66 volatile int exiting;
67};
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
88 if (si.si_code == SI_TIMER) {
89 // 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.
93 timer->exiting = 1;
94 __futex_wake(&timer->exiting, INT32_MAX);
95 return NULL;
96 }
97 }
98}
99
100static void __timer_thread_stop(PosixTimer* timer) {
101 pthread_kill(timer->callback_thread, TIMER_SIGNAL);
102
103 // We can't pthread_join because POSIX says "the threads created in response to a timer
104 // expiration are created detached, or in an unspecified way if the thread attribute's
105 // detachstate is PTHREAD_CREATE_JOINABLE".
106 while (timer->exiting == 0) {
107 __futex_wait(&timer->exiting, 0, NULL);
108 }
109}
110
111// http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_create.html
112int timer_create(clockid_t clock_id, sigevent* evp, timer_t* timer_id) {
113 PosixTimer* new_timer = reinterpret_cast<PosixTimer*>(malloc(sizeof(PosixTimer)));
114 if (new_timer == NULL) {
115 return -1;
116 }
117
118 new_timer->sigev_notify = (evp == NULL) ? SIGEV_SIGNAL : evp->sigev_notify;
119
120 // If not a SIGEV_THREAD timer, the kernel can handle it without our help.
121 if (new_timer->sigev_notify != SIGEV_THREAD) {
122 if (__timer_create(clock_id, evp, &new_timer->kernel_timer_id) == -1) {
123 free(new_timer);
124 return -1;
125 }
126
127 *timer_id = new_timer;
128 return 0;
129 }
130
131 // Otherwise, this must be SIGEV_THREAD timer...
132 new_timer->callback = evp->sigev_notify_function;
133 new_timer->callback_argument = evp->sigev_value;
134 new_timer->exiting = 0;
135
136 // Check arguments that the kernel doesn't care about but we do.
137 if (new_timer->callback == NULL) {
138 free(new_timer);
139 errno = EINVAL;
140 return -1;
141 }
142
143 // Create this timer's thread.
144 pthread_attr_t thread_attributes;
145 if (evp->sigev_notify_attributes == NULL) {
146 pthread_attr_init(&thread_attributes);
147 } else {
148 thread_attributes = *reinterpret_cast<pthread_attr_t*>(evp->sigev_notify_attributes);
149 }
150 pthread_attr_setdetachstate(&thread_attributes, PTHREAD_CREATE_DETACHED);
151
152 // We start the thread with TIMER_SIGNAL blocked by blocking the signal here and letting it
153 // inherit. If it tried to block the signal itself, there would be a race.
154 kernel_sigset_t sigset;
155 sigaddset(sigset.get(), TIMER_SIGNAL);
156 kernel_sigset_t old_sigset;
157 pthread_sigmask(SIG_BLOCK, sigset.get(), old_sigset.get());
158
159 int rc = pthread_create(&new_timer->callback_thread, &thread_attributes, __timer_thread_start, new_timer);
160
161 pthread_sigmask(SIG_SETMASK, old_sigset.get(), NULL);
162
163 if (rc != 0) {
164 free(new_timer);
165 errno = rc;
166 return -1;
167 }
168
169 sigevent se = *evp;
170 se.sigev_signo = TIMER_SIGNAL;
171 se.sigev_notify = SIGEV_THREAD_ID;
172 se.sigev_notify_thread_id = __pthread_gettid(new_timer->callback_thread);
173 if (__timer_create(clock_id, &se, &new_timer->kernel_timer_id) == -1) {
174 __timer_thread_stop(new_timer);
175 free(new_timer);
176 return -1;
177 }
178
179 // Give the thread a meaningful name.
180 // It can't do this itself because the kernel timer isn't created until after it's running.
181 char name[32];
182 snprintf(name, sizeof(name), "POSIX interval timer %d", to_kernel_timer_id(new_timer));
183 pthread_setname_np(new_timer->callback_thread, name);
184
185 *timer_id = new_timer;
186 return 0;
187}
188
189// http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_delete.html
190int timer_delete(timer_t id) {
191 int rc = __timer_delete(to_kernel_timer_id(id));
192 if (rc == -1) {
193 return -1;
194 }
195
196 PosixTimer* timer = reinterpret_cast<PosixTimer*>(id);
197
198 // Make sure the timer's thread has exited before we free the timer data.
199 if (timer->sigev_notify == SIGEV_THREAD) {
200 __timer_thread_stop(timer);
201 }
202
203 free(timer);
204
205 return 0;
206}
207
208// http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_getoverrun.html
209int timer_gettime(timer_t id, itimerspec* ts) {
210 return __timer_gettime(to_kernel_timer_id(id), ts);
211}
212
213// http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_getoverrun.html
214int timer_settime(timer_t id, int flags, const itimerspec* ts, itimerspec* ots) {
215 return __timer_settime(to_kernel_timer_id(id), flags, ts, ots);
216}
217
218// http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_getoverrun.html
219int timer_getoverrun(timer_t id) {
220 return __timer_getoverrun(to_kernel_timer_id(id));
221}