2011-02-12 00:22:29 -05:00
|
|
|
#include "pthread_impl.h"
|
|
|
|
|
|
|
|
int pthread_mutex_trylock(pthread_mutex_t *m)
|
|
|
|
{
|
2011-03-08 03:41:05 -05:00
|
|
|
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++;
|
2011-02-12 00:22:29 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-17 17:16:20 -05:00
|
|
|
if (a_xchg(&m->_m_lock, 1))
|
|
|
|
if (m->_m_type == PTHREAD_MUTEX_ERRORCHECK
|
2011-03-08 03:41:05 -05:00
|
|
|
&& m->_m_owner == self->tid) return EDEADLK;
|
2011-02-12 00:22:29 -05:00
|
|
|
else return EBUSY;
|
2011-03-08 03:41:05 -05:00
|
|
|
if (m->_m_type != PTHREAD_MUTEX_NORMAL) {
|
|
|
|
m->_m_owner = self->tid;
|
|
|
|
m->_m_count = 1;
|
|
|
|
}
|
2011-02-12 00:22:29 -05:00
|
|
|
return 0;
|
|
|
|
}
|