void object_lock_exclusive_(IObject *thiz, const char *file, int line) { int ok; ok = pthread_mutex_trylock(&thiz->mMutex); if (0 != ok) { // not android_atomic_acquire_load because we don't care about relative load/load ordering int32_t oldGeneration = thiz->mGeneration; // wait up to a total of 250 ms static const long nanoBackoffs[] = { 10 * 1000000, 20 * 1000000, 30 * 1000000, 40 * 1000000, 50 * 1000000, 100 * 1000000}; unsigned i = 0; timespec ts; memset(&ts, 0, sizeof(timespec)); for (;;) { init_time_spec(&ts, nanoBackoffs[i]); ok = pthread_mutex_timedlock(&thiz->mMutex, &ts); if (0 == ok) { break; } if (EBUSY == ok) { // this is the expected return value for timeout, and will be handled below } else if (EDEADLK == ok) { // we don't use the kind of mutex that can return this error, but just in case SL_LOGE("%s:%d: recursive lock detected", file, line); } else { // some other return value SL_LOGE("%s:%d: pthread_mutex_lock_timeout_np returned %d", file, line, ok); } // is anyone else making forward progress? int32_t newGeneration = thiz->mGeneration; if (newGeneration != oldGeneration) { // if we ever see forward progress then lock without timeout (more efficient) goto forward_progress; } // no, then continue trying to lock but with increasing timeouts if (++i >= (sizeof(nanoBackoffs) / sizeof(nanoBackoffs[0]))) { // the extra block avoids a C++ compiler error about goto past initialization { pthread_t me = pthread_self(); pthread_t owner = thiz->mOwner; // unlikely, but this could result in a memory fault if owner is corrupt pid_t ownerTid = LIKELY_VALID(owner) ? __pthread_gettid(owner) : -1; SL_LOGW("%s:%d: pthread %p (tid %d) sees object %p was locked by pthread %p" " (tid %d) at %s:%d\n", file, line, *(void **)&me, gettid(), thiz, *(void **)&owner, ownerTid, thiz->mFile, thiz->mLine); } forward_progress: // attempt one more time without timeout; maybe this time we will be successful ok = pthread_mutex_lock(&thiz->mMutex); assert(0 == ok); break; } } } // here if mutex was successfully locked pthread_t zero; memset(&zero, 0, sizeof(pthread_t)); if (0 != memcmp(&zero, &thiz->mOwner, sizeof(pthread_t))) { pthread_t me = pthread_self(); pthread_t owner = thiz->mOwner; pid_t ownerTid = LIKELY_VALID(owner) ? __pthread_gettid(owner) : -1; if (pthread_equal(pthread_self(), owner)) { SL_LOGE("%s:%d: pthread %p (tid %d) sees object %p was recursively locked by pthread" " %p (tid %d) at %s:%d\n", file, line, *(void **)&me, gettid(), thiz, *(void **)&owner, ownerTid, thiz->mFile, thiz->mLine); } else { SL_LOGE("%s:%d: pthread %p (tid %d) sees object %p was left unlocked in unexpected" " state by pthread %p (tid %d) at %s:%d\n", file, line, *(void **)&me, gettid(), thiz, *(void **)&owner, ownerTid, thiz->mFile, thiz->mLine); } assert(false); } thiz->mOwner = pthread_self(); thiz->mFile = file; thiz->mLine = line; // not android_atomic_inc because we are already holding a mutex ++thiz->mGeneration; }
// http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_create.html int timer_create(clockid_t clock_id, sigevent* evp, timer_t* timer_id) { PosixTimer* timer = reinterpret_cast<PosixTimer*>(malloc(sizeof(PosixTimer))); if (timer == NULL) { return -1; } timer->sigev_notify = (evp == NULL) ? SIGEV_SIGNAL : evp->sigev_notify; // If not a SIGEV_THREAD timer, the kernel can handle it without our help. if (timer->sigev_notify != SIGEV_THREAD) { if (__timer_create(clock_id, evp, &timer->kernel_timer_id) == -1) { free(timer); return -1; } *timer_id = timer; return 0; } // Otherwise, this must be SIGEV_THREAD timer... timer->callback = evp->sigev_notify_function; timer->callback_argument = evp->sigev_value; // Check arguments that the kernel doesn't care about but we do. if (timer->callback == NULL) { free(timer); errno = EINVAL; return -1; } // Create this timer's thread. pthread_attr_t thread_attributes; if (evp->sigev_notify_attributes == NULL) { pthread_attr_init(&thread_attributes); } else { thread_attributes = *reinterpret_cast<pthread_attr_t*>(evp->sigev_notify_attributes); } pthread_attr_setdetachstate(&thread_attributes, PTHREAD_CREATE_DETACHED); // We start the thread with TIMER_SIGNAL blocked by blocking the signal here and letting it // inherit. If it tried to block the signal itself, there would be a race. kernel_sigset_t sigset; sigaddset(sigset.get(), TIMER_SIGNAL); kernel_sigset_t old_sigset; pthread_sigmask(SIG_BLOCK, sigset.get(), old_sigset.get()); int rc = pthread_create(&timer->callback_thread, &thread_attributes, __timer_thread_start, timer); pthread_sigmask(SIG_SETMASK, old_sigset.get(), NULL); if (rc != 0) { free(timer); errno = rc; return -1; } sigevent se = *evp; se.sigev_signo = TIMER_SIGNAL; se.sigev_notify = SIGEV_THREAD_ID; se.sigev_notify_thread_id = __pthread_gettid(timer->callback_thread); if (__timer_create(clock_id, &se, &timer->kernel_timer_id) == -1) { __timer_thread_stop(timer); return -1; } // Give the thread a meaningful name. // It can't do this itself because the kernel timer isn't created until after it's running. char name[32]; snprintf(name, sizeof(name), "POSIX interval timer %d", to_kernel_timer_id(timer)); pthread_setname_np(timer->callback_thread, name); *timer_id = timer; return 0; }