fix and optimize non-default-type mutex behavior
[musl] / src / thread / pthread_mutex_unlock.c
1 #include "pthread_impl.h"
2
3 int pthread_mutex_unlock(pthread_mutex_t *m)
4 {
5         if (m->_m_type != PTHREAD_MUTEX_NORMAL) {
6                 if (m->_m_owner != pthread_self()->tid)
7                         return EPERM;
8                 if (m->_m_type == PTHREAD_MUTEX_RECURSIVE && --m->_m_count)
9                         return 0;
10         }
11
12         m->_m_owner = 0;
13         m->_m_lock = 0;
14         if (m->_m_waiters) __wake(&m->_m_lock, 1, 0);
15         return 0;
16 }