2011-02-12 00:22:29 -05:00
|
|
|
#include "pthread_impl.h"
|
|
|
|
|
2012-09-06 22:44:55 -04:00
|
|
|
int pthread_mutex_timedlock(pthread_mutex_t *restrict m, const struct timespec *restrict at)
|
2011-02-12 00:22:29 -05:00
|
|
|
{
|
2011-08-02 20:31:15 -04:00
|
|
|
int r, t;
|
|
|
|
|
|
|
|
if (m->_m_type == PTHREAD_MUTEX_NORMAL && !a_cas(&m->_m_lock, 0, EBUSY))
|
|
|
|
return 0;
|
|
|
|
|
2011-02-12 00:22:29 -05:00
|
|
|
while ((r=pthread_mutex_trylock(m)) == EBUSY) {
|
2011-03-17 20:41:37 -04:00
|
|
|
if (!(r=m->_m_lock) || (r&0x40000000)) continue;
|
2011-08-02 20:31:15 -04:00
|
|
|
if ((m->_m_type&3) == PTHREAD_MUTEX_ERRORCHECK
|
|
|
|
&& (r&0x1fffffff) == pthread_self()->tid)
|
|
|
|
return EDEADLK;
|
|
|
|
|
|
|
|
a_inc(&m->_m_waiters);
|
|
|
|
t = r | 0x80000000;
|
|
|
|
a_cas(&m->_m_lock, r, t);
|
2011-08-02 21:11:36 -04:00
|
|
|
r = __timedwait(&m->_m_lock, t, CLOCK_REALTIME, at, 0, 0, 0);
|
2011-08-02 20:31:15 -04:00
|
|
|
a_dec(&m->_m_waiters);
|
|
|
|
if (r && r != EINTR) break;
|
2011-02-12 00:22:29 -05:00
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|