blob: 95a433c113224408bfe49b2762b13c913998fbac [file] [log] [blame]
Elliott Hughesc3f11402013-10-30 14:40:09 -07001/*
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.h>
30
31#include <errno.h>
32#include <limits.h>
Yabin Cuie5f816c2015-01-29 12:13:33 -080033#include <stdatomic.h>
Elliott Hughesc3f11402013-10-30 14:40:09 -070034#include <sys/mman.h>
Narayan Kamath51e6cb32014-03-03 15:38:51 +000035#include <time.h>
Elliott Hughesc3f11402013-10-30 14:40:09 -070036#include <unistd.h>
37
38#include "pthread_internal.h"
39
Elliott Hughesc3f11402013-10-30 14:40:09 -070040#include "private/bionic_futex.h"
Elliott Hughesc3f11402013-10-30 14:40:09 -070041#include "private/bionic_time_conversions.h"
42#include "private/bionic_tls.h"
Elliott Hughesc3f11402013-10-30 14:40:09 -070043
Yabin Cui32651b82015-03-13 20:30:00 -070044// XXX *technically* there is a race condition that could allow
45// XXX a signal to be missed. If thread A is preempted in _wait()
46// XXX after unlocking the mutex and before waiting, and if other
47// XXX threads call signal or broadcast UINT_MAX/2 times (exactly),
48// XXX before thread A is scheduled again and calls futex_wait(),
49// XXX then the signal will be lost.
50
Narayan Kamath51e6cb32014-03-03 15:38:51 +000051// We use one bit in pthread_condattr_t (long) values as the 'shared' flag
52// and one bit for the clock type (CLOCK_REALTIME is ((clockid_t) 1), and
53// CLOCK_MONOTONIC is ((clockid_t) 0).). The rest of the bits are a counter.
54//
55// The 'value' field pthread_cond_t has the same layout.
56
57#define COND_SHARED_MASK 0x0001
58#define COND_CLOCK_MASK 0x0002
59#define COND_COUNTER_STEP 0x0004
60#define COND_FLAGS_MASK (COND_SHARED_MASK | COND_CLOCK_MASK)
61#define COND_COUNTER_MASK (~COND_FLAGS_MASK)
62
63#define COND_IS_SHARED(c) (((c) & COND_SHARED_MASK) != 0)
64#define COND_GET_CLOCK(c) (((c) & COND_CLOCK_MASK) >> 1)
65#define COND_SET_CLOCK(attr, c) ((attr) | (c << 1))
66
Elliott Hughesc3f11402013-10-30 14:40:09 -070067int pthread_condattr_init(pthread_condattr_t* attr) {
Narayan Kamath51e6cb32014-03-03 15:38:51 +000068 *attr = 0;
69 *attr |= PTHREAD_PROCESS_PRIVATE;
70 *attr |= (CLOCK_REALTIME << 1);
Elliott Hughesc3f11402013-10-30 14:40:09 -070071 return 0;
72}
73
74int pthread_condattr_getpshared(const pthread_condattr_t* attr, int* pshared) {
Narayan Kamath51e6cb32014-03-03 15:38:51 +000075 *pshared = static_cast<int>(COND_IS_SHARED(*attr));
Elliott Hughesc3f11402013-10-30 14:40:09 -070076 return 0;
77}
78
79int pthread_condattr_setpshared(pthread_condattr_t* attr, int pshared) {
Elliott Hughesc3f11402013-10-30 14:40:09 -070080 if (pshared != PTHREAD_PROCESS_SHARED && pshared != PTHREAD_PROCESS_PRIVATE) {
81 return EINVAL;
82 }
Narayan Kamath51e6cb32014-03-03 15:38:51 +000083
84 *attr |= pshared;
85 return 0;
86}
87
88int pthread_condattr_getclock(const pthread_condattr_t* attr, clockid_t* clock) {
89 *clock = COND_GET_CLOCK(*attr);
90 return 0;
91}
92
93int pthread_condattr_setclock(pthread_condattr_t* attr, clockid_t clock) {
94 if (clock != CLOCK_MONOTONIC && clock != CLOCK_REALTIME) {
95 return EINVAL;
96 }
97
98 *attr = COND_SET_CLOCK(*attr, clock);
Elliott Hughesc3f11402013-10-30 14:40:09 -070099 return 0;
100}
101
102int pthread_condattr_destroy(pthread_condattr_t* attr) {
Elliott Hughesc3f11402013-10-30 14:40:09 -0700103 *attr = 0xdeada11d;
104 return 0;
105}
106
Yabin Cui32651b82015-03-13 20:30:00 -0700107struct pthread_cond_internal_t {
108 atomic_uint state;
Yabin Cuie5f816c2015-01-29 12:13:33 -0800109
Yabin Cui9e6c7bc2015-03-16 14:26:53 -0700110 bool process_shared() {
Yabin Cui32651b82015-03-13 20:30:00 -0700111 return COND_IS_SHARED(atomic_load_explicit(&state, memory_order_relaxed));
112 }
113
Yabin Cui9e6c7bc2015-03-16 14:26:53 -0700114 int get_clock() {
Yabin Cui32651b82015-03-13 20:30:00 -0700115 return COND_GET_CLOCK(atomic_load_explicit(&state, memory_order_relaxed));
116 }
117
118#if defined(__LP64__)
119 char __reserved[44];
120#endif
121};
122
123static pthread_cond_internal_t* __get_internal_cond(pthread_cond_t* cond_interface) {
124 static_assert(sizeof(pthread_cond_t) == sizeof(pthread_cond_internal_t),
125 "pthread_cond_t should actually be pthread_cond_internal_t in implementation.");
126 return reinterpret_cast<pthread_cond_internal_t*>(cond_interface);
Yabin Cuie5f816c2015-01-29 12:13:33 -0800127}
Elliott Hughesc3f11402013-10-30 14:40:09 -0700128
Yabin Cui32651b82015-03-13 20:30:00 -0700129int pthread_cond_init(pthread_cond_t* cond_interface, const pthread_condattr_t* attr) {
130 pthread_cond_internal_t* cond = __get_internal_cond(cond_interface);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700131
Yabin Cui32651b82015-03-13 20:30:00 -0700132 unsigned int init_state = 0;
Narayan Kamath51e6cb32014-03-03 15:38:51 +0000133 if (attr != NULL) {
Yabin Cui32651b82015-03-13 20:30:00 -0700134 init_state = (*attr & COND_FLAGS_MASK);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700135 }
Yabin Cui32651b82015-03-13 20:30:00 -0700136 atomic_init(&cond->state, init_state);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700137
138 return 0;
139}
140
Yabin Cui32651b82015-03-13 20:30:00 -0700141int pthread_cond_destroy(pthread_cond_t* cond_interface) {
142 pthread_cond_internal_t* cond = __get_internal_cond(cond_interface);
143 atomic_store_explicit(&cond->state, 0xdeadc04d, memory_order_relaxed);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700144 return 0;
145}
146
147// This function is used by pthread_cond_broadcast and
148// pthread_cond_signal to atomically decrement the counter
Yabin Cuie5f816c2015-01-29 12:13:33 -0800149// then wake up thread_count threads.
Yabin Cui32651b82015-03-13 20:30:00 -0700150static int __pthread_cond_pulse(pthread_cond_internal_t* cond, int thread_count) {
Yabin Cuie5f816c2015-01-29 12:13:33 -0800151 // We don't use a release/seq_cst fence here. Because pthread_cond_wait/signal can't be
152 // used as a method for memory synchronization by itself. It should always be used with
153 // pthread mutexes. Note that Spurious wakeups from pthread_cond_wait/timedwait may occur,
154 // so when using condition variables there is always a boolean predicate involving shared
155 // variables associated with each condition wait that is true if the thread should proceed.
156 // If the predicate is seen true before a condition wait, pthread_cond_wait/timedwait will
157 // not be called. That's why pthread_wait/signal pair can't be used as a method for memory
158 // synchronization. And it doesn't help even if we use any fence here.
Elliott Hughesc3f11402013-10-30 14:40:09 -0700159
Yabin Cuie5f816c2015-01-29 12:13:33 -0800160 // The increase of value should leave flags alone, even if the value can overflows.
Yabin Cui32651b82015-03-13 20:30:00 -0700161 atomic_fetch_add_explicit(&cond->state, COND_COUNTER_STEP, memory_order_relaxed);
Yabin Cuie5f816c2015-01-29 12:13:33 -0800162
Yabin Cui32651b82015-03-13 20:30:00 -0700163 __futex_wake_ex(&cond->state, cond->process_shared(), thread_count);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700164 return 0;
165}
166
Yabin Cui32651b82015-03-13 20:30:00 -0700167static int __pthread_cond_timedwait_relative(pthread_cond_internal_t* cond, pthread_mutex_t* mutex,
168 const timespec* rel_timeout_or_null) {
169 unsigned int old_state = atomic_load_explicit(&cond->state, memory_order_relaxed);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700170
171 pthread_mutex_unlock(mutex);
Yabin Cui32651b82015-03-13 20:30:00 -0700172 int status = __futex_wait_ex(&cond->state, cond->process_shared(), old_state, rel_timeout_or_null);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700173 pthread_mutex_lock(mutex);
174
Elliott Hughes9e79af32013-12-18 10:05:42 -0800175 if (status == -ETIMEDOUT) {
Elliott Hughesc3f11402013-10-30 14:40:09 -0700176 return ETIMEDOUT;
177 }
178 return 0;
179}
180
Yabin Cui32651b82015-03-13 20:30:00 -0700181static int __pthread_cond_timedwait(pthread_cond_internal_t* cond, pthread_mutex_t* mutex,
182 const timespec* abs_timeout_or_null, clockid_t clock) {
Elliott Hughesc3f11402013-10-30 14:40:09 -0700183 timespec ts;
Yabin Cui32651b82015-03-13 20:30:00 -0700184 timespec* rel_timeout = NULL;
Elliott Hughesc3f11402013-10-30 14:40:09 -0700185
Yabin Cui32651b82015-03-13 20:30:00 -0700186 if (abs_timeout_or_null != NULL) {
187 rel_timeout = &ts;
188 if (!timespec_from_absolute_timespec(*rel_timeout, *abs_timeout_or_null, clock)) {
Elliott Hughesc3f11402013-10-30 14:40:09 -0700189 return ETIMEDOUT;
190 }
Elliott Hughesc3f11402013-10-30 14:40:09 -0700191 }
192
Yabin Cui32651b82015-03-13 20:30:00 -0700193 return __pthread_cond_timedwait_relative(cond, mutex, rel_timeout);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700194}
195
Yabin Cui32651b82015-03-13 20:30:00 -0700196int pthread_cond_broadcast(pthread_cond_t* cond_interface) {
197 return __pthread_cond_pulse(__get_internal_cond(cond_interface), INT_MAX);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700198}
199
Yabin Cui32651b82015-03-13 20:30:00 -0700200int pthread_cond_signal(pthread_cond_t* cond_interface) {
201 return __pthread_cond_pulse(__get_internal_cond(cond_interface), 1);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700202}
203
Yabin Cui32651b82015-03-13 20:30:00 -0700204int pthread_cond_wait(pthread_cond_t* cond_interface, pthread_mutex_t* mutex) {
205 pthread_cond_internal_t* cond = __get_internal_cond(cond_interface);
206 return __pthread_cond_timedwait(cond, mutex, NULL, cond->get_clock());
Elliott Hughesc3f11402013-10-30 14:40:09 -0700207}
208
Yabin Cui32651b82015-03-13 20:30:00 -0700209int pthread_cond_timedwait(pthread_cond_t *cond_interface, pthread_mutex_t * mutex,
210 const timespec *abstime) {
211
212 pthread_cond_internal_t* cond = __get_internal_cond(cond_interface);
213 return __pthread_cond_timedwait(cond, mutex, abstime, cond->get_clock());
Elliott Hughesc3f11402013-10-30 14:40:09 -0700214}
215
Narayan Kamath51e6cb32014-03-03 15:38:51 +0000216#if !defined(__LP64__)
217// TODO: this exists only for backward binary compatibility on 32 bit platforms.
Yabin Cui32651b82015-03-13 20:30:00 -0700218extern "C" int pthread_cond_timedwait_monotonic(pthread_cond_t* cond_interface,
219 pthread_mutex_t* mutex,
220 const timespec* abs_timeout) {
221
222 return __pthread_cond_timedwait(__get_internal_cond(cond_interface), mutex, abs_timeout,
223 CLOCK_MONOTONIC);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700224}
225
Yabin Cui32651b82015-03-13 20:30:00 -0700226extern "C" int pthread_cond_timedwait_monotonic_np(pthread_cond_t* cond_interface,
227 pthread_mutex_t* mutex,
228 const timespec* abs_timeout) {
229 return pthread_cond_timedwait_monotonic(cond_interface, mutex, abs_timeout);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700230}
Elliott Hughesc3f11402013-10-30 14:40:09 -0700231
Yabin Cui32651b82015-03-13 20:30:00 -0700232extern "C" int pthread_cond_timedwait_relative_np(pthread_cond_t* cond_interface,
233 pthread_mutex_t* mutex,
234 const timespec* rel_timeout) {
235
236 return __pthread_cond_timedwait_relative(__get_internal_cond(cond_interface), mutex, rel_timeout);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700237}
238
Yabin Cui32651b82015-03-13 20:30:00 -0700239extern "C" int pthread_cond_timeout_np(pthread_cond_t* cond_interface,
240 pthread_mutex_t* mutex, unsigned ms) {
Elliott Hughesc3f11402013-10-30 14:40:09 -0700241 timespec ts;
242 timespec_from_ms(ts, ms);
Yabin Cui32651b82015-03-13 20:30:00 -0700243 return pthread_cond_timedwait_relative_np(cond_interface, mutex, &ts);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700244}
Halton Huof0870c32014-02-21 18:05:29 +0800245#endif // !defined(__LP64__)