pthread_rwlock_timedwrlock.c 590 B

1234567891011121314151617181920212223
  1. #include "pthread_impl.h"
  2. int pthread_rwlock_timedwrlock(pthread_rwlock_t *restrict rw, const struct timespec *restrict at)
  3. {
  4. int r, t;
  5. r = pthread_rwlock_trywrlock(rw);
  6. if (r != EBUSY) return r;
  7. int spins = 100;
  8. while (spins-- && rw->_rw_lock) a_spin();
  9. while ((r=pthread_rwlock_trywrlock(rw))==EBUSY) {
  10. if (!(r=rw->_rw_lock)) continue;
  11. t = r | 0x80000000;
  12. a_inc(&rw->_rw_waiters);
  13. a_cas(&rw->_rw_lock, r, t);
  14. r = __timedwait(&rw->_rw_lock, t, CLOCK_REALTIME, at, 0, 0, rw->_rw_shared^128);
  15. a_dec(&rw->_rw_waiters);
  16. if (r && r != EINTR) return r;
  17. }
  18. return r;
  19. }