about summary refs log tree commit diff
path: root/src/thread/pthread_mutex_trylock.c
blob: 29268fdb0fa772f02e06dddf51bf00c851bebe88 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include "pthread_impl.h"

int pthread_mutex_trylock(pthread_mutex_t *m)
{
	if (m->_m_type == PTHREAD_MUTEX_RECURSIVE) {
		pthread_t self = pthread_self();
		if (m->_m_owner == self->tid) {
			if ((unsigned)m->_m_lock >= INT_MAX) return EAGAIN;
			a_inc(&m->_m_lock);
			return 0;
		}
		if (a_fetch_add(&m->_m_lock, 1)) {
			if (a_fetch_add(&m->_m_lock, -1)==1 && m->_m_waiters)
				__wake(&m->_m_lock, 1, 0);
			return EBUSY;
		}
		m->_m_owner = self->tid;
		return 0;
	}

	if (a_xchg(&m->_m_lock, 1))
		if (m->_m_type == PTHREAD_MUTEX_ERRORCHECK
		 && m->_m_owner == pthread_self()->tid) return EDEADLK;
		else return EBUSY;
	if (m->_m_type == PTHREAD_MUTEX_ERRORCHECK)
		m->_m_owner = pthread_self()->tid;
	return 0;
}