1 #include "pthread_impl.h"
2 
__pthread_rwlock_timedrdlock(pthread_rwlock_t *restrict rw, const struct timespec *restrict at)3 int __pthread_rwlock_timedrdlock(pthread_rwlock_t *restrict rw, const struct timespec *restrict at)
4 {
5 	int r, t;
6 	int clock = (rw->_rw_clock == CLOCK_MONOTONIC) ? CLOCK_MONOTONIC : CLOCK_REALTIME;
7 
8 	r = pthread_rwlock_tryrdlock(rw);
9 	if (r != EBUSY) return r;
10 
11 	int spins = 100;
12 	while (spins-- && rw->_rw_lock && !rw->_rw_waiters) a_spin();
13 
14 	while ((r=__pthread_rwlock_tryrdlock(rw))==EBUSY) {
15 		if (!(r=rw->_rw_lock) || (r&0x7fffffff)!=0x7fffffff) continue;
16 		t = r | 0x80000000;
17 		a_inc(&rw->_rw_waiters);
18 		a_cas(&rw->_rw_lock, r, t);
19 		r = __timedwait(&rw->_rw_lock, t, clock, at, rw->_rw_shared^128);
20 		a_dec(&rw->_rw_waiters);
21 		if (r && r != EINTR) return r;
22 	}
23 	return r;
24 }
25 
26 weak_alias(__pthread_rwlock_timedrdlock, pthread_rwlock_timedrdlock);
27