2013-10-30 22:40:09 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
|
|
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <pthread.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <sys/atomics.h>
|
|
|
|
#include <sys/mman.h>
|
2014-03-03 16:38:51 +01:00
|
|
|
#include <time.h>
|
2013-10-30 22:40:09 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "pthread_internal.h"
|
|
|
|
|
|
|
|
#include "private/bionic_atomic_inline.h"
|
|
|
|
#include "private/bionic_futex.h"
|
|
|
|
#include "private/bionic_time_conversions.h"
|
|
|
|
#include "private/bionic_tls.h"
|
|
|
|
#include "private/thread_private.h"
|
|
|
|
|
2014-03-03 16:38:51 +01:00
|
|
|
// We use one bit in pthread_condattr_t (long) values as the 'shared' flag
|
|
|
|
// and one bit for the clock type (CLOCK_REALTIME is ((clockid_t) 1), and
|
|
|
|
// CLOCK_MONOTONIC is ((clockid_t) 0).). The rest of the bits are a counter.
|
|
|
|
//
|
|
|
|
// The 'value' field pthread_cond_t has the same layout.
|
|
|
|
|
|
|
|
#define COND_SHARED_MASK 0x0001
|
|
|
|
#define COND_CLOCK_MASK 0x0002
|
|
|
|
#define COND_COUNTER_STEP 0x0004
|
|
|
|
#define COND_FLAGS_MASK (COND_SHARED_MASK | COND_CLOCK_MASK)
|
|
|
|
#define COND_COUNTER_MASK (~COND_FLAGS_MASK)
|
|
|
|
|
|
|
|
#define COND_IS_SHARED(c) (((c) & COND_SHARED_MASK) != 0)
|
|
|
|
#define COND_GET_CLOCK(c) (((c) & COND_CLOCK_MASK) >> 1)
|
|
|
|
#define COND_SET_CLOCK(attr, c) ((attr) | (c << 1))
|
|
|
|
|
|
|
|
|
2013-10-30 22:40:09 +01:00
|
|
|
int pthread_condattr_init(pthread_condattr_t* attr) {
|
2014-03-03 16:38:51 +01:00
|
|
|
*attr = 0;
|
|
|
|
*attr |= PTHREAD_PROCESS_PRIVATE;
|
|
|
|
*attr |= (CLOCK_REALTIME << 1);
|
2013-10-30 22:40:09 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int pthread_condattr_getpshared(const pthread_condattr_t* attr, int* pshared) {
|
2014-03-03 16:38:51 +01:00
|
|
|
*pshared = static_cast<int>(COND_IS_SHARED(*attr));
|
2013-10-30 22:40:09 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int pthread_condattr_setpshared(pthread_condattr_t* attr, int pshared) {
|
2014-03-03 16:38:51 +01:00
|
|
|
if (pshared != PTHREAD_PROCESS_SHARED && pshared != PTHREAD_PROCESS_PRIVATE) {
|
2013-10-30 22:40:09 +01:00
|
|
|
return EINVAL;
|
|
|
|
}
|
2014-03-03 16:38:51 +01:00
|
|
|
|
|
|
|
*attr |= pshared;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int pthread_condattr_getclock(const pthread_condattr_t* attr, clockid_t* clock) {
|
|
|
|
*clock = COND_GET_CLOCK(*attr);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int pthread_condattr_setclock(pthread_condattr_t* attr, clockid_t clock) {
|
|
|
|
if (clock != CLOCK_MONOTONIC && clock != CLOCK_REALTIME) {
|
2013-10-30 22:40:09 +01:00
|
|
|
return EINVAL;
|
|
|
|
}
|
2014-03-03 16:38:51 +01:00
|
|
|
|
|
|
|
*attr = COND_SET_CLOCK(*attr, clock);
|
2013-10-30 22:40:09 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int pthread_condattr_destroy(pthread_condattr_t* attr) {
|
|
|
|
*attr = 0xdeada11d;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// XXX *technically* there is a race condition that could allow
|
|
|
|
// XXX a signal to be missed. If thread A is preempted in _wait()
|
|
|
|
// XXX after unlocking the mutex and before waiting, and if other
|
|
|
|
// XXX threads call signal or broadcast UINT_MAX/2 times (exactly),
|
|
|
|
// XXX before thread A is scheduled again and calls futex_wait(),
|
|
|
|
// XXX then the signal will be lost.
|
|
|
|
|
|
|
|
int pthread_cond_init(pthread_cond_t* cond, const pthread_condattr_t* attr) {
|
2014-03-03 16:38:51 +01:00
|
|
|
if (attr != NULL) {
|
|
|
|
cond->value = (*attr & COND_FLAGS_MASK);
|
|
|
|
} else {
|
|
|
|
cond->value = 0;
|
2013-10-30 22:40:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int pthread_cond_destroy(pthread_cond_t* cond) {
|
|
|
|
cond->value = 0xdeadc04d;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function is used by pthread_cond_broadcast and
|
|
|
|
// pthread_cond_signal to atomically decrement the counter
|
|
|
|
// then wake up 'counter' threads.
|
|
|
|
static int __pthread_cond_pulse(pthread_cond_t* cond, int counter) {
|
2014-03-03 16:38:51 +01:00
|
|
|
int flags = (cond->value & COND_FLAGS_MASK);
|
2013-10-30 22:40:09 +01:00
|
|
|
while (true) {
|
2014-03-03 16:38:51 +01:00
|
|
|
int old_value = cond->value;
|
|
|
|
int new_value = ((old_value - COND_COUNTER_STEP) & COND_COUNTER_MASK) | flags;
|
2013-10-30 22:40:09 +01:00
|
|
|
if (__bionic_cmpxchg(old_value, new_value, &cond->value) == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that all memory accesses previously made by this thread are
|
|
|
|
// visible to the woken thread(s). On the other side, the "wait"
|
|
|
|
// code will issue any necessary barriers when locking the mutex.
|
|
|
|
//
|
|
|
|
// This may not strictly be necessary -- if the caller follows
|
|
|
|
// recommended practice and holds the mutex before signaling the cond
|
|
|
|
// var, the mutex ops will provide correct semantics. If they don't
|
|
|
|
// hold the mutex, they're subject to race conditions anyway.
|
|
|
|
ANDROID_MEMBAR_FULL();
|
|
|
|
|
2014-03-03 16:38:51 +01:00
|
|
|
__futex_wake_ex(&cond->value, COND_IS_SHARED(cond->value), counter);
|
2013-10-30 22:40:09 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
__LIBC_HIDDEN__
|
|
|
|
int __pthread_cond_timedwait_relative(pthread_cond_t* cond, pthread_mutex_t* mutex, const timespec* reltime) {
|
|
|
|
int old_value = cond->value;
|
|
|
|
|
|
|
|
pthread_mutex_unlock(mutex);
|
2014-03-03 16:38:51 +01:00
|
|
|
int status = __futex_wait_ex(&cond->value, COND_IS_SHARED(cond->value), old_value, reltime);
|
2013-10-30 22:40:09 +01:00
|
|
|
pthread_mutex_lock(mutex);
|
|
|
|
|
2013-12-18 19:05:42 +01:00
|
|
|
if (status == -ETIMEDOUT) {
|
2013-10-30 22:40:09 +01:00
|
|
|
return ETIMEDOUT;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
__LIBC_HIDDEN__
|
|
|
|
int __pthread_cond_timedwait(pthread_cond_t* cond, pthread_mutex_t* mutex, const timespec* abstime, clockid_t clock) {
|
|
|
|
timespec ts;
|
|
|
|
timespec* tsp;
|
|
|
|
|
|
|
|
if (abstime != NULL) {
|
2014-03-04 01:42:47 +01:00
|
|
|
if (__timespec_from_absolute(&ts, abstime, clock) < 0) {
|
2013-10-30 22:40:09 +01:00
|
|
|
return ETIMEDOUT;
|
|
|
|
}
|
|
|
|
tsp = &ts;
|
|
|
|
} else {
|
|
|
|
tsp = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return __pthread_cond_timedwait_relative(cond, mutex, tsp);
|
|
|
|
}
|
|
|
|
|
|
|
|
int pthread_cond_broadcast(pthread_cond_t* cond) {
|
|
|
|
return __pthread_cond_pulse(cond, INT_MAX);
|
|
|
|
}
|
|
|
|
|
|
|
|
int pthread_cond_signal(pthread_cond_t* cond) {
|
|
|
|
return __pthread_cond_pulse(cond, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int pthread_cond_wait(pthread_cond_t* cond, pthread_mutex_t* mutex) {
|
2014-03-03 16:38:51 +01:00
|
|
|
return __pthread_cond_timedwait(cond, mutex, NULL, COND_GET_CLOCK(cond->value));
|
2013-10-30 22:40:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t * mutex, const timespec *abstime) {
|
2014-03-03 16:38:51 +01:00
|
|
|
return __pthread_cond_timedwait(cond, mutex, abstime, COND_GET_CLOCK(cond->value));
|
2013-10-30 22:40:09 +01:00
|
|
|
}
|
|
|
|
|
2014-03-03 16:38:51 +01:00
|
|
|
#if !defined(__LP64__)
|
|
|
|
// TODO: this exists only for backward binary compatibility on 32 bit platforms.
|
|
|
|
extern "C" int pthread_cond_timedwait_monotonic(pthread_cond_t* cond, pthread_mutex_t* mutex, const timespec* abstime) {
|
2013-10-30 22:40:09 +01:00
|
|
|
return __pthread_cond_timedwait(cond, mutex, abstime, CLOCK_MONOTONIC);
|
|
|
|
}
|
|
|
|
|
2014-03-03 16:38:51 +01:00
|
|
|
extern "C" int pthread_cond_timedwait_monotonic_np(pthread_cond_t* cond, pthread_mutex_t* mutex, const timespec* abstime) {
|
2013-10-30 22:40:09 +01:00
|
|
|
return __pthread_cond_timedwait(cond, mutex, abstime, CLOCK_MONOTONIC);
|
|
|
|
}
|
2014-03-03 16:38:51 +01:00
|
|
|
#endif // !defined(__LP64__)
|
2013-10-30 22:40:09 +01:00
|
|
|
|
|
|
|
int pthread_cond_timedwait_relative_np(pthread_cond_t* cond, pthread_mutex_t* mutex, const timespec* reltime) {
|
|
|
|
return __pthread_cond_timedwait_relative(cond, mutex, reltime);
|
|
|
|
}
|
|
|
|
|
|
|
|
int pthread_cond_timeout_np(pthread_cond_t* cond, pthread_mutex_t* mutex, unsigned ms) {
|
|
|
|
timespec ts;
|
|
|
|
timespec_from_ms(ts, ms);
|
|
|
|
return __pthread_cond_timedwait_relative(cond, mutex, &ts);
|
|
|
|
}
|