pthread_mutex_unlock.c 710 B

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