blob: 5542c596586e6fde53077251018ccf73637f770f [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
Narayan Kamath51e6cb32014-03-03 15:38:51 +000044// We use one bit in pthread_condattr_t (long) values as the 'shared' flag
45// and one bit for the clock type (CLOCK_REALTIME is ((clockid_t) 1), and
46// CLOCK_MONOTONIC is ((clockid_t) 0).). The rest of the bits are a counter.
47//
48// The 'value' field pthread_cond_t has the same layout.
49
50#define COND_SHARED_MASK 0x0001
51#define COND_CLOCK_MASK 0x0002
52#define COND_COUNTER_STEP 0x0004
53#define COND_FLAGS_MASK (COND_SHARED_MASK | COND_CLOCK_MASK)
54#define COND_COUNTER_MASK (~COND_FLAGS_MASK)
55
56#define COND_IS_SHARED(c) (((c) & COND_SHARED_MASK) != 0)
57#define COND_GET_CLOCK(c) (((c) & COND_CLOCK_MASK) >> 1)
58#define COND_SET_CLOCK(attr, c) ((attr) | (c << 1))
59
60
Elliott Hughesc3f11402013-10-30 14:40:09 -070061int pthread_condattr_init(pthread_condattr_t* attr) {
Narayan Kamath51e6cb32014-03-03 15:38:51 +000062 *attr = 0;
63 *attr |= PTHREAD_PROCESS_PRIVATE;
64 *attr |= (CLOCK_REALTIME << 1);
Elliott Hughesc3f11402013-10-30 14:40:09 -070065 return 0;
66}
67
68int pthread_condattr_getpshared(const pthread_condattr_t* attr, int* pshared) {
Narayan Kamath51e6cb32014-03-03 15:38:51 +000069 *pshared = static_cast<int>(COND_IS_SHARED(*attr));
Elliott Hughesc3f11402013-10-30 14:40:09 -070070 return 0;
71}
72
73int pthread_condattr_setpshared(pthread_condattr_t* attr, int pshared) {
Elliott Hughesc3f11402013-10-30 14:40:09 -070074 if (pshared != PTHREAD_PROCESS_SHARED && pshared != PTHREAD_PROCESS_PRIVATE) {
75 return EINVAL;
76 }
Narayan Kamath51e6cb32014-03-03 15:38:51 +000077
78 *attr |= pshared;
79 return 0;
80}
81
82int pthread_condattr_getclock(const pthread_condattr_t* attr, clockid_t* clock) {
83 *clock = COND_GET_CLOCK(*attr);
84 return 0;
85}
86
87int pthread_condattr_setclock(pthread_condattr_t* attr, clockid_t clock) {
88 if (clock != CLOCK_MONOTONIC && clock != CLOCK_REALTIME) {
89 return EINVAL;
90 }
91
92 *attr = COND_SET_CLOCK(*attr, clock);
Elliott Hughesc3f11402013-10-30 14:40:09 -070093 return 0;
94}
95
96int pthread_condattr_destroy(pthread_condattr_t* attr) {
Elliott Hughesc3f11402013-10-30 14:40:09 -070097 *attr = 0xdeada11d;
98 return 0;
99}
100
Yabin Cuie5f816c2015-01-29 12:13:33 -0800101static inline atomic_uint* COND_TO_ATOMIC_POINTER(pthread_cond_t* cond) {
102 static_assert(sizeof(atomic_uint) == sizeof(cond->value),
103 "cond->value should actually be atomic_uint in implementation.");
104
105 // We prefer casting to atomic_uint instead of declaring cond->value to be atomic_uint directly.
106 // Because using the second method pollutes pthread.h, and causes an error when compiling libcxx.
107 return reinterpret_cast<atomic_uint*>(&cond->value);
108}
Elliott Hughesc3f11402013-10-30 14:40:09 -0700109
110// XXX *technically* there is a race condition that could allow
111// XXX a signal to be missed. If thread A is preempted in _wait()
112// XXX after unlocking the mutex and before waiting, and if other
113// XXX threads call signal or broadcast UINT_MAX/2 times (exactly),
114// XXX before thread A is scheduled again and calls futex_wait(),
115// XXX then the signal will be lost.
116
117int pthread_cond_init(pthread_cond_t* cond, const pthread_condattr_t* attr) {
Yabin Cuie5f816c2015-01-29 12:13:33 -0800118 atomic_uint* cond_value_ptr = COND_TO_ATOMIC_POINTER(cond);
119 unsigned int init_value = 0;
120
Narayan Kamath51e6cb32014-03-03 15:38:51 +0000121 if (attr != NULL) {
Yabin Cuie5f816c2015-01-29 12:13:33 -0800122 init_value = (*attr & COND_FLAGS_MASK);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700123 }
Yabin Cuie5f816c2015-01-29 12:13:33 -0800124 atomic_init(cond_value_ptr, init_value);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700125
126 return 0;
127}
128
129int pthread_cond_destroy(pthread_cond_t* cond) {
Yabin Cuie5f816c2015-01-29 12:13:33 -0800130 atomic_uint* cond_value_ptr = COND_TO_ATOMIC_POINTER(cond);
131 atomic_store_explicit(cond_value_ptr, 0xdeadc04d, memory_order_relaxed);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700132 return 0;
133}
134
135// This function is used by pthread_cond_broadcast and
136// pthread_cond_signal to atomically decrement the counter
Yabin Cuie5f816c2015-01-29 12:13:33 -0800137// then wake up thread_count threads.
138static int __pthread_cond_pulse(atomic_uint* cond_value_ptr, int thread_count) {
139 unsigned int old_value = atomic_load_explicit(cond_value_ptr, memory_order_relaxed);
140 bool shared = COND_IS_SHARED(old_value);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700141
Yabin Cuie5f816c2015-01-29 12:13:33 -0800142 // We don't use a release/seq_cst fence here. Because pthread_cond_wait/signal can't be
143 // used as a method for memory synchronization by itself. It should always be used with
144 // pthread mutexes. Note that Spurious wakeups from pthread_cond_wait/timedwait may occur,
145 // so when using condition variables there is always a boolean predicate involving shared
146 // variables associated with each condition wait that is true if the thread should proceed.
147 // If the predicate is seen true before a condition wait, pthread_cond_wait/timedwait will
148 // not be called. That's why pthread_wait/signal pair can't be used as a method for memory
149 // synchronization. And it doesn't help even if we use any fence here.
Elliott Hughesc3f11402013-10-30 14:40:09 -0700150
Yabin Cuie5f816c2015-01-29 12:13:33 -0800151 // The increase of value should leave flags alone, even if the value can overflows.
152 atomic_fetch_add_explicit(cond_value_ptr, COND_COUNTER_STEP, memory_order_relaxed);
153
154 __futex_wake_ex(cond_value_ptr, shared, thread_count);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700155 return 0;
156}
157
158__LIBC_HIDDEN__
Yabin Cuie5f816c2015-01-29 12:13:33 -0800159int __pthread_cond_timedwait_relative(atomic_uint* cond_value_ptr, pthread_mutex_t* mutex,
160 const timespec* reltime) {
161 unsigned int old_value = atomic_load_explicit(cond_value_ptr, memory_order_relaxed);
162 bool shared = COND_IS_SHARED(old_value);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700163
164 pthread_mutex_unlock(mutex);
Yabin Cuie5f816c2015-01-29 12:13:33 -0800165 int status = __futex_wait_ex(cond_value_ptr, shared, old_value, reltime);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700166 pthread_mutex_lock(mutex);
167
Elliott Hughes9e79af32013-12-18 10:05:42 -0800168 if (status == -ETIMEDOUT) {
Elliott Hughesc3f11402013-10-30 14:40:09 -0700169 return ETIMEDOUT;
170 }
171 return 0;
172}
173
174__LIBC_HIDDEN__
Yabin Cuie5f816c2015-01-29 12:13:33 -0800175int __pthread_cond_timedwait(atomic_uint* cond_value_ptr, pthread_mutex_t* mutex,
176 const timespec* abs_ts, clockid_t clock) {
Elliott Hughesc3f11402013-10-30 14:40:09 -0700177 timespec ts;
178 timespec* tsp;
179
Elliott Hughes04303f52014-09-18 16:11:59 -0700180 if (abs_ts != NULL) {
181 if (!timespec_from_absolute_timespec(ts, *abs_ts, clock)) {
Elliott Hughesc3f11402013-10-30 14:40:09 -0700182 return ETIMEDOUT;
183 }
184 tsp = &ts;
185 } else {
186 tsp = NULL;
187 }
188
Yabin Cuie5f816c2015-01-29 12:13:33 -0800189 return __pthread_cond_timedwait_relative(cond_value_ptr, mutex, tsp);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700190}
191
192int pthread_cond_broadcast(pthread_cond_t* cond) {
Yabin Cuie5f816c2015-01-29 12:13:33 -0800193 atomic_uint* cond_value_ptr = COND_TO_ATOMIC_POINTER(cond);
194 return __pthread_cond_pulse(cond_value_ptr, INT_MAX);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700195}
196
197int pthread_cond_signal(pthread_cond_t* cond) {
Yabin Cuie5f816c2015-01-29 12:13:33 -0800198 atomic_uint* cond_value_ptr = COND_TO_ATOMIC_POINTER(cond);
199 return __pthread_cond_pulse(cond_value_ptr, 1);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700200}
201
202int pthread_cond_wait(pthread_cond_t* cond, pthread_mutex_t* mutex) {
Yabin Cuie5f816c2015-01-29 12:13:33 -0800203 atomic_uint* cond_value_ptr = COND_TO_ATOMIC_POINTER(cond);
204 return __pthread_cond_timedwait(cond_value_ptr, mutex, NULL,
205 COND_GET_CLOCK(atomic_load_explicit(cond_value_ptr, memory_order_relaxed)));
Elliott Hughesc3f11402013-10-30 14:40:09 -0700206}
207
208int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t * mutex, const timespec *abstime) {
Yabin Cuie5f816c2015-01-29 12:13:33 -0800209 atomic_uint* cond_value_ptr = COND_TO_ATOMIC_POINTER(cond);
210 return __pthread_cond_timedwait(cond_value_ptr, mutex, abstime,
211 COND_GET_CLOCK(atomic_load_explicit(cond_value_ptr, memory_order_relaxed)));
Elliott Hughesc3f11402013-10-30 14:40:09 -0700212}
213
Narayan Kamath51e6cb32014-03-03 15:38:51 +0000214#if !defined(__LP64__)
215// TODO: this exists only for backward binary compatibility on 32 bit platforms.
216extern "C" int pthread_cond_timedwait_monotonic(pthread_cond_t* cond, pthread_mutex_t* mutex, const timespec* abstime) {
Yabin Cuie5f816c2015-01-29 12:13:33 -0800217 atomic_uint* cond_value_ptr = COND_TO_ATOMIC_POINTER(cond);
218 return __pthread_cond_timedwait(cond_value_ptr, mutex, abstime, CLOCK_MONOTONIC);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700219}
220
Narayan Kamath51e6cb32014-03-03 15:38:51 +0000221extern "C" int pthread_cond_timedwait_monotonic_np(pthread_cond_t* cond, pthread_mutex_t* mutex, const timespec* abstime) {
Yabin Cuie5f816c2015-01-29 12:13:33 -0800222 atomic_uint* cond_value_ptr = COND_TO_ATOMIC_POINTER(cond);
223 return __pthread_cond_timedwait(cond_value_ptr, mutex, abstime, CLOCK_MONOTONIC);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700224}
Elliott Hughesc3f11402013-10-30 14:40:09 -0700225
Halton Huof0870c32014-02-21 18:05:29 +0800226extern "C" int pthread_cond_timedwait_relative_np(pthread_cond_t* cond, pthread_mutex_t* mutex, const timespec* reltime) {
Yabin Cuie5f816c2015-01-29 12:13:33 -0800227 atomic_uint* cond_value_ptr = COND_TO_ATOMIC_POINTER(cond);
228 return __pthread_cond_timedwait_relative(cond_value_ptr, mutex, reltime);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700229}
230
Halton Huof0870c32014-02-21 18:05:29 +0800231extern "C" int pthread_cond_timeout_np(pthread_cond_t* cond, pthread_mutex_t* mutex, unsigned ms) {
Elliott Hughesc3f11402013-10-30 14:40:09 -0700232 timespec ts;
233 timespec_from_ms(ts, ms);
Yabin Cuie5f816c2015-01-29 12:13:33 -0800234 atomic_uint* cond_value_ptr = COND_TO_ATOMIC_POINTER(cond);
235 return __pthread_cond_timedwait_relative(cond_value_ptr, mutex, &ts);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700236}
Halton Huof0870c32014-02-21 18:05:29 +0800237#endif // !defined(__LP64__)