]> granicus.if.org Git - gc/commitdiff
Define SET/UNSET_MARK_LOCK_HOLDER macros to set/check GC_acquire_mark_lock
authorIvan Maidanski <ivmai@mail.ru>
Mon, 15 Jul 2013 12:45:52 +0000 (16:45 +0400)
committerIvan Maidanski <ivmai@mail.ru>
Mon, 15 Jul 2013 12:45:52 +0000 (16:45 +0400)
(refactoring)

* pthread_support.c (SET_MARK_LOCK_HOLDER, UNSET_MARK_LOCK_HOLDER):
New macro (defined to no action unless GC_ASSERTIONS).
* win32_threads.c (SET_MARK_LOCK_HOLDER, UNSET_MARK_LOCK_HOLDER):
Likewise.
* pthread_support.c (GC_acquire_mark_lock, GC_release_mark_lock,
GC_wait_builder, GC_wait_marker): Use [UN]SET_MARK_LOCK_HOLDER
(instead of GC_mark_lock_holder setters and checkers).
* win32_threads.c (GC_acquire_mark_lock, GC_release_mark_lock,
GC_wait_builder, GC_wait_marker): Likewise.
* win32_threads.c (GC_mark_lock_holder): Change type to DWORD unless
GC_PTHREADS_PARAMARK defined.
* win32_threads.c (GC_acquire_mark_lock, GC_notify_all_builder):
Remove unnecessary cast of GetCurrentThreadId() result.

pthread_support.c
win32_threads.c

index 334be501a592c3a0fedba5c4f6687baa6f787eec..e851907fbe61ec6b55585ba9ed1609c9da5e4843 100644 (file)
@@ -1943,7 +1943,18 @@ GC_INNER void GC_lock(void)
 
 # ifdef GC_ASSERTIONS
     STATIC unsigned long GC_mark_lock_holder = NO_THREAD;
-# endif
+#   define SET_MARK_LOCK_HOLDER \
+                (void)(GC_mark_lock_holder = NUMERIC_THREAD_ID(pthread_self()))
+#   define UNSET_MARK_LOCK_HOLDER \
+                do { \
+                  GC_ASSERT(GC_mark_lock_holder \
+                                == NUMERIC_THREAD_ID(pthread_self())); \
+                  GC_mark_lock_holder = NO_THREAD; \
+                } while (0)
+# else
+#   define SET_MARK_LOCK_HOLDER (void)0
+#   define UNSET_MARK_LOCK_HOLDER (void)0
+# endif /* !GC_ASSERTIONS */
 
 #ifdef GLIBC_2_1_MUTEX_HACK
   /* Ugly workaround for a linux threads bug in the final versions      */
@@ -1965,17 +1976,12 @@ GC_INNER void GC_acquire_mark_lock(void)
 {
     GC_ASSERT(GC_mark_lock_holder != NUMERIC_THREAD_ID(pthread_self()));
     GC_generic_lock(&mark_mutex);
-#   ifdef GC_ASSERTIONS
-        GC_mark_lock_holder = NUMERIC_THREAD_ID(pthread_self());
-#   endif
+    SET_MARK_LOCK_HOLDER;
 }
 
 GC_INNER void GC_release_mark_lock(void)
 {
-    GC_ASSERT(GC_mark_lock_holder == NUMERIC_THREAD_ID(pthread_self()));
-#   ifdef GC_ASSERTIONS
-        GC_mark_lock_holder = NO_THREAD;
-#   endif
+    UNSET_MARK_LOCK_HOLDER;
     if (pthread_mutex_unlock(&mark_mutex) != 0) {
         ABORT("pthread_mutex_unlock failed");
     }
@@ -1988,18 +1994,13 @@ GC_INNER void GC_release_mark_lock(void)
 /*    free-list link may be ignored.                                    */
 STATIC void GC_wait_builder(void)
 {
-    GC_ASSERT(GC_mark_lock_holder == NUMERIC_THREAD_ID(pthread_self()));
     ASSERT_CANCEL_DISABLED();
-#   ifdef GC_ASSERTIONS
-        GC_mark_lock_holder = NO_THREAD;
-#   endif
+    UNSET_MARK_LOCK_HOLDER;
     if (pthread_cond_wait(&builder_cv, &mark_mutex) != 0) {
         ABORT("pthread_cond_wait failed");
     }
     GC_ASSERT(GC_mark_lock_holder == NO_THREAD);
-#   ifdef GC_ASSERTIONS
-        GC_mark_lock_holder = NUMERIC_THREAD_ID(pthread_self());
-#   endif
+    SET_MARK_LOCK_HOLDER;
 }
 
 GC_INNER void GC_wait_for_reclaim(void)
@@ -2023,18 +2024,13 @@ static pthread_cond_t mark_cv = PTHREAD_COND_INITIALIZER;
 
 GC_INNER void GC_wait_marker(void)
 {
-    GC_ASSERT(GC_mark_lock_holder == NUMERIC_THREAD_ID(pthread_self()));
     ASSERT_CANCEL_DISABLED();
-#   ifdef GC_ASSERTIONS
-        GC_mark_lock_holder = NO_THREAD;
-#   endif
+    UNSET_MARK_LOCK_HOLDER;
     if (pthread_cond_wait(&mark_cv, &mark_mutex) != 0) {
         ABORT("pthread_cond_wait failed");
     }
     GC_ASSERT(GC_mark_lock_holder == NO_THREAD);
-#   ifdef GC_ASSERTIONS
-        GC_mark_lock_holder = NUMERIC_THREAD_ID(pthread_self());
-#   endif
+    SET_MARK_LOCK_HOLDER;
 }
 
 GC_INNER void GC_notify_all_marker(void)
index 2462c2a31c7d8213ae77c316eb783ebe2c4ca97f..4fd1d92807ca74d34c4ef5e46d2eb1bb70fd72af 100644 (file)
@@ -1716,8 +1716,9 @@ GC_INNER void GC_get_next_stack(char *start, char *limit,
     }
   }
 
-# ifdef GC_ASSERTIONS
-    STATIC unsigned long GC_mark_lock_holder = NO_THREAD;
+# ifndef GC_ASSERTIONS
+#   define SET_MARK_LOCK_HOLDER (void)0
+#   define UNSET_MARK_LOCK_HOLDER (void)0
 # endif
 
   /* GC_mark_threads[] is unused here unlike that in pthread_support.c  */
@@ -1772,6 +1773,18 @@ GC_INNER void GC_get_next_stack(char *start, char *limit,
       GC_COND_LOG_PRINTF("Started %d mark helper threads\n", GC_markers_m1);
     }
 
+#   ifdef GC_ASSERTIONS
+      STATIC unsigned long GC_mark_lock_holder = NO_THREAD;
+#     define SET_MARK_LOCK_HOLDER \
+                (void)(GC_mark_lock_holder = NUMERIC_THREAD_ID(pthread_self()))
+#     define UNSET_MARK_LOCK_HOLDER \
+                do { \
+                  GC_ASSERT(GC_mark_lock_holder \
+                                == NUMERIC_THREAD_ID(pthread_self())); \
+                  GC_mark_lock_holder = NO_THREAD; \
+                } while (0)
+#   endif /* GC_ASSERTIONS */
+
     static pthread_mutex_t mark_mutex = PTHREAD_MUTEX_INITIALIZER;
 
     static pthread_cond_t builder_cv = PTHREAD_COND_INITIALIZER;
@@ -1794,17 +1807,12 @@ GC_INNER void GC_get_next_stack(char *start, char *limit,
         (void)AO_fetch_and_add1(&GC_block_count);
 #     endif
       /* GC_generic_lock(&mark_mutex); */
-#     ifdef GC_ASSERTIONS
-        GC_mark_lock_holder = NUMERIC_THREAD_ID(pthread_self());
-#     endif
+      SET_MARK_LOCK_HOLDER;
     }
 
     GC_INNER void GC_release_mark_lock(void)
     {
-      GC_ASSERT(GC_mark_lock_holder == NUMERIC_THREAD_ID(pthread_self()));
-#     ifdef GC_ASSERTIONS
-        GC_mark_lock_holder = NO_THREAD;
-#     endif
+      UNSET_MARK_LOCK_HOLDER;
       if (pthread_mutex_unlock(&mark_mutex) != 0) {
         ABORT("pthread_mutex_unlock failed");
       }
@@ -1817,17 +1825,12 @@ GC_INNER void GC_get_next_stack(char *start, char *limit,
     /* since the free-list link may be ignored.                         */
     STATIC void GC_wait_builder(void)
     {
-      GC_ASSERT(GC_mark_lock_holder == NUMERIC_THREAD_ID(pthread_self()));
-#     ifdef GC_ASSERTIONS
-        GC_mark_lock_holder = NO_THREAD;
-#     endif
+      UNSET_MARK_LOCK_HOLDER;
       if (pthread_cond_wait(&builder_cv, &mark_mutex) != 0) {
         ABORT("pthread_cond_wait failed");
       }
       GC_ASSERT(GC_mark_lock_holder == NO_THREAD);
-#     ifdef GC_ASSERTIONS
-        GC_mark_lock_holder = NUMERIC_THREAD_ID(pthread_self());
-#     endif
+      SET_MARK_LOCK_HOLDER;
     }
 
     GC_INNER void GC_wait_for_reclaim(void)
@@ -1851,17 +1854,12 @@ GC_INNER void GC_get_next_stack(char *start, char *limit,
 
     GC_INNER void GC_wait_marker(void)
     {
-      GC_ASSERT(GC_mark_lock_holder == NUMERIC_THREAD_ID(pthread_self()));
-#     ifdef GC_ASSERTIONS
-        GC_mark_lock_holder = NO_THREAD;
-#     endif
+      UNSET_MARK_LOCK_HOLDER;
       if (pthread_cond_wait(&mark_cv, &mark_mutex) != 0) {
         ABORT("pthread_cond_wait failed");
       }
       GC_ASSERT(GC_mark_lock_holder == NO_THREAD);
-#     ifdef GC_ASSERTIONS
-        GC_mark_lock_holder = NUMERIC_THREAD_ID(pthread_self());
-#     endif
+      SET_MARK_LOCK_HOLDER;
     }
 
     GC_INNER void GC_notify_all_marker(void)
@@ -1953,6 +1951,17 @@ GC_INNER void GC_get_next_stack(char *start, char *limit,
       }
     }
 
+#   ifdef GC_ASSERTIONS
+      STATIC DWORD GC_mark_lock_holder = NO_THREAD;
+#     define SET_MARK_LOCK_HOLDER \
+                (void)(GC_mark_lock_holder = GetCurrentThreadId())
+#     define UNSET_MARK_LOCK_HOLDER \
+                do { \
+                  GC_ASSERT(GC_mark_lock_holder == GetCurrentThreadId()); \
+                  GC_mark_lock_holder = NO_THREAD; \
+                } while (0)
+#   endif /* GC_ASSERTIONS */
+
 #   ifdef DONT_USE_SIGNALANDWAIT
       STATIC /* volatile */ LONG GC_mark_mutex_state = 0;
                                 /* Mutex state: 0 - unlocked,           */
@@ -1972,7 +1981,7 @@ GC_INNER void GC_get_next_stack(char *start, char *limit,
 
     GC_INNER void GC_acquire_mark_lock(void)
     {
-      GC_ASSERT(GC_mark_lock_holder != (unsigned long)GetCurrentThreadId());
+      GC_ASSERT(GC_mark_lock_holder != GetCurrentThreadId());
 #     ifdef DONT_USE_SIGNALANDWAIT
         if (InterlockedExchange(&GC_mark_mutex_state, 1 /* locked */) != 0)
 #     else
@@ -1999,17 +2008,12 @@ GC_INNER void GC_get_next_stack(char *start, char *limit,
 #     endif
 
       GC_ASSERT(GC_mark_lock_holder == NO_THREAD);
-#     ifdef GC_ASSERTIONS
-        GC_mark_lock_holder = (unsigned long)GetCurrentThreadId();
-#     endif
+      SET_MARK_LOCK_HOLDER;
     }
 
     GC_INNER void GC_release_mark_lock(void)
     {
-      GC_ASSERT(GC_mark_lock_holder == (unsigned long)GetCurrentThreadId());
-#     ifdef GC_ASSERTIONS
-        GC_mark_lock_holder = NO_THREAD;
-#     endif
+      UNSET_MARK_LOCK_HOLDER;
 #     ifdef DONT_USE_SIGNALANDWAIT
         if (InterlockedExchange(&GC_mark_mutex_state, 0 /* unlocked */) < 0)
 #     else
@@ -2048,7 +2052,7 @@ GC_INNER void GC_get_next_stack(char *start, char *limit,
 
     GC_INNER void GC_notify_all_builder(void)
     {
-      GC_ASSERT(GC_mark_lock_holder == (unsigned long)GetCurrentThreadId());
+      GC_ASSERT(GC_mark_lock_holder == GetCurrentThreadId());
       GC_ASSERT(builder_cv != 0);
       GC_ASSERT(GC_fl_builder_count == 0);
       if (SetEvent(builder_cv) == FALSE)
@@ -2129,11 +2133,7 @@ GC_INNER void GC_get_next_stack(char *start, char *limit,
 
         /* We inline GC_release_mark_lock() to have atomic              */
         /* unlock-and-wait action here.                                 */
-        GC_ASSERT(GC_mark_lock_holder == (unsigned long)GetCurrentThreadId());
-#       ifdef GC_ASSERTIONS
-          GC_mark_lock_holder = NO_THREAD;
-#       endif
-
+        UNSET_MARK_LOCK_HOLDER;
         if ((waitcnt = AO_load(&GC_mark_mutex_waitcnt)) > 1) {
           (void)AO_fetch_and_sub1_release(&GC_mark_mutex_waitcnt);
         } else {
@@ -2156,9 +2156,7 @@ GC_INNER void GC_get_next_stack(char *start, char *limit,
           if (WaitForSingleObject(mark_mutex_event, INFINITE) == WAIT_FAILED)
             ABORT("WaitForSingleObject failed");
           GC_ASSERT(GC_mark_lock_holder == NO_THREAD);
-#         ifdef GC_ASSERTIONS
-            GC_mark_lock_holder = (unsigned long)GetCurrentThreadId();
-#         endif
+          SET_MARK_LOCK_HOLDER;
         }
       }