about summary refs log tree commit diff
path: root/src/thread/pthread_mutex_trylock.c
blob: 7ff4f703b9ff1e68bc365a2bf31e64fdd6faa278 (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
#include "pthread_impl.h"

int pthread_mutex_trylock(pthread_mutex_t *m)
{
	pthread_t self;
	if (m->_m_type != PTHREAD_MUTEX_NORMAL) {
		self = pthread_self();
		if (m->_m_type == PTHREAD_MUTEX_RECURSIVE
		 && m->_m_owner == self->tid) {
			if ((unsigned)m->_m_count >= INT_MAX) return EAGAIN;
			m->_m_count++;
			return 0;
		}
	}

	if (a_xchg(&m->_m_lock, 1))
		if (m->_m_type == PTHREAD_MUTEX_ERRORCHECK
		 && m->_m_owner == self->tid) return EDEADLK;
		else return EBUSY;
	if (m->_m_type != PTHREAD_MUTEX_NORMAL) {
		m->_m_owner = self->tid;
		m->_m_count = 1;
	}
	return 0;
}