pthread_cond.cpp revision 0e714a5b41451e84c5ded93a42c9a4b0a9440691
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 <sys/atomics.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_atomic_inline.h"
41#include "private/bionic_futex.h"
42#include "private/bionic_time_conversions.h"
43#include "private/bionic_tls.h"
44#include "private/thread_private.h"
45
46// We use one bit in pthread_condattr_t (long) values as the 'shared' flag
47// and one bit for the clock type (CLOCK_REALTIME is ((clockid_t) 1), and
48// CLOCK_MONOTONIC is ((clockid_t) 0).). The rest of the bits are a counter.
49//
50// The 'value' field pthread_cond_t has the same layout.
51
52#define COND_SHARED_MASK 0x0001
53#define COND_CLOCK_MASK 0x0002
54#define COND_COUNTER_STEP 0x0004
55#define COND_FLAGS_MASK (COND_SHARED_MASK | COND_CLOCK_MASK)
56#define COND_COUNTER_MASK (~COND_FLAGS_MASK)
57
58#define COND_IS_SHARED(c) (((c) & COND_SHARED_MASK) != 0)
59#define COND_GET_CLOCK(c) (((c) & COND_CLOCK_MASK) >> 1)
60#define COND_SET_CLOCK(attr, c) ((attr) | (c << 1))
61
62
63int pthread_condattr_init(pthread_condattr_t* attr) {
64  *attr = 0;
65  *attr |= PTHREAD_PROCESS_PRIVATE;
66  *attr |= (CLOCK_REALTIME << 1);
67  return 0;
68}
69
70int pthread_condattr_getpshared(const pthread_condattr_t* attr, int* pshared) {
71  *pshared = static_cast<int>(COND_IS_SHARED(*attr));
72  return 0;
73}
74
75int pthread_condattr_setpshared(pthread_condattr_t* attr, int pshared) {
76  if (pshared != PTHREAD_PROCESS_SHARED && pshared != PTHREAD_PROCESS_PRIVATE) {
77    return EINVAL;
78  }
79
80  *attr |= pshared;
81  return 0;
82}
83
84int pthread_condattr_getclock(const pthread_condattr_t* attr, clockid_t* clock) {
85  *clock = COND_GET_CLOCK(*attr);
86  return 0;
87}
88
89int pthread_condattr_setclock(pthread_condattr_t* attr, clockid_t clock) {
90  if (clock != CLOCK_MONOTONIC && clock != CLOCK_REALTIME) {
91    return EINVAL;
92  }
93
94  *attr = COND_SET_CLOCK(*attr, clock);
95  return 0;
96}
97
98int pthread_condattr_destroy(pthread_condattr_t* attr) {
99  if (attr == NULL) {
100    return EINVAL;
101  }
102  *attr = 0xdeada11d;
103  return 0;
104}
105
106
107// XXX *technically* there is a race condition that could allow
108// XXX a signal to be missed.  If thread A is preempted in _wait()
109// XXX after unlocking the mutex and before waiting, and if other
110// XXX threads call signal or broadcast UINT_MAX/2 times (exactly),
111// XXX before thread A is scheduled again and calls futex_wait(),
112// XXX then the signal will be lost.
113
114int pthread_cond_init(pthread_cond_t* cond, const pthread_condattr_t* attr) {
115  if (cond == NULL) {
116    return EINVAL;
117  }
118
119  if (attr != NULL) {
120    cond->value = (*attr & COND_FLAGS_MASK);
121  } else {
122    cond->value = 0;
123  }
124
125  return 0;
126}
127
128int pthread_cond_destroy(pthread_cond_t* cond) {
129  if (cond == NULL) {
130    return EINVAL;
131  }
132
133  cond->value = 0xdeadc04d;
134  return 0;
135}
136
137// This function is used by pthread_cond_broadcast and
138// pthread_cond_signal to atomically decrement the counter
139// then wake up 'counter' threads.
140static int __pthread_cond_pulse(pthread_cond_t* cond, int counter) {
141  if (__predict_false(cond == NULL)) {
142    return EINVAL;
143  }
144
145  int flags = (cond->value & COND_FLAGS_MASK);
146  while (true) {
147    int old_value = cond->value;
148    int new_value = ((old_value - COND_COUNTER_STEP) & COND_COUNTER_MASK) | flags;
149    if (__bionic_cmpxchg(old_value, new_value, &cond->value) == 0) {
150      break;
151    }
152  }
153
154  // Ensure that all memory accesses previously made by this thread are
155  // visible to the woken thread(s).  On the other side, the "wait"
156  // code will issue any necessary barriers when locking the mutex.
157  //
158  // This may not strictly be necessary -- if the caller follows
159  // recommended practice and holds the mutex before signaling the cond
160  // var, the mutex ops will provide correct semantics.  If they don't
161  // hold the mutex, they're subject to race conditions anyway.
162  ANDROID_MEMBAR_FULL();
163
164  __futex_wake_ex(&cond->value, COND_IS_SHARED(cond->value), counter);
165  return 0;
166}
167
168__LIBC_HIDDEN__
169int __pthread_cond_timedwait_relative(pthread_cond_t* cond, pthread_mutex_t* mutex, const timespec* reltime) {
170  int old_value = cond->value;
171
172  pthread_mutex_unlock(mutex);
173  int status = __futex_wait_ex(&cond->value, COND_IS_SHARED(cond->value), old_value, reltime);
174  pthread_mutex_lock(mutex);
175
176  if (status == -ETIMEDOUT) {
177    return ETIMEDOUT;
178  }
179  return 0;
180}
181
182__LIBC_HIDDEN__
183int __pthread_cond_timedwait(pthread_cond_t* cond, pthread_mutex_t* mutex, const timespec* abstime, clockid_t clock) {
184  timespec ts;
185  timespec* tsp;
186
187  if (abstime != NULL) {
188    if (__timespec_from_absolute(&ts, abstime, clock) < 0) {
189      return ETIMEDOUT;
190    }
191    tsp = &ts;
192  } else {
193    tsp = NULL;
194  }
195
196  return __pthread_cond_timedwait_relative(cond, mutex, tsp);
197}
198
199int pthread_cond_broadcast(pthread_cond_t* cond) {
200  return __pthread_cond_pulse(cond, INT_MAX);
201}
202
203int pthread_cond_signal(pthread_cond_t* cond) {
204  return __pthread_cond_pulse(cond, 1);
205}
206
207int pthread_cond_wait(pthread_cond_t* cond, pthread_mutex_t* mutex) {
208  return __pthread_cond_timedwait(cond, mutex, NULL, COND_GET_CLOCK(cond->value));
209}
210
211int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t * mutex, const timespec *abstime) {
212  return __pthread_cond_timedwait(cond, mutex, abstime, COND_GET_CLOCK(cond->value));
213}
214
215#if !defined(__LP64__)
216// TODO: this exists only for backward binary compatibility on 32 bit platforms.
217extern "C" int pthread_cond_timedwait_monotonic(pthread_cond_t* cond, pthread_mutex_t* mutex, const timespec* abstime) {
218  return __pthread_cond_timedwait(cond, 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  return __pthread_cond_timedwait(cond, mutex, abstime, CLOCK_MONOTONIC);
223}
224#endif // !defined(__LP64__)
225
226int pthread_cond_timedwait_relative_np(pthread_cond_t* cond, pthread_mutex_t* mutex, const timespec* reltime) {
227  return __pthread_cond_timedwait_relative(cond, mutex, reltime);
228}
229
230int pthread_cond_timeout_np(pthread_cond_t* cond, pthread_mutex_t* mutex, unsigned ms) {
231  timespec ts;
232  timespec_from_ms(ts, ms);
233  return __pthread_cond_timedwait_relative(cond, mutex, &ts);
234}
235