#define _m_count __u.__i[5]
#define _c_block __u.__i[0]
#define _c_clock __u.__i[1]
-#define _rw_wrlock __u.__i[0]
-#define _rw_readers __u.__i[1]
-#define _rw_waiters __u.__i[2]
-#define _rw_owner __u.__i[3]
+#define _rw_lock __u.__i[0]
+#define _rw_waiters __u.__i[1]
#define _b_inst __u.__p[0]
#define _b_limit __u.__i[2]
#define _b_lock __u.__i[3]
int pthread_rwlock_rdlock(pthread_rwlock_t *rw)
{
- while (pthread_rwlock_tryrdlock(rw))
- __wait(&rw->_rw_wrlock, &rw->_rw_waiters, 1, 0);
- return 0;
+ return pthread_rwlock_timedrdlock(rw, 0);
}
int pthread_rwlock_timedrdlock(pthread_rwlock_t *rw, const struct timespec *at)
{
- int w=0;
- while (pthread_rwlock_tryrdlock(rw)) {
- if (!w) a_inc(&rw->_rw_waiters), w++;
- if (__timedwait(&rw->_rw_wrlock, 1, CLOCK_REALTIME, at, 0, 0, 0)==ETIMEDOUT) {
- if (w) a_dec(&rw->_rw_waiters);
- return ETIMEDOUT;
- }
+ int r, t;
+ while ((r=pthread_rwlock_tryrdlock(rw))==EBUSY) {
+ if (!(r=rw->_rw_lock) || (r&0x7fffffff)!=0x7fffffff) continue;
+ t = r | 0x80000000;
+ a_inc(&rw->_rw_waiters);
+ a_cas(&rw->_rw_lock, r, t);
+ r = __timedwait(&rw->_rw_lock, t, CLOCK_REALTIME, at, 0, 0, 0);
+ a_dec(&rw->_rw_waiters);
+ if (r && r != EINTR) return r;
}
- if (w) a_dec(&rw->_rw_waiters);
- return 0;
+ return r;
}
int pthread_rwlock_timedwrlock(pthread_rwlock_t *rw, const struct timespec *at)
{
- int nr, *p, w=0;
- while (pthread_rwlock_trywrlock(rw)==EAGAIN) {
- if (!w) a_inc(&rw->_rw_waiters), w++;
- if ((nr=rw->_rw_readers)) p = &rw->_rw_readers;
- else nr=1, p = &rw->_rw_wrlock;
- if (__timedwait(p, nr, CLOCK_REALTIME, at, 0, 0, 0)==ETIMEDOUT) {
- if (w) a_dec(&rw->_rw_waiters);
- return ETIMEDOUT;
- }
+ int r, t;
+ while ((r=pthread_rwlock_trywrlock(rw))==EBUSY) {
+ if (!(r=rw->_rw_lock)) continue;
+ t = r | 0x80000000;
+ a_inc(&rw->_rw_waiters);
+ a_cas(&rw->_rw_lock, r, t);
+ r = __timedwait(&rw->_rw_lock, t, CLOCK_REALTIME, at, 0, 0, 0);
+ a_dec(&rw->_rw_waiters);
+ if (r && r != EINTR) return r;
}
- if (w) a_dec(&rw->_rw_waiters);
- return 0;
+ return r;
}
int pthread_rwlock_tryrdlock(pthread_rwlock_t *rw)
{
- a_inc(&rw->_rw_readers);
- if (rw->_rw_wrlock) {
- a_dec(&rw->_rw_readers);
- if (rw->_rw_waiters && !rw->_rw_readers)
- __wake(&rw->_rw_readers, 1, 0);
- return EBUSY;
- }
+ int val, cnt;
+ do {
+ val = rw->_rw_lock;
+ cnt = val & 0x7fffffff;
+ if (cnt == 0x7fffffff) return EBUSY;
+ if (cnt == 0x7ffffffe) return EAGAIN;
+ } while (a_cas(&rw->_rw_lock, val, val+1) != val);
return 0;
}
int pthread_rwlock_trywrlock(pthread_rwlock_t *rw)
{
- if (a_xchg(&rw->_rw_wrlock, 1))
- return EBUSY;
- if (rw->_rw_readers) {
- a_store(&rw->_rw_wrlock, 0);
- return EBUSY;
- }
- rw->_rw_owner = pthread_self()->tid;
+ if (a_cas(&rw->_rw_lock, 0, 0x7fffffff)) return EBUSY;
return 0;
}
int pthread_rwlock_unlock(pthread_rwlock_t *rw)
{
- struct pthread *self = pthread_self();
- if (rw->_rw_owner == self->tid) {
- rw->_rw_owner = 0;
- a_store(&rw->_rw_wrlock, 0);
- if (rw->_rw_waiters)
- __wake(&rw->_rw_wrlock, -1, 0);
- return 0;
- }
- a_dec(&rw->_rw_readers);
- if (rw->_rw_waiters && !rw->_rw_readers)
- __wake(&rw->_rw_readers, 1, 0);
+ int val, cnt, waiters, new;
+
+ do {
+ val = rw->_rw_lock;
+ cnt = val & 0x7fffffff;
+ waiters = rw->_rw_waiters;
+ new = (cnt == 0x7fffffff || cnt == 1) ? 0 : val-1;
+ } while (a_cas(&rw->_rw_lock, val, new) != val);
+
+ if (!new && (waiters || val<0))
+ __wake(&rw->_rw_lock, 1, 0);
+
return 0;
}
int pthread_rwlock_wrlock(pthread_rwlock_t *rw)
{
- int nr;
- while (pthread_rwlock_trywrlock(rw)==EAGAIN) {
- if ((nr=rw->_rw_readers))
- __wait(&rw->_rw_readers, &rw->_rw_waiters, nr, 0);
- else
- __wait(&rw->_rw_wrlock, &rw->_rw_waiters, 1, 0);
- }
- return 0;
+ return pthread_rwlock_timedwrlock(rw, 0);
}