blob: 32ff81ac684cc1aa38581483a686e3004e587874 [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>
Elliott Hughesc3f11402013-10-30 14:40:09 -070033#include <sys/mman.h>
Narayan Kamath51e6cb32014-03-03 15:38:51 +000034#include <time.h>
Elliott Hughesc3f11402013-10-30 14:40:09 -070035#include <unistd.h>
36
37#include "pthread_internal.h"
38
39#include "private/bionic_atomic_inline.h"
40#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
Elliott Hughesc3f11402013-10-30 14:40:09 -0700101
102// XXX *technically* there is a race condition that could allow
103// XXX a signal to be missed. If thread A is preempted in _wait()
104// XXX after unlocking the mutex and before waiting, and if other
105// XXX threads call signal or broadcast UINT_MAX/2 times (exactly),
106// XXX before thread A is scheduled again and calls futex_wait(),
107// XXX then the signal will be lost.
108
109int pthread_cond_init(pthread_cond_t* cond, const pthread_condattr_t* attr) {
Narayan Kamath51e6cb32014-03-03 15:38:51 +0000110 if (attr != NULL) {
111 cond->value = (*attr & COND_FLAGS_MASK);
112 } else {
113 cond->value = 0;
Elliott Hughesc3f11402013-10-30 14:40:09 -0700114 }
115
116 return 0;
117}
118
119int pthread_cond_destroy(pthread_cond_t* cond) {
Elliott Hughesc3f11402013-10-30 14:40:09 -0700120 cond->value = 0xdeadc04d;
121 return 0;
122}
123
124// This function is used by pthread_cond_broadcast and
125// pthread_cond_signal to atomically decrement the counter
126// then wake up 'counter' threads.
127static int __pthread_cond_pulse(pthread_cond_t* cond, int counter) {
Narayan Kamath51e6cb32014-03-03 15:38:51 +0000128 int flags = (cond->value & COND_FLAGS_MASK);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700129 while (true) {
Narayan Kamath51e6cb32014-03-03 15:38:51 +0000130 int old_value = cond->value;
131 int new_value = ((old_value - COND_COUNTER_STEP) & COND_COUNTER_MASK) | flags;
Elliott Hughesc3f11402013-10-30 14:40:09 -0700132 if (__bionic_cmpxchg(old_value, new_value, &cond->value) == 0) {
133 break;
134 }
135 }
136
137 // Ensure that all memory accesses previously made by this thread are
138 // visible to the woken thread(s). On the other side, the "wait"
139 // code will issue any necessary barriers when locking the mutex.
140 //
141 // This may not strictly be necessary -- if the caller follows
142 // recommended practice and holds the mutex before signaling the cond
143 // var, the mutex ops will provide correct semantics. If they don't
144 // hold the mutex, they're subject to race conditions anyway.
145 ANDROID_MEMBAR_FULL();
146
Narayan Kamath51e6cb32014-03-03 15:38:51 +0000147 __futex_wake_ex(&cond->value, COND_IS_SHARED(cond->value), counter);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700148 return 0;
149}
150
151__LIBC_HIDDEN__
152int __pthread_cond_timedwait_relative(pthread_cond_t* cond, pthread_mutex_t* mutex, const timespec* reltime) {
153 int old_value = cond->value;
154
155 pthread_mutex_unlock(mutex);
Narayan Kamath51e6cb32014-03-03 15:38:51 +0000156 int status = __futex_wait_ex(&cond->value, COND_IS_SHARED(cond->value), old_value, reltime);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700157 pthread_mutex_lock(mutex);
158
Elliott Hughes9e79af32013-12-18 10:05:42 -0800159 if (status == -ETIMEDOUT) {
Elliott Hughesc3f11402013-10-30 14:40:09 -0700160 return ETIMEDOUT;
161 }
162 return 0;
163}
164
165__LIBC_HIDDEN__
Elliott Hughes04303f52014-09-18 16:11:59 -0700166int __pthread_cond_timedwait(pthread_cond_t* cond, pthread_mutex_t* mutex, const timespec* abs_ts, clockid_t clock) {
Elliott Hughesc3f11402013-10-30 14:40:09 -0700167 timespec ts;
168 timespec* tsp;
169
Elliott Hughes04303f52014-09-18 16:11:59 -0700170 if (abs_ts != NULL) {
171 if (!timespec_from_absolute_timespec(ts, *abs_ts, clock)) {
Elliott Hughesc3f11402013-10-30 14:40:09 -0700172 return ETIMEDOUT;
173 }
174 tsp = &ts;
175 } else {
176 tsp = NULL;
177 }
178
179 return __pthread_cond_timedwait_relative(cond, mutex, tsp);
180}
181
182int pthread_cond_broadcast(pthread_cond_t* cond) {
183 return __pthread_cond_pulse(cond, INT_MAX);
184}
185
186int pthread_cond_signal(pthread_cond_t* cond) {
187 return __pthread_cond_pulse(cond, 1);
188}
189
190int pthread_cond_wait(pthread_cond_t* cond, pthread_mutex_t* mutex) {
Narayan Kamath51e6cb32014-03-03 15:38:51 +0000191 return __pthread_cond_timedwait(cond, mutex, NULL, COND_GET_CLOCK(cond->value));
Elliott Hughesc3f11402013-10-30 14:40:09 -0700192}
193
194int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t * mutex, const timespec *abstime) {
Narayan Kamath51e6cb32014-03-03 15:38:51 +0000195 return __pthread_cond_timedwait(cond, mutex, abstime, COND_GET_CLOCK(cond->value));
Elliott Hughesc3f11402013-10-30 14:40:09 -0700196}
197
Narayan Kamath51e6cb32014-03-03 15:38:51 +0000198#if !defined(__LP64__)
199// TODO: this exists only for backward binary compatibility on 32 bit platforms.
200extern "C" int pthread_cond_timedwait_monotonic(pthread_cond_t* cond, pthread_mutex_t* mutex, const timespec* abstime) {
Elliott Hughesc3f11402013-10-30 14:40:09 -0700201 return __pthread_cond_timedwait(cond, mutex, abstime, CLOCK_MONOTONIC);
202}
203
Narayan Kamath51e6cb32014-03-03 15:38:51 +0000204extern "C" int pthread_cond_timedwait_monotonic_np(pthread_cond_t* cond, pthread_mutex_t* mutex, const timespec* abstime) {
Elliott Hughesc3f11402013-10-30 14:40:09 -0700205 return __pthread_cond_timedwait(cond, mutex, abstime, CLOCK_MONOTONIC);
206}
Elliott Hughesc3f11402013-10-30 14:40:09 -0700207
Halton Huof0870c32014-02-21 18:05:29 +0800208extern "C" int pthread_cond_timedwait_relative_np(pthread_cond_t* cond, pthread_mutex_t* mutex, const timespec* reltime) {
Elliott Hughesc3f11402013-10-30 14:40:09 -0700209 return __pthread_cond_timedwait_relative(cond, mutex, reltime);
210}
211
Halton Huof0870c32014-02-21 18:05:29 +0800212extern "C" int pthread_cond_timeout_np(pthread_cond_t* cond, pthread_mutex_t* mutex, unsigned ms) {
Elliott Hughesc3f11402013-10-30 14:40:09 -0700213 timespec ts;
214 timespec_from_ms(ts, ms);
215 return __pthread_cond_timedwait_relative(cond, mutex, &ts);
216}
Halton Huof0870c32014-02-21 18:05:29 +0800217#endif // !defined(__LP64__)