Author: Armin Rigo <ar...@tunes.org>
Branch: nogil-unsafe-2
Changeset: r92163:3f8621fd99f2
Date: 2017-08-17 16:28 +0200
http://bitbucket.org/pypy/pypy/changeset/3f8621fd99f2/

Log:    Really need a read-write lock here, not a reentrant mutex

diff --git a/rpython/translator/c/src/threadlocal.c 
b/rpython/translator/c/src/threadlocal.c
--- a/rpython/translator/c/src/threadlocal.c
+++ b/rpython/translator/c/src/threadlocal.c
@@ -11,9 +11,10 @@
 #include "src/thread.h"
 
 
-/* this is a reentrant lock that must be acquired around each 
doubly-linked-list
-   manipulation (because such manipulations can occur without the GIL) */
-static pthread_mutex_t _rpy_threadlocal_lock;
+/* this is a read-write lock that must be acquired around each
+   doubly-linked-list access or manipulation (because such manipulations
+   can occur without the GIL) */
+static pthread_rwlockattr_t _rpy_threadlocal_lock;
 
 static int check_valid(void);
 
@@ -27,20 +28,23 @@
 
 static void init_lock(void)
 {
-    pthread_mutexattr_t attr;
-    do_check(pthread_mutexattr_init(&attr)
-          || pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE)
-          || pthread_mutex_init(&_rpy_threadlocal_lock, &attr)
-          || pthread_mutexattr_destroy(&attr));
+    do_check(pthread_rwlock_init(&_rpy_threadlocal_lock, NULL));
 }
 
-void _RPython_ThreadLocals_Acquire(void) {
-    do_check(pthread_mutex_lock(&_rpy_threadlocal_lock));
+void _RPython_ThreadLocals_Acquire(void)
+{
+    do_check(pthread_rwlock_wrlock(&_rpy_threadlocal_lock));
     assert(check_valid());
 }
-void _RPython_ThreadLocals_Release(void) {
+void _RPython_ThreadLocals_ReadOnlyAcquire(void)
+{
+    do_check(pthread_rwlock_rdlock(&_rpy_threadlocal_lock));
     assert(check_valid());
-    do_check(pthread_mutex_unlock(&_rpy_threadlocal_lock));
+}
+void _RPython_ThreadLocals_Release(void)
+{
+    assert(check_valid());
+    do_check(pthread_rwlock_unlock(&_rpy_threadlocal_lock));
 }
 
 
diff --git a/rpython/translator/c/src/threadlocal.h 
b/rpython/translator/c/src/threadlocal.h
--- a/rpython/translator/c/src/threadlocal.h
+++ b/rpython/translator/c/src/threadlocal.h
@@ -20,6 +20,7 @@
 RPY_EXTERN char *_RPython_ThreadLocals_Build(void);
 
 RPY_EXTERN void _RPython_ThreadLocals_Acquire(void);
+RPY_EXTERN void _RPython_ThreadLocals_ReadOnlyAcquire(void);
 RPY_EXTERN void _RPython_ThreadLocals_Release(void);
 
 /* Must acquire/release the thread-local lock around a series of calls
@@ -30,7 +31,7 @@
 /* will return the head of the list */
 RPY_EXTERN struct pypy_threadlocal_s *_RPython_ThreadLocals_Head();
 
-#define OP_THREADLOCALREF_ACQUIRE(r)   _RPython_ThreadLocals_Acquire()
+#define OP_THREADLOCALREF_ACQUIRE(r)   _RPython_ThreadLocals_ReadOnlyAcquire()
 #define OP_THREADLOCALREF_RELEASE(r)   _RPython_ThreadLocals_Release()
 #define OP_THREADLOCALREF_ENUM(p, r)   r = _RPython_ThreadLocals_Enum(p)
 
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to