about summary refs log tree commit diff
path: root/src/thread/pthread_mutex_unlock.c
blob: 23e64ac8d24fc25f3b15bd898523fa0c5e4f1a6e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "pthread_impl.h"

int pthread_mutex_unlock(pthread_mutex_t *m)
{
	if (m->__type == PTHREAD_MUTEX_RECURSIVE) {
		if (a_fetch_add(&m->__lock, -1)==1 && m->__waiters)
			__wake(&m->__lock, 1, 0);
		return 0;
	}

	if (m->__type == PTHREAD_MUTEX_ERRORCHECK
	 && m->__owner != pthread_self())
	 	return EPERM;

	m->__owner = 0;
	m->__lock = 0;
	if (m->__waiters) __wake(&m->__lock, 1, 0);
	return 0;
}