rework langinfo code for ABI compat and for use by time code
[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
13         if (m->_m_type != PTHREAD_MUTEX_NORMAL) {
14                 if (!m->_m_lock)
15                         return EPERM;
16                 self = pthread_self();
17                 if ((m->_m_lock&0x1fffffff) != self->tid)
18                         return EPERM;
19                 if ((m->_m_type&3) == PTHREAD_MUTEX_RECURSIVE && m->_m_count)
20                         return m->_m_count--, 0;
21                 if (m->_m_type >= 4) {
22                         robust = 1;
23                         self->robust_list.pending = &m->_m_next;
24                         *(void **)m->_m_prev = m->_m_next;
25                         if (m->_m_next) ((void **)m->_m_next)[-1] = m->_m_prev;
26                         __vm_lock_impl(+1);
27                 }
28         }
29         cont = a_swap(&m->_m_lock, 0);
30         if (robust) {
31                 self->robust_list.pending = 0;
32                 __vm_unlock_impl();
33         }
34         if (waiters || cont<0)
35                 __wake(&m->_m_lock, 1, 0);
36         return 0;
37 }