7ff4f703b9ff1e68bc365a2bf31e64fdd6faa278
[musl] / src / thread / pthread_mutex_trylock.c
1 #include "pthread_impl.h"
2
3 int pthread_mutex_trylock(pthread_mutex_t *m)
4 {
5         pthread_t self;
6         if (m->_m_type != PTHREAD_MUTEX_NORMAL) {
7                 self = pthread_self();
8                 if (m->_m_type == PTHREAD_MUTEX_RECURSIVE
9                  && m->_m_owner == self->tid) {
10                         if ((unsigned)m->_m_count >= INT_MAX) return EAGAIN;
11                         m->_m_count++;
12                         return 0;
13                 }
14         }
15
16         if (a_xchg(&m->_m_lock, 1))
17                 if (m->_m_type == PTHREAD_MUTEX_ERRORCHECK
18                  && m->_m_owner == self->tid) return EDEADLK;
19                 else return EBUSY;
20         if (m->_m_type != PTHREAD_MUTEX_NORMAL) {
21                 m->_m_owner = self->tid;
22                 m->_m_count = 1;
23         }
24         return 0;
25 }