Move conditional GC_need_to_lock setting to gc_locks.h
authorIvan Maidanski <ivmai@mail.ru>
Fri, 6 Oct 2017 18:26:06 +0000 (21:26 +0300)
committerIvan Maidanski <ivmai@mail.ru>
Fri, 6 Oct 2017 18:26:06 +0000 (21:26 +0300)
(code refactoring)

* include/private/gc_locks.h [GC_PTHREADS && !THREAD_LOCAL_ALLOC
&& !USE_PTHREAD_LOCKS && !GC_ASSERTIONS] (UNCOND_LOCK): Remove
GC_ASSERT(I_DONT_HOLD_LOCK()) because it is no-op (when assertion
checking is off).
* include/private/gc_locks.h [THREADS] (set_need_to_lock): New macro.
* pthread_support.c [!GC_ALWAYS_MULTITHREADED]
(GC_allow_register_threads, WRAP_FUNC(pthread_create)): Replace
GC_need_to_lock=TRUE to set_need_to_lock().
* win32_threads.c [!GC_ALWAYS_MULTITHREADED]
(GC_allow_register_threads, GC_CreateThread, GC_init_parallel):
Likewise.
* win32_threads.c [!MSWINCE && !CYGWIN32 && !GC_ALWAYS_MULTITHREADED]
(GC_beginthreadex): Likewise.
* win32_threads.c [GC_PTHREADS && !GC_ALWAYS_MULTITHREADED]
(GC_pthread_create): Likewise.

include/private/gc_locks.h
pthread_support.c
win32_threads.c

index e744e17..c961869 100644 (file)
                 AO_CLEAR(&GC_allocate_lock); }
 #     else
 #        define UNCOND_LOCK() \
-              { GC_ASSERT(I_DONT_HOLD_LOCK()); \
-                if (AO_test_and_set_acquire(&GC_allocate_lock) == AO_TS_SET) \
+              { if (AO_test_and_set_acquire(&GC_allocate_lock) == AO_TS_SET) \
                   GC_lock(); }
 #        define UNCOND_UNLOCK() AO_CLEAR(&GC_allocate_lock)
 #     endif /* !GC_ASSERTIONS */
 #  if defined(GC_ALWAYS_MULTITHREADED) \
       && (defined(USE_PTHREAD_LOCKS) || defined(USE_SPIN_LOCK))
 #    define GC_need_to_lock TRUE
+#    define set_need_to_lock() (void)0
 #  else
 #    if defined(GC_ALWAYS_MULTITHREADED) && !defined(CPPCHECK)
 #      error Runtime initialization of GC lock is needed!
 #    endif
 #    undef GC_ALWAYS_MULTITHREADED
      GC_EXTERN GC_bool GC_need_to_lock;
+#       define set_need_to_lock() (void)(GC_need_to_lock = TRUE)
+                                        /* We are multi-threaded now.   */
 #  endif
 
 # else /* !THREADS */
index 2fdcbb8..9da8014 100644 (file)
@@ -1648,10 +1648,7 @@ GC_API void GC_CALL GC_allow_register_threads(void)
 {
     /* Check GC is initialized and the current thread is registered. */
     GC_ASSERT(GC_lookup_thread(pthread_self()) != 0);
-
-#   ifndef GC_ALWAYS_MULTITHREADED
-      GC_need_to_lock = TRUE;   /* We are multi-threaded now. */
-#   endif
+    set_need_to_lock();
 }
 
 GC_API int GC_CALL GC_register_my_thread(const struct GC_stack_base *sb)
@@ -1843,10 +1840,7 @@ GC_API int WRAP_FUNC(pthread_create)(pthread_t *new_thread,
       GC_log_printf("About to start new thread from thread %p\n",
                     (void *)pthread_self());
 #   endif
-#   ifndef GC_ALWAYS_MULTITHREADED
-      GC_need_to_lock = TRUE;
-#   endif
-
+    set_need_to_lock();
     result = REAL_FUNC(pthread_create)(new_thread, attr, GC_start_routine, si);
 
     /* Wait until child has been added to the thread table.             */
index dd2df38..a8ac09d 100644 (file)
@@ -730,13 +730,12 @@ GC_API void GC_CALL GC_allow_register_threads(void)
 {
   /* Check GC is initialized and the current thread is registered. */
   GC_ASSERT(GC_lookup_thread_inner(GetCurrentThreadId()) != 0);
-# ifndef GC_ALWAYS_MULTITHREADED
-#   if !defined(GC_NO_THREADS_DISCOVERY) && !defined(PARALLEL_MARK)
+# if !defined(GC_ALWAYS_MULTITHREADED) && !defined(PARALLEL_MARK) \
+     && !defined(GC_NO_THREADS_DISCOVERY)
       /* GC_init() does not call GC_init_parallel() in this case.   */
-      parallel_initialized = TRUE;
-#   endif
-    GC_need_to_lock = TRUE; /* We are multi-threaded now. */
+    parallel_initialized = TRUE;
 # endif
+  set_need_to_lock();
 }
 
 GC_API int GC_CALL GC_register_my_thread(const struct GC_stack_base *sb)
@@ -2257,9 +2256,7 @@ GC_INNER void GC_get_next_stack(char *start, char *limit,
       args -> start = lpStartAddress;
       args -> param = lpParameter;
 
-#     ifndef GC_ALWAYS_MULTITHREADED
-        GC_need_to_lock = TRUE;
-#     endif
+      set_need_to_lock();
       thread_h = CreateThread(lpThreadAttributes, dwStackSize, GC_win32_start,
                               args, dwCreationFlags, lpThreadId);
       if (thread_h == 0) GC_free(args);
@@ -2311,9 +2308,7 @@ GC_INNER void GC_get_next_stack(char *start, char *limit,
         args -> start = (LPTHREAD_START_ROUTINE)start_address;
         args -> param = arglist;
 
-#       ifndef GC_ALWAYS_MULTITHREADED
-          GC_need_to_lock = TRUE;
-#       endif
+        set_need_to_lock();
         thread_h = _beginthreadex(security, stack_size,
                         (unsigned (__stdcall *)(void *))GC_win32_start,
                         args, initflag, thrdaddr);
@@ -2608,9 +2603,7 @@ GC_INNER void GC_thr_init(void)
                       (void *)GC_PTHREAD_PTRVAL(pthread_self()),
                       (long)GetCurrentThreadId());
 #     endif
-#     ifndef GC_ALWAYS_MULTITHREADED
-        GC_need_to_lock = TRUE;
-#     endif
+      set_need_to_lock();
       result = pthread_create(new_thread, attr, GC_pthread_start, si);
 
       if (result) { /* failure */
@@ -2839,15 +2832,13 @@ GC_INNER void GC_init_parallel(void)
 # if defined(CPPCHECK) && !defined(GC_NO_THREADS_DISCOVERY)
     GC_noop1((word)&GC_DllMain);
 # endif
-# ifndef GC_ALWAYS_MULTITHREADED
-    if (GC_win32_dll_threads) {
-      GC_need_to_lock = TRUE;
+  if (GC_win32_dll_threads) {
+    set_need_to_lock();
         /* Cannot intercept thread creation.  Hence we don't know if    */
         /* other threads exist.  However, client is not allowed to      */
         /* create other threads before collector initialization.        */
         /* Thus it's OK not to lock before this.                        */
-    }
-# endif
+  }
   /* Initialize thread local free lists if used.        */
 # if defined(THREAD_LOCAL_ALLOC)
     LOCK();