blob: 59933ec78d90871288ae9f46b2c6f0ccebdbfa32 [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;
Elliott Hughes4b558f52014-03-04 15:58:02 -080066};
67
68static __kernel_timer_t to_kernel_timer_id(timer_t timer) {
69 return reinterpret_cast<PosixTimer*>(timer)->kernel_timer_id;
70}
71
72static 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
87 if (si.si_code == SI_TIMER) {
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 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) {
99 pthread_kill(timer->callback_thread, TIMER_SIGNAL);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800100}
101
102// http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_create.html
103int timer_create(clockid_t clock_id, sigevent* evp, timer_t* timer_id) {
Elliott Hughes473d0672014-04-01 19:07:52 -0700104 PosixTimer* timer = reinterpret_cast<PosixTimer*>(malloc(sizeof(PosixTimer)));
105 if (timer == NULL) {
Elliott Hughes4b558f52014-03-04 15:58:02 -0800106 return -1;
107 }
108
Elliott Hughes473d0672014-04-01 19:07:52 -0700109 timer->sigev_notify = (evp == NULL) ? SIGEV_SIGNAL : evp->sigev_notify;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800110
111 // If not a SIGEV_THREAD timer, the kernel can handle it without our help.
Elliott Hughes473d0672014-04-01 19:07:52 -0700112 if (timer->sigev_notify != SIGEV_THREAD) {
113 if (__timer_create(clock_id, evp, &timer->kernel_timer_id) == -1) {
114 free(timer);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800115 return -1;
116 }
117
Elliott Hughes473d0672014-04-01 19:07:52 -0700118 *timer_id = timer;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800119 return 0;
120 }
121
122 // Otherwise, this must be SIGEV_THREAD timer...
Elliott Hughes473d0672014-04-01 19:07:52 -0700123 timer->callback = evp->sigev_notify_function;
124 timer->callback_argument = evp->sigev_value;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800125
126 // Check arguments that the kernel doesn't care about but we do.
Elliott Hughes473d0672014-04-01 19:07:52 -0700127 if (timer->callback == NULL) {
128 free(timer);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800129 errno = EINVAL;
130 return -1;
131 }
132
133 // Create this timer's thread.
134 pthread_attr_t thread_attributes;
135 if (evp->sigev_notify_attributes == NULL) {
136 pthread_attr_init(&thread_attributes);
137 } else {
138 thread_attributes = *reinterpret_cast<pthread_attr_t*>(evp->sigev_notify_attributes);
139 }
140 pthread_attr_setdetachstate(&thread_attributes, PTHREAD_CREATE_DETACHED);
141
142 // We start the thread with TIMER_SIGNAL blocked by blocking the signal here and letting it
143 // inherit. If it tried to block the signal itself, there would be a race.
144 kernel_sigset_t sigset;
145 sigaddset(sigset.get(), TIMER_SIGNAL);
146 kernel_sigset_t old_sigset;
147 pthread_sigmask(SIG_BLOCK, sigset.get(), old_sigset.get());
148
Elliott Hughes473d0672014-04-01 19:07:52 -0700149 int rc = pthread_create(&timer->callback_thread, &thread_attributes, __timer_thread_start, timer);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800150
151 pthread_sigmask(SIG_SETMASK, old_sigset.get(), NULL);
152
153 if (rc != 0) {
Elliott Hughes473d0672014-04-01 19:07:52 -0700154 free(timer);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800155 errno = rc;
156 return -1;
157 }
158
159 sigevent se = *evp;
160 se.sigev_signo = TIMER_SIGNAL;
161 se.sigev_notify = SIGEV_THREAD_ID;
Elliott Hughes473d0672014-04-01 19:07:52 -0700162 se.sigev_notify_thread_id = __pthread_gettid(timer->callback_thread);
163 if (__timer_create(clock_id, &se, &timer->kernel_timer_id) == -1) {
164 __timer_thread_stop(timer);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800165 return -1;
166 }
167
168 // Give the thread a meaningful name.
169 // It can't do this itself because the kernel timer isn't created until after it's running.
170 char name[32];
Elliott Hughes473d0672014-04-01 19:07:52 -0700171 snprintf(name, sizeof(name), "POSIX interval timer %d", to_kernel_timer_id(timer));
172 pthread_setname_np(timer->callback_thread, name);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800173
Elliott Hughes473d0672014-04-01 19:07:52 -0700174 *timer_id = timer;
Elliott Hughes4b558f52014-03-04 15:58:02 -0800175 return 0;
176}
177
178// http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_delete.html
179int timer_delete(timer_t id) {
180 int rc = __timer_delete(to_kernel_timer_id(id));
181 if (rc == -1) {
182 return -1;
183 }
184
185 PosixTimer* timer = reinterpret_cast<PosixTimer*>(id);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800186 if (timer->sigev_notify == SIGEV_THREAD) {
Elliott Hughes473d0672014-04-01 19:07:52 -0700187 // Stopping the timer's thread frees the timer data when it's safe.
Elliott Hughes4b558f52014-03-04 15:58:02 -0800188 __timer_thread_stop(timer);
Elliott Hughes473d0672014-04-01 19:07:52 -0700189 } else {
190 // For timers without threads, we can just free right away.
191 free(timer);
Elliott Hughes4b558f52014-03-04 15:58:02 -0800192 }
193
Elliott Hughes4b558f52014-03-04 15:58:02 -0800194 return 0;
195}
196
197// http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_getoverrun.html
198int timer_gettime(timer_t id, itimerspec* ts) {
199 return __timer_gettime(to_kernel_timer_id(id), ts);
200}
201
202// http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_getoverrun.html
203int timer_settime(timer_t id, int flags, const itimerspec* ts, itimerspec* ots) {
204 return __timer_settime(to_kernel_timer_id(id), flags, ts, ots);
205}
206
207// http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_getoverrun.html
208int timer_getoverrun(timer_t id) {
209 return __timer_getoverrun(to_kernel_timer_id(id));
210}