spin before waiting on futex in mutex and rwlock lock operations
authorRich Felker <dalias@aerifal.cx>
Tue, 26 Aug 2014 00:16:26 +0000 (20:16 -0400)
committerRich Felker <dalias@aerifal.cx>
Tue, 26 Aug 2014 00:16:26 +0000 (20:16 -0400)
src/thread/pthread_mutex_timedlock.c
src/thread/pthread_rwlock_timedrdlock.c
src/thread/pthread_rwlock_timedwrlock.c

index 2a959d258167985e6696bed10f83a61d529c3c17..116a8b7b3cd4112fc6e34f88f75537f05291307e 100644 (file)
@@ -8,6 +8,12 @@ int pthread_mutex_timedlock(pthread_mutex_t *restrict m, const struct timespec *
 
        int r, t, priv = (m->_m_type & 128) ^ 128;
 
+       r = pthread_mutex_trylock(m);
+       if (r != EBUSY) return r;
+       
+       int spins = 100;
+       while (spins-- && m->_m_lock) a_spin();
+
        while ((r=pthread_mutex_trylock(m)) == EBUSY) {
                if (!(r=m->_m_lock) || ((r&0x40000000) && (m->_m_type&4)))
                        continue;
index a2b4d446a9d96830f860d307fdb60c4e77a74399..884b7a1e52f3ad38cdba7da35f93ff973229d072 100644 (file)
@@ -3,6 +3,13 @@
 int pthread_rwlock_timedrdlock(pthread_rwlock_t *restrict rw, const struct timespec *restrict at)
 {
        int r, t;
+
+       r = pthread_rwlock_tryrdlock(rw);
+       if (r != EBUSY) return r;
+       
+       int spins = 100;
+       while (spins-- && rw->_rw_lock) a_spin();
+
        while ((r=pthread_rwlock_tryrdlock(rw))==EBUSY) {
                if (!(r=rw->_rw_lock) || (r&0x7fffffff)!=0x7fffffff) continue;
                t = r | 0x80000000;
index 63a32ecb9e79be75741fcd8b921e7139a6d90d42..f02b174b08aafc0e807c24df1893fd2a7551f538 100644 (file)
@@ -3,6 +3,13 @@
 int pthread_rwlock_timedwrlock(pthread_rwlock_t *restrict rw, const struct timespec *restrict at)
 {
        int r, t;
+       
+       r = pthread_rwlock_trywrlock(rw);
+       if (r != EBUSY) return r;
+       
+       int spins = 100;
+       while (spins-- && rw->_rw_lock) a_spin();
+
        while ((r=pthread_rwlock_trywrlock(rw))==EBUSY) {
                if (!(r=rw->_rw_lock)) continue;
                t = r | 0x80000000;