__LIBC_HIDDEN__ int __pthread_cond_timedwait(pthread_cond_t* cond, pthread_mutex_t* mutex, const struct timespec* abstime, clockid_t clock) { struct timespec ts; struct timespec* tsp; if (abstime != NULL) { if (__timespec_to_absolute(&ts, abstime, clock) < 0) { return ETIMEDOUT; } tsp = &ts; } else { tsp = NULL; } return __pthread_cond_timedwait_relative(cond, mutex, tsp); }
__LIBC_HIDDEN__ int pthread_mutex_lock_timeout_np_impl(pthread_mutex_t *mutex, unsigned msecs) { clockid_t clock = CLOCK_MONOTONIC; struct timespec abstime; struct timespec ts; int mvalue, mtype, tid, shared; /* compute absolute expiration time */ __timespec_to_relative_msec(&abstime, msecs, clock); if (__unlikely(mutex == NULL)) return EINVAL; mvalue = mutex->value; mtype = (mvalue & MUTEX_TYPE_MASK); shared = (mvalue & MUTEX_SHARED_MASK); /* Handle common case first */ if ( __likely(mtype == MUTEX_TYPE_BITS_NORMAL) ) { const int unlocked = shared | MUTEX_STATE_BITS_UNLOCKED; const int locked_uncontended = shared | MUTEX_STATE_BITS_LOCKED_UNCONTENDED; const int locked_contended = shared | MUTEX_STATE_BITS_LOCKED_CONTENDED; /* fast path for uncontended lock. Note: MUTEX_TYPE_BITS_NORMAL is 0 */ if (__bionic_cmpxchg(unlocked, locked_uncontended, &mutex->value) == 0) { ANDROID_MEMBAR_FULL(); return 0; } /* loop while needed */ while (__bionic_swap(locked_contended, &mutex->value) != unlocked) { if (__timespec_to_absolute(&ts, &abstime, clock) < 0) return EBUSY; __futex_wait_ex(&mutex->value, shared, locked_contended, &ts); } ANDROID_MEMBAR_FULL(); return 0; } /* Do we already own this recursive or error-check mutex ? */ tid = __get_thread()->tid; if ( tid == MUTEX_OWNER_FROM_BITS(mvalue) ) return _recursive_increment(mutex, mvalue, mtype); /* the following implements the same loop than pthread_mutex_lock_impl * but adds checks to ensure that the operation never exceeds the * absolute expiration time. */ mtype |= shared; /* first try a quick lock */ if (mvalue == mtype) { mvalue = MUTEX_OWNER_TO_BITS(tid) | mtype | MUTEX_STATE_BITS_LOCKED_UNCONTENDED; if (__likely(__bionic_cmpxchg(mtype, mvalue, &mutex->value) == 0)) { ANDROID_MEMBAR_FULL(); return 0; } mvalue = mutex->value; } for (;;) { struct timespec ts; /* if the value is 'unlocked', try to acquire it directly */ /* NOTE: put state to 2 since we know there is contention */ if (mvalue == mtype) { /* unlocked */ mvalue = MUTEX_OWNER_TO_BITS(tid) | mtype | MUTEX_STATE_BITS_LOCKED_CONTENDED; if (__bionic_cmpxchg(mtype, mvalue, &mutex->value) == 0) { ANDROID_MEMBAR_FULL(); return 0; } /* the value changed before we could lock it. We need to check * the time to avoid livelocks, reload the value, then loop again. */ if (__timespec_to_absolute(&ts, &abstime, clock) < 0) return EBUSY; mvalue = mutex->value; continue; } /* The value is locked. If 'uncontended', try to switch its state * to 'contented' to ensure we get woken up later. */ if (MUTEX_STATE_BITS_IS_LOCKED_UNCONTENDED(mvalue)) { int newval = MUTEX_STATE_BITS_FLIP_CONTENTION(mvalue); if (__bionic_cmpxchg(mvalue, newval, &mutex->value) != 0) { /* this failed because the value changed, reload it */ mvalue = mutex->value; } else { /* this succeeded, update mvalue */ mvalue = newval; } } /* check time and update 'ts' */ if (__timespec_to_absolute(&ts, &abstime, clock) < 0) return EBUSY; /* Only wait to be woken up if the state is '2', otherwise we'll * simply loop right now. This can happen when the second cmpxchg * in our loop failed because the mutex was unlocked by another * thread. */ if (MUTEX_STATE_BITS_IS_LOCKED_CONTENDED(mvalue)) { if (__futex_wait_ex(&mutex->value, shared, mvalue, &ts) == ETIMEDOUT) { return EBUSY; } mvalue = mutex->value; } } /* NOTREACHED */ }