introduce namespace-safe rwlock aliases; use in pthread_key_create
authorRich Felker <dalias@aerifal.cx>
Sat, 16 Feb 2019 16:44:07 +0000 (11:44 -0500)
committerRich Felker <dalias@aerifal.cx>
Sat, 16 Feb 2019 16:44:07 +0000 (11:44 -0500)
commit 84d061d5a31c9c773e29e1e2b1ffe8cb9557bc58 inadvertently
introduced namespace violations by using the pthread-namespace rwlock
functions in pthread_key_create, which is in turn used for C11 tss.
fix that and possible future uses of rwlocks elsewhere.

src/include/pthread.h
src/thread/pthread_key_create.c
src/thread/pthread_rwlock_rdlock.c
src/thread/pthread_rwlock_timedrdlock.c
src/thread/pthread_rwlock_timedwrlock.c
src/thread/pthread_rwlock_tryrdlock.c
src/thread/pthread_rwlock_trywrlock.c
src/thread/pthread_rwlock_unlock.c
src/thread/pthread_rwlock_wrlock.c

index d93ac3a53743380675f146869e06c2cc27ed331a..7167d3e11a11eff076d6fa8066f03df64c616ffc 100644 (file)
@@ -18,5 +18,12 @@ hidden int __private_cond_signal(pthread_cond_t *, int);
 hidden int __pthread_cond_timedwait(pthread_cond_t *restrict, pthread_mutex_t *restrict, const struct timespec *restrict);
 hidden int __pthread_key_create(pthread_key_t *, void (*)(void *));
 hidden int __pthread_key_delete(pthread_key_t);
+hidden int __pthread_rwlock_rdlock(pthread_rwlock_t *);
+hidden int __pthread_rwlock_tryrdlock(pthread_rwlock_t *);
+hidden int __pthread_rwlock_timedrdlock(pthread_rwlock_t *__restrict, const struct timespec *__restrict);
+hidden int __pthread_rwlock_wrlock(pthread_rwlock_t *);
+hidden int __pthread_rwlock_trywrlock(pthread_rwlock_t *);
+hidden int __pthread_rwlock_timedwrlock(pthread_rwlock_t *__restrict, const struct timespec *__restrict);
+hidden int __pthread_rwlock_unlock(pthread_rwlock_t *);
 
 #endif
index da1fb1162d955c03b2716b0a84ed0bfad7f33d26..dc20cc3f3b54d647ebf299bdf189ed61fd38482f 100644 (file)
@@ -32,16 +32,16 @@ int __pthread_key_create(pthread_key_t *k, void (*dtor)(void *))
        /* Purely a sentinel value since null means slot is free. */
        if (!dtor) dtor = nodtor;
 
-       pthread_rwlock_wrlock(&key_lock);
+       __pthread_rwlock_wrlock(&key_lock);
        do {
                if (!keys[j]) {
                        keys[next_key = *k = j] = dtor;
-                       pthread_rwlock_unlock(&key_lock);
+                       __pthread_rwlock_unlock(&key_lock);
                        return 0;
                }
        } while ((j=(j+1)%PTHREAD_KEYS_MAX) != next_key);
 
-       pthread_rwlock_unlock(&key_lock);
+       __pthread_rwlock_unlock(&key_lock);
        return EAGAIN;
 }
 
@@ -57,9 +57,9 @@ int __pthread_key_delete(pthread_key_t k)
        __tl_unlock();
        __restore_sigs(&set);
 
-       pthread_rwlock_wrlock(&key_lock);
+       __pthread_rwlock_wrlock(&key_lock);
        keys[k] = 0;
-       pthread_rwlock_unlock(&key_lock);
+       __pthread_rwlock_unlock(&key_lock);
 
        return 0;
 }
@@ -69,19 +69,19 @@ void __pthread_tsd_run_dtors()
        pthread_t self = __pthread_self();
        int i, j;
        for (j=0; self->tsd_used && j<PTHREAD_DESTRUCTOR_ITERATIONS; j++) {
-               pthread_rwlock_rdlock(&key_lock);
+               __pthread_rwlock_rdlock(&key_lock);
                self->tsd_used = 0;
                for (i=0; i<PTHREAD_KEYS_MAX; i++) {
                        void *val = self->tsd[i];
                        void (*dtor)(void *) = keys[i];
                        self->tsd[i] = 0;
                        if (val && dtor && dtor != nodtor) {
-                               pthread_rwlock_unlock(&key_lock);
+                               __pthread_rwlock_unlock(&key_lock);
                                dtor(val);
-                               pthread_rwlock_rdlock(&key_lock);
+                               __pthread_rwlock_rdlock(&key_lock);
                        }
                }
-               pthread_rwlock_unlock(&key_lock);
+               __pthread_rwlock_unlock(&key_lock);
        }
 }
 
index 0800d21ffd63fba0a97fd81b070a46c5aeb12df1..8546c07d2e790cb0874586e9212a6001e3b85794 100644 (file)
@@ -1,6 +1,8 @@
 #include "pthread_impl.h"
 
-int pthread_rwlock_rdlock(pthread_rwlock_t *rw)
+int __pthread_rwlock_rdlock(pthread_rwlock_t *rw)
 {
-       return pthread_rwlock_timedrdlock(rw, 0);
+       return __pthread_rwlock_timedrdlock(rw, 0);
 }
+
+weak_alias(__pthread_rwlock_rdlock, pthread_rwlock_rdlock);
index 0d5d0d6c05fe8fe5c93999d45dbd76b47ee860b1..8cdd8ecf1775c4a180023ceac61069d76b741e61 100644 (file)
@@ -1,6 +1,6 @@
 #include "pthread_impl.h"
 
-int pthread_rwlock_timedrdlock(pthread_rwlock_t *restrict rw, const struct timespec *restrict at)
+int __pthread_rwlock_timedrdlock(pthread_rwlock_t *restrict rw, const struct timespec *restrict at)
 {
        int r, t;
 
@@ -10,7 +10,7 @@ int pthread_rwlock_timedrdlock(pthread_rwlock_t *restrict rw, const struct times
        int spins = 100;
        while (spins-- && rw->_rw_lock && !rw->_rw_waiters) a_spin();
 
-       while ((r=pthread_rwlock_tryrdlock(rw))==EBUSY) {
+       while ((r=__pthread_rwlock_tryrdlock(rw))==EBUSY) {
                if (!(r=rw->_rw_lock) || (r&0x7fffffff)!=0x7fffffff) continue;
                t = r | 0x80000000;
                a_inc(&rw->_rw_waiters);
@@ -21,3 +21,5 @@ int pthread_rwlock_timedrdlock(pthread_rwlock_t *restrict rw, const struct times
        }
        return r;
 }
+
+weak_alias(__pthread_rwlock_timedrdlock, pthread_rwlock_timedrdlock);
index 7f26dad1f5915e8cf5f81f6a39e9695a071d5005..d77706e6bc208f77ff8e886fe41b600af954e198 100644 (file)
@@ -1,6 +1,6 @@
 #include "pthread_impl.h"
 
-int pthread_rwlock_timedwrlock(pthread_rwlock_t *restrict rw, const struct timespec *restrict at)
+int __pthread_rwlock_timedwrlock(pthread_rwlock_t *restrict rw, const struct timespec *restrict at)
 {
        int r, t;
        
@@ -10,7 +10,7 @@ int pthread_rwlock_timedwrlock(pthread_rwlock_t *restrict rw, const struct times
        int spins = 100;
        while (spins-- && rw->_rw_lock && !rw->_rw_waiters) a_spin();
 
-       while ((r=pthread_rwlock_trywrlock(rw))==EBUSY) {
+       while ((r=__pthread_rwlock_trywrlock(rw))==EBUSY) {
                if (!(r=rw->_rw_lock)) continue;
                t = r | 0x80000000;
                a_inc(&rw->_rw_waiters);
@@ -21,3 +21,5 @@ int pthread_rwlock_timedwrlock(pthread_rwlock_t *restrict rw, const struct times
        }
        return r;
 }
+
+weak_alias(__pthread_rwlock_timedwrlock, pthread_rwlock_timedwrlock);
index fa271fcc6a156a78e9dfe975bf084113e3aa3167..c13bc9cc1979178d573639fa2a6fcfee5cc41cb4 100644 (file)
@@ -1,6 +1,6 @@
 #include "pthread_impl.h"
 
-int pthread_rwlock_tryrdlock(pthread_rwlock_t *rw)
+int __pthread_rwlock_tryrdlock(pthread_rwlock_t *rw)
 {
        int val, cnt;
        do {
@@ -11,3 +11,5 @@ int pthread_rwlock_tryrdlock(pthread_rwlock_t *rw)
        } while (a_cas(&rw->_rw_lock, val, val+1) != val);
        return 0;
 }
+
+weak_alias(__pthread_rwlock_tryrdlock, pthread_rwlock_tryrdlock);
index bb3d3a992d1f3dd8a4919de9945b7fad75786a6a..64d9d3121a70989b01cbb5a1324e131765d72a60 100644 (file)
@@ -1,7 +1,9 @@
 #include "pthread_impl.h"
 
-int pthread_rwlock_trywrlock(pthread_rwlock_t *rw)
+int __pthread_rwlock_trywrlock(pthread_rwlock_t *rw)
 {
        if (a_cas(&rw->_rw_lock, 0, 0x7fffffff)) return EBUSY;
        return 0;
 }
+
+weak_alias(__pthread_rwlock_trywrlock, pthread_rwlock_trywrlock);
index 7b5eec84d833c30673269e19b5467f2b13a05180..9ae27ad2b3a76c05d4038b291469053bf61f62e2 100644 (file)
@@ -1,6 +1,6 @@
 #include "pthread_impl.h"
 
-int pthread_rwlock_unlock(pthread_rwlock_t *rw)
+int __pthread_rwlock_unlock(pthread_rwlock_t *rw)
 {
        int val, cnt, waiters, new, priv = rw->_rw_shared^128;
 
@@ -16,3 +16,5 @@ int pthread_rwlock_unlock(pthread_rwlock_t *rw)
 
        return 0;
 }
+
+weak_alias(__pthread_rwlock_unlock, pthread_rwlock_unlock);
index 7f33535c7d9bb08f3fc0ce7db13edc0de70f24a1..46a3b3a5b0af073191b21a21e99f6ab0376fc154 100644 (file)
@@ -1,6 +1,8 @@
 #include "pthread_impl.h"
 
-int pthread_rwlock_wrlock(pthread_rwlock_t *rw)
+int __pthread_rwlock_wrlock(pthread_rwlock_t *rw)
 {
-       return pthread_rwlock_timedwrlock(rw, 0);
+       return __pthread_rwlock_timedwrlock(rw, 0);
 }
+
+weak_alias(__pthread_rwlock_wrlock, pthread_rwlock_wrlock);