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"
#include <threads.h>
int mtx_trylock(mtx_t *m)
{
int tid, old, own, ret = 0;
pthread_t self;
if (m->_m_type == PTHREAD_MUTEX_NORMAL) {
return a_cas(&m->_m_lock, 0, thrd_busy) & thrd_busy;
} else {
self = __pthread_self();
tid = self->tid;
old = m->_m_lock;
own = old & 0x7fffffff;
if (own == tid) {
if ((unsigned)m->_m_count >= INT_MAX) return thrd_error;
else m->_m_count++;
} else {
if ((own && !(own & 0x40000000)) || a_cas(&m->_m_lock, old, tid)!=old)
return thrd_busy;
}
}
return thrd_success;
}
|