1 #include "pthread_impl.h"
3 int __pthread_mutex_trylock_owner(pthread_mutex_t *m)
7 pthread_t self = __pthread_self();
11 own = old & 0x3fffffff;
13 if ((type&8) && m->_m_count<0) {
18 if ((type&3) == PTHREAD_MUTEX_RECURSIVE) {
19 if ((unsigned)m->_m_count >= INT_MAX) return EAGAIN;
24 if (own == 0x3fffffff) return ENOTRECOVERABLE;
25 if (own || (old && !(type & 4))) return EBUSY;
28 if (!self->robust_list.off) {
29 self->robust_list.off = (char*)&m->_m_lock-(char *)&m->_m_next;
30 __syscall(SYS_set_robust_list, &self->robust_list, 3*sizeof(long));
32 if (m->_m_waiters) tid |= 0x80000000;
33 self->robust_list.pending = &m->_m_next;
35 tid |= old & 0x40000000;
37 if (a_cas(&m->_m_lock, old, tid) != old) {
38 self->robust_list.pending = 0;
39 if ((type&12)==12 && m->_m_waiters) return ENOTRECOVERABLE;
44 if ((type&8) && m->_m_waiters) {
45 int priv = (type & 128) ^ 128;
46 __syscall(SYS_futex, &m->_m_lock, FUTEX_UNLOCK_PI|priv);
47 self->robust_list.pending = 0;
48 return (type&4) ? ENOTRECOVERABLE : EBUSY;
51 volatile void *next = self->robust_list.head;
53 m->_m_prev = &self->robust_list.head;
54 if (next != &self->robust_list.head) *(volatile void *volatile *)
55 ((char *)next - sizeof(void *)) = &m->_m_next;
56 self->robust_list.head = &m->_m_next;
57 self->robust_list.pending = 0;
67 int __pthread_mutex_trylock(pthread_mutex_t *m)
69 if ((m->_m_type&15) == PTHREAD_MUTEX_NORMAL)
70 return a_cas(&m->_m_lock, 0, EBUSY) & EBUSY;
71 return __pthread_mutex_trylock_owner(m);
74 weak_alias(__pthread_mutex_trylock, pthread_mutex_trylock);