[pypy-commit] stmgc c7-refactor: Fix _has_mutex() to actually work if there are several threads running

arigo noreply at buildbot.pypy.org
Mon Feb 24 17:12:09 CET 2014


Author: Armin Rigo <arigo at tunes.org>
Branch: c7-refactor
Changeset: r838:707536c3c87f
Date: 2014-02-24 17:11 +0100
http://bitbucket.org/pypy/stmgc/changeset/707536c3c87f/

Log:	Fix _has_mutex() to actually work if there are several threads
	running

diff --git a/c7/stm/sync.c b/c7/stm/sync.c
--- a/c7/stm/sync.c
+++ b/c7/stm/sync.c
@@ -45,6 +45,14 @@
     memset(&sync_ctl, 0, sizeof(sync_ctl.in_use));
 }
 
+#ifndef NDEBUG
+__thread bool _has_mutex_here;
+static inline bool _has_mutex(void)
+{
+    return _has_mutex_here;
+}
+#endif
+
 static void set_gs_register(char *value)
 {
     if (UNLIKELY(syscall(SYS_arch_prctl, ARCH_SET_GS, (uint64_t)value) != 0))
@@ -53,8 +61,10 @@
 
 static inline void mutex_lock(void)
 {
+    assert(!_has_mutex_here);
     if (UNLIKELY(pthread_mutex_lock(&sync_ctl.global_mutex) != 0))
         stm_fatalerror("pthread_mutex_lock: %m\n");
+    assert((_has_mutex_here = true, 1));
 
     if (STM_PSEGMENT->transaction_state == TS_MUST_ABORT)
         abort_with_mutex();
@@ -65,19 +75,10 @@
     assert(STM_PSEGMENT->safe_point == SP_NO_TRANSACTION ||
            STM_PSEGMENT->safe_point == SP_RUNNING);
 
+    assert(_has_mutex_here);
     if (UNLIKELY(pthread_mutex_unlock(&sync_ctl.global_mutex) != 0))
         stm_fatalerror("pthread_mutex_unlock: %m\n");
-}
-
-static inline bool _has_mutex(void)
-{
-    if (pthread_mutex_trylock(&sync_ctl.global_mutex) == EBUSY) {
-        return true;
-    }
-    else {
-        pthread_mutex_unlock(&sync_ctl.global_mutex);
-        return false;
-    }
+    assert((_has_mutex_here = false, 1));
 }
 
 static inline void cond_wait(void)
@@ -87,6 +88,7 @@
     abort();
 #endif
 
+    assert(_has_mutex_here);
     if (UNLIKELY(pthread_cond_wait(&sync_ctl.global_cond,
                                    &sync_ctl.global_mutex) != 0))
         stm_fatalerror("pthread_cond_wait: %m\n");
diff --git a/c7/stm/sync.h b/c7/stm/sync.h
--- a/c7/stm/sync.h
+++ b/c7/stm/sync.h
@@ -8,7 +8,9 @@
 static void mutex_unlock(void);
 static void cond_wait(void);
 static void cond_broadcast(void);
+#ifndef NDEBUG
 static bool _has_mutex(void);
+#endif
 
 /* acquire and release one of the segments for running the given thread
    (must have the mutex acquired!) */


More information about the pypy-commit mailing list