minor locking optimizations
[musl] / src / thread / pthread_mutex_unlock.c
1 #include "pthread_impl.h"
2
3 int pthread_mutex_unlock(pthread_mutex_t *m)
4 {
5         pthread_t self;
6
7         if (m->_m_type != PTHREAD_MUTEX_NORMAL) {
8                 if (!m->_m_lock)
9                         return EPERM;
10                 self = __pthread_self();
11                 if ((m->_m_lock&0x1fffffff) != self->tid)
12                         return EPERM;
13                 if ((m->_m_type&3) == PTHREAD_MUTEX_RECURSIVE && --m->_m_count)
14                         return 0;
15                 if (m->_m_type >= 4) {
16                         self->robust_list.pending = &m->_m_next;
17                         *(void **)m->_m_prev = m->_m_next;
18                         if (m->_m_next) ((void **)m->_m_next)[-1] = m->_m_prev;
19                         a_store(&m->_m_lock, 0);
20                         self->robust_list.pending = 0;
21                 } else {
22                         a_store(&m->_m_lock, 0);
23                 }
24         } else {
25                 a_store(&m->_m_lock, 0);
26         }
27
28         if (m->_m_waiters) __wake(&m->_m_lock, 1, 0);
29         return 0;
30 }