#include "pthread_impl.h" #include int mtx_trylock(mtx_t *m) { int tid, old, own, ret = 0; pthread_t self; if (m->_m_type == PTHREAD_MUTEX_NORMAL) { return a_cas(&m->_m_lock, 0, thrd_busy) & thrd_busy; } else { self = __pthread_self(); tid = self->tid; old = m->_m_lock; own = old & 0x7fffffff; if (own == tid) { if ((unsigned)m->_m_count >= INT_MAX) return thrd_error; else m->_m_count++; } else { if ((own && !(own & 0x40000000)) || a_cas(&m->_m_lock, old, tid)!=old) return thrd_busy; } } return thrd_success; }