OSDN Git Service

fix namespace violation in dependencies of mtx_lock
[android-x86/external-musl-libc.git] / src / thread / pthread_mutex_timedlock.c
1 #include "pthread_impl.h"
2
3 int __pthread_mutex_timedlock(pthread_mutex_t *restrict m, const struct timespec *restrict at)
4 {
5         if ((m->_m_type&15) == PTHREAD_MUTEX_NORMAL
6             && !a_cas(&m->_m_lock, 0, EBUSY))
7                 return 0;
8
9         int type = m->_m_type;
10         int r, t, priv = (type & 128) ^ 128;
11
12         r = __pthread_mutex_trylock(m);
13         if (r != EBUSY) return r;
14         
15         int spins = 100;
16         while (spins-- && m->_m_lock && !m->_m_waiters) a_spin();
17
18         while ((r=__pthread_mutex_trylock(m)) == EBUSY) {
19                 r = m->_m_lock;
20                 int own = r & 0x3fffffff;
21                 if (!own && (!r || (type&4)))
22                         continue;
23                 if ((type&3) == PTHREAD_MUTEX_ERRORCHECK
24                     && own == __pthread_self()->tid)
25                         return EDEADLK;
26
27                 a_inc(&m->_m_waiters);
28                 t = r | 0x80000000;
29                 a_cas(&m->_m_lock, r, t);
30                 r = __timedwait(&m->_m_lock, t, CLOCK_REALTIME, at, priv);
31                 a_dec(&m->_m_waiters);
32                 if (r && r != EINTR) break;
33         }
34         return r;
35 }
36
37 weak_alias(__pthread_mutex_timedlock, pthread_mutex_timedlock);