769d6e56fd200fedcc8aae5f876441d65e6a5b81
[musl] / src / thread / pthread_mutex_unlock.c
1 #include "pthread_impl.h"
2
3 void __vm_lock_impl(int);
4 void __vm_unlock_impl(void);
5
6 int pthread_mutex_unlock(pthread_mutex_t *m)
7 {
8         pthread_t self;
9         int waiters = m->_m_waiters;
10         int cont;
11         int robust = 0;
12         int type = m->_m_type & 15;
13         int priv = (m->_m_type & 128) ^ 128;
14
15         if (type != PTHREAD_MUTEX_NORMAL) {
16                 if (!m->_m_lock)
17                         return EPERM;
18                 self = __pthread_self();
19                 if ((m->_m_lock&0x1fffffff) != self->tid)
20                         return EPERM;
21                 if ((type&3) == PTHREAD_MUTEX_RECURSIVE && m->_m_count)
22                         return m->_m_count--, 0;
23                 if (type >= 4) {
24                         robust = 1;
25                         self->robust_list.pending = &m->_m_next;
26                         *(void **)m->_m_prev = m->_m_next;
27                         if (m->_m_next) ((void **)m->_m_next)[-1] = m->_m_prev;
28                         __vm_lock_impl(+1);
29                 }
30         }
31         cont = a_swap(&m->_m_lock, 0);
32         if (robust) {
33                 self->robust_list.pending = 0;
34                 __vm_unlock_impl();
35         }
36         if (waiters || cont<0)
37                 __wake(&m->_m_lock, 1, priv);
38         return 0;
39 }