- pthread_t self = pthread_self ();
-
- /* If the count is > 0 then someone had the lock, maybe us. If it is
- * 0, then it might immediately change right after we read it,
- * but it will be changed by another thread; i.e. if we read 0,
- * we assume that this thread doesn't have the lock.
- *
- * Not 100% sure this is safe, but ... seems like it should be.
- */
- if (pmutex->count == 0)
- {
- /* We know we don't have the lock; someone may have the lock. */
-
- PTHREAD_CHECK ("pthread_mutex_lock", pthread_mutex_lock (&pmutex->lock));
-
- /* We now have the lock. Count must be 0 since it must be 0 when
- * the lock is released by another thread, and we just now got
- * the lock.
- */
- _dbus_assert (pmutex->count == 0);
-
- pmutex->holder = self;
- pmutex->count = 1;
- }
- else
- {
- /* We know someone had the lock, possibly us. Thus
- * pmutex->holder is not pointing to junk, though it may not be
- * the lock holder anymore if the lock holder is not us. If the
- * lock holder is us, then we definitely have the lock.
- */
-
- if (pthread_equal (pmutex->holder, self))
- {
- /* We already have the lock. */
- _dbus_assert (pmutex->count > 0);
- }
- else
- {
- /* Wait for the lock */
- PTHREAD_CHECK ("pthread_mutex_lock", pthread_mutex_lock (&pmutex->lock));
- pmutex->holder = self;
- _dbus_assert (pmutex->count == 0);
- }
-
- pmutex->count += 1;
- }