pthread_mutex_timedlock.c 382 B

123456789101112131415
  1. #include "pthread_impl.h"
  2. int pthread_mutex_timedlock(pthread_mutex_t *m, const struct timespec *at)
  3. {
  4. int r, w=0;
  5. while ((r=pthread_mutex_trylock(m)) == EBUSY) {
  6. if (!w) a_inc(&m->_m_waiters), w++;
  7. if (__timedwait(&m->_m_lock, 1, CLOCK_REALTIME, at, 0) == ETIMEDOUT) {
  8. if (w) a_dec(&m->_m_waiters);
  9. return ETIMEDOUT;
  10. }
  11. }
  12. if (w) a_dec(&m->_m_waiters);
  13. return r;
  14. }