pthread_cond.cpp revision e5f816c01780220880ee59a29f727c48b51365d3
1/*
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>
33#include <stdatomic.h>
34#include <sys/mman.h>
35#include <time.h>
36#include <unistd.h>
37
38#include "pthread_internal.h"
39
40#include "private/bionic_futex.h"
41#include "private/bionic_time_conversions.h"
42#include "private/bionic_tls.h"
43
44// 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
61int pthread_condattr_init(pthread_condattr_t* attr) {
62  *attr = 0;
63  *attr |= PTHREAD_PROCESS_PRIVATE;
64  *attr |= (CLOCK_REALTIME << 1);
65  return 0;
66}
67
68int pthread_condattr_getpshared(const pthread_condattr_t* attr, int* pshared) {
69  *pshared = static_cast<int>(COND_IS_SHARED(*attr));
70  return 0;
71}
72
73int pthread_condattr_setpshared(pthread_condattr_t* attr, int pshared) {
74  if (pshared != PTHREAD_PROCESS_SHARED && pshared != PTHREAD_PROCESS_PRIVATE) {
75    return EINVAL;
76  }
77
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);
93  return 0;
94}
95
96int pthread_condattr_destroy(pthread_condattr_t* attr) {
97  *attr = 0xdeada11d;
98  return 0;
99}
100
101static 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}
109
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) {
118  atomic_uint* cond_value_ptr = COND_TO_ATOMIC_POINTER(cond);
119  unsigned int init_value = 0;
120
121  if (attr != NULL) {
122    init_value = (*attr & COND_FLAGS_MASK);
123  }
124  atomic_init(cond_value_ptr, init_value);
125
126  return 0;
127}
128
129int pthread_cond_destroy(pthread_cond_t* cond) {
130  atomic_uint* cond_value_ptr = COND_TO_ATOMIC_POINTER(cond);
131  atomic_store_explicit(cond_value_ptr, 0xdeadc04d, memory_order_relaxed);
132  return 0;
133}
134
135// This function is used by pthread_cond_broadcast and
136// pthread_cond_signal to atomically decrement the counter
137// 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);
141
142  // 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.
150
151  // 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);
155  return 0;
156}
157
158__LIBC_HIDDEN__
159int __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);
163
164  pthread_mutex_unlock(mutex);
165  int status = __futex_wait_ex(cond_value_ptr, shared, old_value, reltime);
166  pthread_mutex_lock(mutex);
167
168  if (status == -ETIMEDOUT) {
169    return ETIMEDOUT;
170  }
171  return 0;
172}
173
174__LIBC_HIDDEN__
175int __pthread_cond_timedwait(atomic_uint* cond_value_ptr, pthread_mutex_t* mutex,
176                             const timespec* abs_ts, clockid_t clock) {
177  timespec ts;
178  timespec* tsp;
179
180  if (abs_ts != NULL) {
181    if (!timespec_from_absolute_timespec(ts, *abs_ts, clock)) {
182      return ETIMEDOUT;
183    }
184    tsp = &ts;
185  } else {
186    tsp = NULL;
187  }
188
189  return __pthread_cond_timedwait_relative(cond_value_ptr, mutex, tsp);
190}
191
192int pthread_cond_broadcast(pthread_cond_t* cond) {
193  atomic_uint* cond_value_ptr = COND_TO_ATOMIC_POINTER(cond);
194  return __pthread_cond_pulse(cond_value_ptr, INT_MAX);
195}
196
197int pthread_cond_signal(pthread_cond_t* cond) {
198  atomic_uint* cond_value_ptr = COND_TO_ATOMIC_POINTER(cond);
199  return __pthread_cond_pulse(cond_value_ptr, 1);
200}
201
202int pthread_cond_wait(pthread_cond_t* cond, pthread_mutex_t* mutex) {
203  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)));
206}
207
208int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t * mutex, const timespec *abstime) {
209  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)));
212}
213
214#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) {
217  atomic_uint* cond_value_ptr = COND_TO_ATOMIC_POINTER(cond);
218  return __pthread_cond_timedwait(cond_value_ptr, mutex, abstime, CLOCK_MONOTONIC);
219}
220
221extern "C" int pthread_cond_timedwait_monotonic_np(pthread_cond_t* cond, pthread_mutex_t* mutex, const timespec* abstime) {
222  atomic_uint* cond_value_ptr = COND_TO_ATOMIC_POINTER(cond);
223  return __pthread_cond_timedwait(cond_value_ptr, mutex, abstime, CLOCK_MONOTONIC);
224}
225
226extern "C" int pthread_cond_timedwait_relative_np(pthread_cond_t* cond, pthread_mutex_t* mutex, const timespec* reltime) {
227  atomic_uint* cond_value_ptr = COND_TO_ATOMIC_POINTER(cond);
228  return __pthread_cond_timedwait_relative(cond_value_ptr, mutex, reltime);
229}
230
231extern "C" int pthread_cond_timeout_np(pthread_cond_t* cond, pthread_mutex_t* mutex, unsigned ms) {
232  timespec ts;
233  timespec_from_ms(ts, ms);
234  atomic_uint* cond_value_ptr = COND_TO_ATOMIC_POINTER(cond);
235  return __pthread_cond_timedwait_relative(cond_value_ptr, mutex, &ts);
236}
237#endif // !defined(__LP64__)
238