aboutsummaryrefslogtreecommitdiff
path: root/lib/libc/wasi/thread-stub/pthread_mutex_trylock.c
blob: 9f00f893fc916ac554ecace2affe24327b67cf1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "pthread_impl.h"

int __pthread_mutex_trylock(pthread_mutex_t *m)
{
	/*
		_m_type[1:0] 	- type
		0 - normal
		1 - recursive
		2 - errorcheck
	*/
	if (m->_m_type&3 != PTHREAD_MUTEX_RECURSIVE) {
		if (m->_m_count) return EBUSY;
		m->_m_count = 1;
	} else {
		if ((unsigned)m->_m_count >= INT_MAX) return EAGAIN;
		m->_m_count++;
	}
	return 0;
}

weak_alias(__pthread_mutex_trylock, pthread_mutex_trylock);