]> granicus.if.org Git - gc/commitdiff
Public API (GC_deinit) to allow Win32 critical sections deletion
authorTautvydas Zilys <tautvydas.zilys@gmail.com>
Wed, 19 Jul 2017 21:53:43 +0000 (00:53 +0300)
committerIvan Maidanski <ivmai@mail.ru>
Wed, 19 Jul 2017 22:05:50 +0000 (01:05 +0300)
Public GC_deinit allows the clients to avoid a leak of two critical
sections on GC shutdown.

* include/gc.h (GC_deinit): New public function declaration.
* include/private/gc_priv.h [MSWIN32 || MSWINCE] (GC_deinit): Remove.
* misc.c (GC_deinit): Make it public; define it unconditionally; call
DeleteCriticalSection(GC_allocate_ml) if GC_is_initialized; set
GC_is_initialized to false.
DeleteCriticalSection(GC_allocate_ml)
* tests/test.c [!GC_WIN32_THREADS && !GC_PTHREADS && CPPCHECK] (main):
Add UNTESTED(GC_deinit).
* win32_threads.c [GC_WINMAIN_REDIRECT && MSWINCE] (WinMain): Do not
call DeleteCriticalSection(GC_allocate_ml).
* win32_threads.c [!GC_NO_THREADS_DISCOVERY && !GC_PTHREADS]
(GC_DllMain): Likewise.

include/gc.h
include/private/gc_priv.h
misc.c
tests/test.c
win32_threads.c

index ec0e86fae966c55a8983ff86fb5d2af29dfd1a4b..ebabfb08b5674498d20af07a38d9fed610269bc6 100644 (file)
@@ -440,6 +440,11 @@ GC_API void GC_CALL GC_atfork_child(void);
 /* from the main program instead.                                       */
 GC_API void GC_CALL GC_init(void);
 
+/* Perform the collector shutdown.  (E.g. dispose critical sections on  */
+/* Win32 target.)  A duplicate invocation is a no-op.  GC_INIT should   */
+/* not be called after the shutdown.  See also GC_win32_free_heap().    */
+GC_API void GC_CALL GC_deinit(void);
+
 /* General purpose allocation routines, with roughly malloc calling     */
 /* conv.  The atomic versions promise that no relevant pointers are     */
 /* contained in the object.  The non-atomic versions guarantee that the */
index 7d8e5c169b2f46e18c370cd758a95eb55bb5275e..804d094857de5fac231b03774e64518ba6ce3694 100644 (file)
@@ -1905,12 +1905,6 @@ GC_INNER GC_bool GC_try_to_collect_inner(GC_stop_func f);
 
 GC_EXTERN GC_bool GC_is_initialized; /* GC_init() has been run. */
 
-#if defined(MSWIN32) || defined(MSWINCE)
-  void GC_deinit(void);
-                                /* Free any resources allocated by      */
-                                /* GC_init                              */
-#endif
-
 GC_INNER void GC_collect_a_little_inner(int n);
                                 /* Do n units worth of garbage          */
                                 /* collection work, if appropriate.     */
diff --git a/misc.c b/misc.c
index 7bf4ba73513632c95c8aa037c1f4834c77d8c82d..e833f37eee2ca4045484a0ec160c3f933ef695f2 100644 (file)
--- a/misc.c
+++ b/misc.c
@@ -1373,6 +1373,18 @@ GC_API void GC_CALL GC_enable_incremental(void)
   }
 #endif
 
+  GC_API void GC_CALL GC_deinit(void)
+  {
+    if (GC_is_initialized) {
+      /* Prevent duplicate resource close.  */
+      GC_is_initialized = FALSE;
+#     if defined(THREADS) && (defined(MSWIN32) || defined(MSWINCE))
+        DeleteCriticalSection(&GC_write_cs);
+        DeleteCriticalSection(&GC_allocate_ml);
+#     endif
+    }
+  }
+
 #if defined(MSWIN32) || defined(MSWINCE)
 
 # if defined(_MSC_VER) && defined(_DEBUG) && !defined(MSWINCE)
@@ -1381,15 +1393,6 @@ GC_API void GC_CALL GC_enable_incremental(void)
 
   STATIC HANDLE GC_log = 0;
 
-  void GC_deinit(void)
-  {
-#   ifdef THREADS
-      if (GC_is_initialized) {
-        DeleteCriticalSection(&GC_write_cs);
-      }
-#   endif
-  }
-
 # ifdef THREADS
 #   if defined(PARALLEL_MARK) && !defined(GC_ALWAYS_MULTITHREADED)
 #     define IF_NEED_TO_LOCK(x) if (GC_parallel || GC_need_to_lock) x
index ef7e8095d135ab83cd885a5022a953d57884ae46..ddbdd328a6e27ae74fe262c883b3fbd7bcb47a82 100644 (file)
@@ -1868,6 +1868,7 @@ void GC_CALLBACK warn_proc(char *msg, GC_word p)
        UNTESTED(GC_malloc_explicitly_typed_ignore_off_page);
        UNTESTED(GC_debug_change_stubborn);
        UNTESTED(GC_debug_strndup);
+       UNTESTED(GC_deinit);
        UNTESTED(GC_strndup);
        UNTESTED(GC_posix_memalign);
        UNTESTED(GC_new_free_list);
index 692b75b6240aa82fa8d70b49e0a8d69047837cc7..54fa5d2b3bffa8e31ce567e4573dd4e71b1d720d 100644 (file)
@@ -2377,7 +2377,6 @@ GC_INNER void GC_get_next_stack(char *start, char *limit,
 
 #   ifdef MSWINCE
       GC_deinit();
-      DeleteCriticalSection(&GC_allocate_ml);
 #   endif
     return (int) exit_code;
   }
@@ -2786,7 +2785,6 @@ GC_INNER void GC_thr_init(void)
              GC_delete_gc_thread_no_free(&dll_thread_table[i]);
           }
           GC_deinit();
-          DeleteCriticalSection(&GC_allocate_ml);
         }
         break;
       }