/* This common inlined function is used to increment the counter of an * errorcheck or recursive mutex. * * For errorcheck mutexes, it will return EDEADLK * If the counter overflows, it will return EAGAIN * Otherwise, it atomically increments the counter and returns 0 * after providing an acquire barrier. * * mtype is the current mutex type * mvalue is the current mutex value (already loaded) * mutex pointers to the mutex. */ static __inline__ __attribute__((always_inline)) int _recursive_increment(pthread_mutex_t* mutex, int mvalue, int mtype) { if (mtype == MUTEX_TYPE_BITS_ERRORCHECK) { /* trying to re-lock a mutex we already acquired */ return EDEADLK; } /* Detect recursive lock overflow and return EAGAIN. * This is safe because only the owner thread can modify the * counter bits in the mutex value. */ if (MUTEX_COUNTER_BITS_WILL_OVERFLOW(mvalue)) { return EAGAIN; } /* We own the mutex, but other threads are able to change * the lower bits (e.g. promoting it to "contended"), so we * need to use an atomic cmpxchg loop to update the counter. */ for (;;) { /* increment counter, overflow was already checked */ int newval = mvalue + MUTEX_COUNTER_BITS_ONE; if (__predict_true(__bionic_cmpxchg(mvalue, newval, &mutex->value) == 0)) { /* mutex is still locked, not need for a memory barrier */ return 0; } /* the value was changed, this happens when another thread changes * the lower state bits from 1 to 2 to indicate contention. This * cannot change the counter, so simply reload and try again. */ mvalue = mutex->value; } }
// 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) { if (__predict_false(cond == NULL)) { return EINVAL; } long flags = (cond->value & ~COND_COUNTER_MASK); while (true) { long old_value = cond->value; long new_value = ((old_value - COND_COUNTER_INCREMENT) & COND_COUNTER_MASK) | flags; 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(); __futex_wake_ex(&cond->value, COND_IS_SHARED(cond), counter); return 0; }
/* * Lock a non-recursive mutex. * * As noted above, there are three states: * 0 (unlocked, no contention) * 1 (locked, no contention) * 2 (locked, contention) * * Non-recursive mutexes don't use the thread-id or counter fields, and the * "type" value is zero, so the only bits that will be set are the ones in * the lock state field. */ static __inline__ void _normal_lock(pthread_mutex_t* mutex, int shared) { /* convenience shortcuts */ const int unlocked = shared | MUTEX_STATE_BITS_UNLOCKED; const int locked_uncontended = shared | MUTEX_STATE_BITS_LOCKED_UNCONTENDED; /* * The common case is an unlocked mutex, so we begin by trying to * change the lock's state from 0 (UNLOCKED) to 1 (LOCKED). * __bionic_cmpxchg() returns 0 if it made the swap successfully. * If the result is nonzero, this lock is already held by another thread. */ if (__bionic_cmpxchg(unlocked, locked_uncontended, &mutex->value) != 0) { const int locked_contended = shared | MUTEX_STATE_BITS_LOCKED_CONTENDED; /* * We want to go to sleep until the mutex is available, which * requires promoting it to state 2 (CONTENDED). We need to * swap in the new state value and then wait until somebody wakes us up. * * __bionic_swap() returns the previous value. We swap 2 in and * see if we got zero back; if so, we have acquired the lock. If * not, another thread still holds the lock and we wait again. * * The second argument to the __futex_wait() call is compared * against the current value. If it doesn't match, __futex_wait() * returns immediately (otherwise, it sleeps for a time specified * by the third argument; 0 means sleep forever). This ensures * that the mutex is in state 2 when we go to sleep on it, which * guarantees a wake-up call. */ while (__bionic_swap(locked_contended, &mutex->value) != unlocked) __futex_wait_ex(&mutex->value, shared, locked_contended, 0); } ANDROID_MEMBAR_FULL(); }
/* 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) { long flags; if (__unlikely(cond == NULL)) return EINVAL; flags = (cond->value & ~COND_COUNTER_MASK); for (;;) { long oldval = cond->value; long newval = ((oldval - COND_COUNTER_INCREMENT) & COND_COUNTER_MASK) | flags; if (__bionic_cmpxchg(oldval, newval, &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(); __futex_wake_ex(&cond->value, COND_IS_SHARED(cond), counter); return 0; }
__LIBC_HIDDEN__ int pthread_mutex_trylock_impl(pthread_mutex_t *mutex) { int mvalue, mtype, tid, shared; 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) ) { if (__bionic_cmpxchg(shared|MUTEX_STATE_BITS_UNLOCKED, shared|MUTEX_STATE_BITS_LOCKED_UNCONTENDED, &mutex->value) == 0) { ANDROID_MEMBAR_FULL(); return 0; } return EBUSY; } /* 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); /* Same as pthread_mutex_lock, except that we don't want to wait, and * the only operation that can succeed is a single cmpxchg to acquire the * lock if it is released / not owned by anyone. No need for a complex loop. */ mtype |= shared | MUTEX_STATE_BITS_UNLOCKED; 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; } return EBUSY; }
__LIBC_HIDDEN__ int pthread_mutex_unlock_impl(pthread_mutex_t *mutex) { int mvalue, mtype, tid, shared; 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)) { _normal_unlock(mutex, shared); return 0; } /* Do we already own this recursive or error-check mutex ? */ tid = __get_thread()->tid; if ( tid != MUTEX_OWNER_FROM_BITS(mvalue) ) return EPERM; /* If the counter is > 0, we can simply decrement it atomically. * Since other threads can mutate the lower state bits (and only the * lower state bits), use a cmpxchg to do it. */ if (!MUTEX_COUNTER_BITS_IS_ZERO(mvalue)) { for (;;) { int newval = mvalue - MUTEX_COUNTER_BITS_ONE; if (__likely(__bionic_cmpxchg(mvalue, newval, &mutex->value) == 0)) { /* success: we still own the mutex, so no memory barrier */ return 0; } /* the value changed, so reload and loop */ mvalue = mutex->value; } } /* the counter is 0, so we're going to unlock the mutex by resetting * its value to 'unlocked'. We need to perform a swap in order * to read the current state, which will be 2 if there are waiters * to awake. * * TODO: Change this to __bionic_swap_release when we implement it * to get rid of the explicit memory barrier below. */ ANDROID_MEMBAR_FULL(); /* RELEASE BARRIER */ mvalue = __bionic_swap(mtype | shared | MUTEX_STATE_BITS_UNLOCKED, &mutex->value); /* Wake one waiting thread, if any */ if (MUTEX_STATE_BITS_IS_LOCKED_CONTENDED(mvalue)) { __futex_wake_ex(&mutex->value, shared, 1); } return 0; }
extern "C" int __cxa_guard_acquire(_guard_t* gv) { // 0 -> pending, return 1 // pending -> waiting, wait and return 0 // waiting: untouched, wait and return 0 // ready: untouched, return 0 retry: if (__bionic_cmpxchg(0, pending, &gv->state) == 0) { ANDROID_MEMBAR_FULL(); return 1; } __bionic_cmpxchg(pending, waiting, &gv->state); // Indicate there is a waiter __futex_wait(&gv->state, waiting, NULL); if (gv->state != ready) // __cxa_guard_abort was called, let every thread try since there is no return code for this condition goto retry; ANDROID_MEMBAR_FULL(); return 0; }
extern "C" void __cxa_guard_release(_guard_t* gv) { // pending -> ready // waiting -> ready, and wake ANDROID_MEMBAR_FULL(); if (__bionic_cmpxchg(pending, ready, &gv->state) == 0) { return; } gv->state = ready; __futex_wake(&gv->state, 0x7fffffff); }
static int __pthread_mutex_timedlock(pthread_mutex_t* mutex, const timespec* abs_timeout, clockid_t clock) { timespec ts; int mvalue = mutex->value; int mtype = (mvalue & MUTEX_TYPE_MASK); int shared = (mvalue & MUTEX_SHARED_MASK); // Handle common case first. if (__predict_true(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_from_absolute(&ts, abs_timeout, clock) < 0) { return ETIMEDOUT; } __futex_wait_ex(&mutex->value, shared, locked_contended, &ts); } ANDROID_MEMBAR_FULL(); return 0; } // Do we already own this recursive or error-check mutex? pid_t tid = __get_thread()->tid; if (tid == MUTEX_OWNER_FROM_BITS(mvalue)) { return _recursive_increment(mutex, mvalue, mtype); } // The following implements the same loop as 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 (__predict_true(__bionic_cmpxchg(mtype, mvalue, &mutex->value) == 0)) { ANDROID_MEMBAR_FULL(); return 0; } mvalue = mutex->value; } while (true) { // 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_from_absolute(&ts, abs_timeout, clock) < 0) { return ETIMEDOUT; } 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_from_absolute(&ts, abs_timeout, clock) < 0) { return ETIMEDOUT; } // 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 ETIMEDOUT; } mvalue = mutex->value; } } /* NOTREACHED */ }
__LIBC_HIDDEN__ int pthread_mutex_lock_impl(pthread_mutex_t *mutex) { int mvalue, mtype, tid, shared; mvalue = mutex->value; mtype = (mvalue & MUTEX_TYPE_MASK); shared = (mvalue & MUTEX_SHARED_MASK); /* Handle normal case first */ if ( __predict_true(mtype == MUTEX_TYPE_BITS_NORMAL) ) { _normal_lock(mutex, shared); 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); /* Add in shared state to avoid extra 'or' operations below */ mtype |= shared; /* First, if the mutex is unlocked, try to quickly acquire it. * In the optimistic case where this works, set the state to 1 to * indicate locked with no contention */ if (mvalue == mtype) { int newval = MUTEX_OWNER_TO_BITS(tid) | mtype | MUTEX_STATE_BITS_LOCKED_UNCONTENDED; if (__bionic_cmpxchg(mvalue, newval, &mutex->value) == 0) { ANDROID_MEMBAR_FULL(); return 0; } /* argh, the value changed, reload before entering the loop */ mvalue = mutex->value; } for (;;) { int newval; /* if the mutex is unlocked, its value should be 'mtype' and * we try to acquire it by setting its owner and state atomically. * NOTE: We put the state to 2 since we _know_ there is contention * when we are in this loop. This ensures all waiters will be * unlocked. */ if (mvalue == mtype) { newval = MUTEX_OWNER_TO_BITS(tid) | mtype | MUTEX_STATE_BITS_LOCKED_CONTENDED; /* TODO: Change this to __bionic_cmpxchg_acquire when we * implement it to get rid of the explicit memory * barrier below. */ if (__predict_false(__bionic_cmpxchg(mvalue, newval, &mutex->value) != 0)) { mvalue = mutex->value; continue; } ANDROID_MEMBAR_FULL(); return 0; } /* the mutex is already locked by another thread, if its state is 1 * we will change it to 2 to indicate contention. */ if (MUTEX_STATE_BITS_IS_LOCKED_UNCONTENDED(mvalue)) { newval = MUTEX_STATE_BITS_FLIP_CONTENTION(mvalue); /* locked state 1 => state 2 */ if (__predict_false(__bionic_cmpxchg(mvalue, newval, &mutex->value) != 0)) { mvalue = mutex->value; continue; } mvalue = newval; } /* wait until the mutex is unlocked */ __futex_wait_ex(&mutex->value, shared, mvalue, NULL); mvalue = mutex->value; } /* NOTREACHED */ }
__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 */ }
/* NOTE: this implementation doesn't support a init function that throws a C++ exception * or calls fork() */ int pthread_once( pthread_once_t* once_control, void (*init_routine)(void) ) { volatile pthread_once_t* ocptr = once_control; /* PTHREAD_ONCE_INIT is 0, we use the following bit flags * * bit 0 set -> initialization is under way * bit 1 set -> initialization is complete */ #define ONCE_INITIALIZING (1 << 0) #define ONCE_COMPLETED (1 << 1) /* First check if the once is already initialized. This will be the common * case and we want to make this as fast as possible. Note that this still * requires a load_acquire operation here to ensure that all the * stores performed by the initialization function are observable on * this CPU after we exit. */ if (__likely((*ocptr & ONCE_COMPLETED) != 0)) { ANDROID_MEMBAR_FULL(); return 0; } for (;;) { /* Try to atomically set the INITIALIZING flag. * This requires a cmpxchg loop, and we may need * to exit prematurely if we detect that * COMPLETED is now set. */ int32_t oldval, newval; do { oldval = *ocptr; if ((oldval & ONCE_COMPLETED) != 0) break; newval = oldval | ONCE_INITIALIZING; } while (__bionic_cmpxchg(oldval, newval, ocptr) != 0); if ((oldval & ONCE_COMPLETED) != 0) { /* We detected that COMPLETED was set while in our loop */ ANDROID_MEMBAR_FULL(); return 0; } if ((oldval & ONCE_INITIALIZING) == 0) { /* We got there first, we can jump out of the loop to * handle the initialization */ break; } /* Another thread is running the initialization and hasn't completed * yet, so wait for it, then try again. */ __futex_wait_ex(ocptr, 0, oldval, NULL); } /* call the initialization function. */ (*init_routine)(); /* Do a store_release indicating that initialization is complete */ ANDROID_MEMBAR_FULL(); *ocptr = ONCE_COMPLETED; /* Wake up any waiters, if any */ __futex_wake_ex(ocptr, 0, INT_MAX); return 0; }