unify lock and owner fields of mutex structure
[musl] / src / thread / pthread_mutex_trylock.c
1 #include "pthread_impl.h"
2
3 int pthread_mutex_trylock(pthread_mutex_t *m)
4 {
5         int tid;
6
7         if (m->_m_type == PTHREAD_MUTEX_NORMAL)
8                 return (m->_m_lock || a_swap(&m->_m_lock, 1)) ? EBUSY : 0;
9
10         tid = pthread_self()->tid;
11
12         if (m->_m_lock == tid && m->_m_type == PTHREAD_MUTEX_RECURSIVE) {
13                 if ((unsigned)m->_m_count >= INT_MAX) return EAGAIN;
14                 m->_m_count++;
15                 return 0;
16         }
17
18         if (m->_m_lock || a_cas(&m->_m_lock, 0, tid)) return EBUSY;
19         m->_m_count = 1;
20         return 0;
21 }