9f7496485fa939f788b0b212e13224af6ad63762
[musl] / src / thread / pthread_rwlock_timedwrlock.c
1 #include "pthread_impl.h"
2
3 int pthread_rwlock_timedwrlock(pthread_rwlock_t *rw, const struct timespec *at)
4 {
5         int nr, *p, w=0;
6         while (pthread_rwlock_trywrlock(rw)==EAGAIN) {
7                 if (!w) a_inc(&rw->__waiters), w++;
8                 if ((nr=rw->__readers)) p = &rw->__readers;
9                 else nr=1, p = &rw->__wrlock;
10                 if (__timedwait(p, nr, CLOCK_REALTIME, at, 0)==ETIMEDOUT) {
11                         if (w) a_dec(&rw->__waiters);
12                         return ETIMEDOUT;
13                 }
14         }
15         if (w) a_dec(&rw->__waiters);
16         return 0;
17 }