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