1c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes/*
2c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * Copyright (C) 2008 The Android Open Source Project
3c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * All rights reserved.
4c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes *
5c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * Redistribution and use in source and binary forms, with or without
6c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * modification, are permitted provided that the following conditions
7c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * are met:
8c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes *  * Redistributions of source code must retain the above copyright
9c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes *    notice, this list of conditions and the following disclaimer.
10c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes *  * Redistributions in binary form must reproduce the above copyright
11c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes *    notice, this list of conditions and the following disclaimer in
12c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes *    the documentation and/or other materials provided with the
13c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes *    distribution.
14c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes *
15c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes * SUCH DAMAGE.
27c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes */
28c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
29c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes#include <pthread.h>
30c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
31c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes#include <errno.h>
32c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes#include <limits.h>
33e5f816c01780220880ee59a29f727c48b51365d3Yabin Cui#include <stdatomic.h>
34c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes#include <sys/mman.h>
3551e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath#include <time.h>
36c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes#include <unistd.h>
37c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
38c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes#include "pthread_internal.h"
39c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
40c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes#include "private/bionic_futex.h"
41c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes#include "private/bionic_time_conversions.h"
42c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes#include "private/bionic_tls.h"
43c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
4432651b8e8e453391c7aaca47cd885e94d54d0bf4Yabin Cui// XXX *technically* there is a race condition that could allow
4532651b8e8e453391c7aaca47cd885e94d54d0bf4Yabin Cui// XXX a signal to be missed.  If thread A is preempted in _wait()
4632651b8e8e453391c7aaca47cd885e94d54d0bf4Yabin Cui// XXX after unlocking the mutex and before waiting, and if other
4732651b8e8e453391c7aaca47cd885e94d54d0bf4Yabin Cui// XXX threads call signal or broadcast UINT_MAX/2 times (exactly),
4832651b8e8e453391c7aaca47cd885e94d54d0bf4Yabin Cui// XXX before thread A is scheduled again and calls futex_wait(),
4932651b8e8e453391c7aaca47cd885e94d54d0bf4Yabin Cui// XXX then the signal will be lost.
5032651b8e8e453391c7aaca47cd885e94d54d0bf4Yabin Cui
5151e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath// We use one bit in pthread_condattr_t (long) values as the 'shared' flag
5251e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath// and one bit for the clock type (CLOCK_REALTIME is ((clockid_t) 1), and
5351e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath// CLOCK_MONOTONIC is ((clockid_t) 0).). The rest of the bits are a counter.
5451e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath//
5551e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath// The 'value' field pthread_cond_t has the same layout.
5651e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath
5751e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath#define COND_SHARED_MASK 0x0001
5851e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath#define COND_CLOCK_MASK 0x0002
5951e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath#define COND_COUNTER_STEP 0x0004
6051e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath#define COND_FLAGS_MASK (COND_SHARED_MASK | COND_CLOCK_MASK)
6151e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath#define COND_COUNTER_MASK (~COND_FLAGS_MASK)
6251e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath
6351e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath#define COND_IS_SHARED(c) (((c) & COND_SHARED_MASK) != 0)
6451e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath#define COND_GET_CLOCK(c) (((c) & COND_CLOCK_MASK) >> 1)
6551e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath#define COND_SET_CLOCK(attr, c) ((attr) | (c << 1))
6651e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath
67c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughesint pthread_condattr_init(pthread_condattr_t* attr) {
6851e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  *attr = 0;
6951e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  *attr |= PTHREAD_PROCESS_PRIVATE;
7051e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  *attr |= (CLOCK_REALTIME << 1);
71c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  return 0;
72c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes}
73c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
74c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughesint pthread_condattr_getpshared(const pthread_condattr_t* attr, int* pshared) {
7551e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  *pshared = static_cast<int>(COND_IS_SHARED(*attr));
76c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  return 0;
77c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes}
78c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
79c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughesint pthread_condattr_setpshared(pthread_condattr_t* attr, int pshared) {
8051e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  if (pshared != PTHREAD_PROCESS_SHARED && pshared != PTHREAD_PROCESS_PRIVATE) {
81c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes    return EINVAL;
82c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  }
8351e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath
8451e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  *attr |= pshared;
8551e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  return 0;
8651e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath}
8751e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath
8851e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamathint pthread_condattr_getclock(const pthread_condattr_t* attr, clockid_t* clock) {
8951e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  *clock = COND_GET_CLOCK(*attr);
9051e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  return 0;
9151e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath}
9251e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath
9351e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamathint pthread_condattr_setclock(pthread_condattr_t* attr, clockid_t clock) {
9451e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  if (clock != CLOCK_MONOTONIC && clock != CLOCK_REALTIME) {
95c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes    return EINVAL;
96c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  }
9751e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath
9851e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  *attr = COND_SET_CLOCK(*attr, clock);
99c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  return 0;
100c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes}
101c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
102c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughesint pthread_condattr_destroy(pthread_condattr_t* attr) {
103c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  *attr = 0xdeada11d;
104c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  return 0;
105c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes}
106c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
10732651b8e8e453391c7aaca47cd885e94d54d0bf4Yabin Cuistruct pthread_cond_internal_t {
10832651b8e8e453391c7aaca47cd885e94d54d0bf4Yabin Cui  atomic_uint state;
109e5f816c01780220880ee59a29f727c48b51365d3Yabin Cui
1109e6c7bc61838476d749d9bc4801777d35fd46a63Yabin Cui  bool process_shared() {
11132651b8e8e453391c7aaca47cd885e94d54d0bf4Yabin Cui    return COND_IS_SHARED(atomic_load_explicit(&state, memory_order_relaxed));
11232651b8e8e453391c7aaca47cd885e94d54d0bf4Yabin Cui  }
113c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
114c9a659c57b256001fd63f9825bde69e660c2655bYabin Cui  bool use_realtime_clock() {
115c9a659c57b256001fd63f9825bde69e660c2655bYabin Cui    return COND_GET_CLOCK(atomic_load_explicit(&state, memory_order_relaxed)) == CLOCK_REALTIME;
11632651b8e8e453391c7aaca47cd885e94d54d0bf4Yabin Cui  }
11732651b8e8e453391c7aaca47cd885e94d54d0bf4Yabin Cui
11832651b8e8e453391c7aaca47cd885e94d54d0bf4Yabin Cui#if defined(__LP64__)
11932651b8e8e453391c7aaca47cd885e94d54d0bf4Yabin Cui  char __reserved[44];
12032651b8e8e453391c7aaca47cd885e94d54d0bf4Yabin Cui#endif
12132651b8e8e453391c7aaca47cd885e94d54d0bf4Yabin Cui};
122c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
123b58457221364eaad039c2c49a42626b725e980d5Yabin Cuistatic_assert(sizeof(pthread_cond_t) == sizeof(pthread_cond_internal_t),
124b58457221364eaad039c2c49a42626b725e980d5Yabin Cui              "pthread_cond_t should actually be pthread_cond_internal_t in implementation.");
125b58457221364eaad039c2c49a42626b725e980d5Yabin Cui
126b58457221364eaad039c2c49a42626b725e980d5Yabin Cui// For binary compatibility with old version of pthread_cond_t, we can't use more strict alignment
127b58457221364eaad039c2c49a42626b725e980d5Yabin Cui// than 4-byte alignment.
128b58457221364eaad039c2c49a42626b725e980d5Yabin Cuistatic_assert(alignof(pthread_cond_t) == 4,
129b58457221364eaad039c2c49a42626b725e980d5Yabin Cui              "pthread_cond_t should fulfill the alignment requirement of pthread_cond_internal_t.");
130b58457221364eaad039c2c49a42626b725e980d5Yabin Cui
13132651b8e8e453391c7aaca47cd885e94d54d0bf4Yabin Cuistatic pthread_cond_internal_t* __get_internal_cond(pthread_cond_t* cond_interface) {
13232651b8e8e453391c7aaca47cd885e94d54d0bf4Yabin Cui  return reinterpret_cast<pthread_cond_internal_t*>(cond_interface);
13332651b8e8e453391c7aaca47cd885e94d54d0bf4Yabin Cui}
13432651b8e8e453391c7aaca47cd885e94d54d0bf4Yabin Cui
13532651b8e8e453391c7aaca47cd885e94d54d0bf4Yabin Cuiint pthread_cond_init(pthread_cond_t* cond_interface, const pthread_condattr_t* attr) {
13632651b8e8e453391c7aaca47cd885e94d54d0bf4Yabin Cui  pthread_cond_internal_t* cond = __get_internal_cond(cond_interface);
137e5f816c01780220880ee59a29f727c48b51365d3Yabin Cui
13832651b8e8e453391c7aaca47cd885e94d54d0bf4Yabin Cui  unsigned int init_state = 0;
13951e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  if (attr != NULL) {
14032651b8e8e453391c7aaca47cd885e94d54d0bf4Yabin Cui    init_state = (*attr & COND_FLAGS_MASK);
141c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  }
14232651b8e8e453391c7aaca47cd885e94d54d0bf4Yabin Cui  atomic_init(&cond->state, init_state);
143c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
144c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  return 0;
145c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes}
146c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
14732651b8e8e453391c7aaca47cd885e94d54d0bf4Yabin Cuiint pthread_cond_destroy(pthread_cond_t* cond_interface) {
14832651b8e8e453391c7aaca47cd885e94d54d0bf4Yabin Cui  pthread_cond_internal_t* cond = __get_internal_cond(cond_interface);
14932651b8e8e453391c7aaca47cd885e94d54d0bf4Yabin Cui  atomic_store_explicit(&cond->state, 0xdeadc04d, memory_order_relaxed);
150c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  return 0;
151c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes}
152c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
153c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes// This function is used by pthread_cond_broadcast and
154c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes// pthread_cond_signal to atomically decrement the counter
155e5f816c01780220880ee59a29f727c48b51365d3Yabin Cui// then wake up thread_count threads.
15632651b8e8e453391c7aaca47cd885e94d54d0bf4Yabin Cuistatic int __pthread_cond_pulse(pthread_cond_internal_t* cond, int thread_count) {
157e5f816c01780220880ee59a29f727c48b51365d3Yabin Cui  // We don't use a release/seq_cst fence here. Because pthread_cond_wait/signal can't be
158e5f816c01780220880ee59a29f727c48b51365d3Yabin Cui  // used as a method for memory synchronization by itself. It should always be used with
159e5f816c01780220880ee59a29f727c48b51365d3Yabin Cui  // pthread mutexes. Note that Spurious wakeups from pthread_cond_wait/timedwait may occur,
160e5f816c01780220880ee59a29f727c48b51365d3Yabin Cui  // so when using condition variables there is always a boolean predicate involving shared
161e5f816c01780220880ee59a29f727c48b51365d3Yabin Cui  // variables associated with each condition wait that is true if the thread should proceed.
162e5f816c01780220880ee59a29f727c48b51365d3Yabin Cui  // If the predicate is seen true before a condition wait, pthread_cond_wait/timedwait will
163e5f816c01780220880ee59a29f727c48b51365d3Yabin Cui  // not be called. That's why pthread_wait/signal pair can't be used as a method for memory
164e5f816c01780220880ee59a29f727c48b51365d3Yabin Cui  // synchronization. And it doesn't help even if we use any fence here.
165e5f816c01780220880ee59a29f727c48b51365d3Yabin Cui
166e5f816c01780220880ee59a29f727c48b51365d3Yabin Cui  // The increase of value should leave flags alone, even if the value can overflows.
16732651b8e8e453391c7aaca47cd885e94d54d0bf4Yabin Cui  atomic_fetch_add_explicit(&cond->state, COND_COUNTER_STEP, memory_order_relaxed);
168e5f816c01780220880ee59a29f727c48b51365d3Yabin Cui
16932651b8e8e453391c7aaca47cd885e94d54d0bf4Yabin Cui  __futex_wake_ex(&cond->state, cond->process_shared(), thread_count);
170c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  return 0;
171c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes}
172c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
173c9a659c57b256001fd63f9825bde69e660c2655bYabin Cuistatic int __pthread_cond_timedwait(pthread_cond_internal_t* cond, pthread_mutex_t* mutex,
174c9a659c57b256001fd63f9825bde69e660c2655bYabin Cui                                    bool use_realtime_clock, const timespec* abs_timeout_or_null) {
175dd586f2ebd0c42904e699f3634568a38c97d4da7Elliott Hughes  int result = check_timespec(abs_timeout_or_null, true);
176c9a659c57b256001fd63f9825bde69e660c2655bYabin Cui  if (result != 0) {
177c9a659c57b256001fd63f9825bde69e660c2655bYabin Cui    return result;
178c9a659c57b256001fd63f9825bde69e660c2655bYabin Cui  }
179c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
180c9a659c57b256001fd63f9825bde69e660c2655bYabin Cui  unsigned int old_state = atomic_load_explicit(&cond->state, memory_order_relaxed);
181c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  pthread_mutex_unlock(mutex);
182c9a659c57b256001fd63f9825bde69e660c2655bYabin Cui  int status = __futex_wait_ex(&cond->state, cond->process_shared(), old_state,
183c9a659c57b256001fd63f9825bde69e660c2655bYabin Cui                               use_realtime_clock, abs_timeout_or_null);
184c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  pthread_mutex_lock(mutex);
185c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
1869e79af3b61b5a617c537862ebe72248beff58f19Elliott Hughes  if (status == -ETIMEDOUT) {
187c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes    return ETIMEDOUT;
188c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  }
189c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  return 0;
190c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes}
191c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
19232651b8e8e453391c7aaca47cd885e94d54d0bf4Yabin Cuiint pthread_cond_broadcast(pthread_cond_t* cond_interface) {
19332651b8e8e453391c7aaca47cd885e94d54d0bf4Yabin Cui  return __pthread_cond_pulse(__get_internal_cond(cond_interface), INT_MAX);
194c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes}
195c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
19632651b8e8e453391c7aaca47cd885e94d54d0bf4Yabin Cuiint pthread_cond_signal(pthread_cond_t* cond_interface) {
19732651b8e8e453391c7aaca47cd885e94d54d0bf4Yabin Cui  return __pthread_cond_pulse(__get_internal_cond(cond_interface), 1);
198c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes}
199c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
20032651b8e8e453391c7aaca47cd885e94d54d0bf4Yabin Cuiint pthread_cond_wait(pthread_cond_t* cond_interface, pthread_mutex_t* mutex) {
20132651b8e8e453391c7aaca47cd885e94d54d0bf4Yabin Cui  pthread_cond_internal_t* cond = __get_internal_cond(cond_interface);
202c9a659c57b256001fd63f9825bde69e660c2655bYabin Cui  return __pthread_cond_timedwait(cond, mutex, false, nullptr);
203c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes}
204c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
20532651b8e8e453391c7aaca47cd885e94d54d0bf4Yabin Cuiint pthread_cond_timedwait(pthread_cond_t *cond_interface, pthread_mutex_t * mutex,
20632651b8e8e453391c7aaca47cd885e94d54d0bf4Yabin Cui                           const timespec *abstime) {
20732651b8e8e453391c7aaca47cd885e94d54d0bf4Yabin Cui
20832651b8e8e453391c7aaca47cd885e94d54d0bf4Yabin Cui  pthread_cond_internal_t* cond = __get_internal_cond(cond_interface);
209c9a659c57b256001fd63f9825bde69e660c2655bYabin Cui  return __pthread_cond_timedwait(cond, mutex, cond->use_realtime_clock(), abstime);
210c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes}
211c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
21251e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath#if !defined(__LP64__)
21351e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath// TODO: this exists only for backward binary compatibility on 32 bit platforms.
21432651b8e8e453391c7aaca47cd885e94d54d0bf4Yabin Cuiextern "C" int pthread_cond_timedwait_monotonic(pthread_cond_t* cond_interface,
21532651b8e8e453391c7aaca47cd885e94d54d0bf4Yabin Cui                                                pthread_mutex_t* mutex,
21632651b8e8e453391c7aaca47cd885e94d54d0bf4Yabin Cui                                                const timespec* abs_timeout) {
21732651b8e8e453391c7aaca47cd885e94d54d0bf4Yabin Cui
218c9a659c57b256001fd63f9825bde69e660c2655bYabin Cui  return __pthread_cond_timedwait(__get_internal_cond(cond_interface), mutex, false, abs_timeout);
219c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes}
220c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
22132651b8e8e453391c7aaca47cd885e94d54d0bf4Yabin Cuiextern "C" int pthread_cond_timedwait_monotonic_np(pthread_cond_t* cond_interface,
22232651b8e8e453391c7aaca47cd885e94d54d0bf4Yabin Cui                                                   pthread_mutex_t* mutex,
22332651b8e8e453391c7aaca47cd885e94d54d0bf4Yabin Cui                                                   const timespec* abs_timeout) {
22432651b8e8e453391c7aaca47cd885e94d54d0bf4Yabin Cui  return pthread_cond_timedwait_monotonic(cond_interface, mutex, abs_timeout);
225c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes}
226c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
22732651b8e8e453391c7aaca47cd885e94d54d0bf4Yabin Cuiextern "C" int pthread_cond_timedwait_relative_np(pthread_cond_t* cond_interface,
22832651b8e8e453391c7aaca47cd885e94d54d0bf4Yabin Cui                                                  pthread_mutex_t* mutex,
22932651b8e8e453391c7aaca47cd885e94d54d0bf4Yabin Cui                                                  const timespec* rel_timeout) {
230c9a659c57b256001fd63f9825bde69e660c2655bYabin Cui  timespec ts;
231c9a659c57b256001fd63f9825bde69e660c2655bYabin Cui  timespec* abs_timeout = nullptr;
232c9a659c57b256001fd63f9825bde69e660c2655bYabin Cui  if (rel_timeout != nullptr) {
233c9a659c57b256001fd63f9825bde69e660c2655bYabin Cui    absolute_timespec_from_timespec(ts, *rel_timeout, CLOCK_REALTIME);
234c9a659c57b256001fd63f9825bde69e660c2655bYabin Cui    abs_timeout = &ts;
235c9a659c57b256001fd63f9825bde69e660c2655bYabin Cui  }
236c9a659c57b256001fd63f9825bde69e660c2655bYabin Cui  return __pthread_cond_timedwait(__get_internal_cond(cond_interface), mutex, true, abs_timeout);
237c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes}
238c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
23932651b8e8e453391c7aaca47cd885e94d54d0bf4Yabin Cuiextern "C" int pthread_cond_timeout_np(pthread_cond_t* cond_interface,
24032651b8e8e453391c7aaca47cd885e94d54d0bf4Yabin Cui                                       pthread_mutex_t* mutex, unsigned ms) {
241c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  timespec ts;
242c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  timespec_from_ms(ts, ms);
24332651b8e8e453391c7aaca47cd885e94d54d0bf4Yabin Cui  return pthread_cond_timedwait_relative_np(cond_interface, mutex, &ts);
244c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes}
245f0870c3bfeba99482392fafe6d5f49615393c2b1Halton Huo#endif // !defined(__LP64__)
246