[前][次][番号順一覧][スレッド一覧]

ruby-changes:48740

From: nobu <ko1@a...>
Date: Mon, 20 Nov 2017 10:17:56 +0900 (JST)
Subject: [ruby-changes:48740] nobu:r60856 (trunk): win32.c: vm_exit_handler

nobu	2017-11-20 10:17:43 +0900 (Mon, 20 Nov 2017)

  New Revision: 60856

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=60856

  Log:
    win32.c: vm_exit_handler
    
    * win32/win32.c (vm_exit_handler): separate exit handler for
      resources which must be released at exit of Ruby VM.
    
    * win32/win32.c (socklist_insert, constat_handle): install the VM
      exit handler.
    
    * gc.c (ENABLE_VM_OBJSPACE): no longer needs process global object
      space on Windows too.

  Modified files:
    trunk/common.mk
    trunk/gc.c
    trunk/win32/win32.c
Index: gc.c
===================================================================
--- gc.c	(revision 60855)
+++ gc.c	(revision 60856)
@@ -719,16 +719,7 @@ struct heap_page { https://github.com/ruby/ruby/blob/trunk/gc.c#L719
 #endif
 
 #ifndef ENABLE_VM_OBJSPACE
-# ifdef _WIN32
-/*
- * TODO: object space independent st_table.
- * socklist and conlist will be freed exit_handler(), after object
- * space destruction.
- */
-#   define ENABLE_VM_OBJSPACE 0
-# else
-#   define ENABLE_VM_OBJSPACE 1
-# endif
+# define ENABLE_VM_OBJSPACE 1
 #endif
 
 /* Aliases */
Index: win32/win32.c
===================================================================
--- win32/win32.c	(revision 60855)
+++ win32/win32.c	(revision 60856)
@@ -49,6 +49,7 @@ https://github.com/ruby/ruby/blob/trunk/win32/win32.c#L49
 #include <mswsock.h>
 #endif
 #include "ruby/win32.h"
+#include "ruby/vm.h"
 #include "win32/dir.h"
 #include "win32/file.h"
 #include "internal.h"
@@ -689,7 +690,7 @@ rtc_error_handler(int e, const char *src https://github.com/ruby/ruby/blob/trunk/win32/win32.c#L690
 #endif
 
 static CRITICAL_SECTION select_mutex;
-static int NtSocketsInitialized = 0;
+#define NtSocketsInitialized 1
 static st_table *socklist = NULL;
 static st_table *conlist = NULL;
 #define conlist_disabled ((st_table *)-1)
@@ -730,21 +731,38 @@ exit_handler(void) https://github.com/ruby/ruby/blob/trunk/win32/win32.c#L731
 {
     if (NtSocketsInitialized) {
 	WSACleanup();
-	if (socklist) {
-	    st_free_table(socklist);
-	    socklist = NULL;
-	}
 	DeleteCriticalSection(&select_mutex);
-	NtSocketsInitialized = 0;
+    }
+    if (uenvarea) {
+	free(uenvarea);
+	uenvarea = NULL;
+    }
+}
+
+/* License: Ruby's */
+static void
+vm_exit_handler(ruby_vm_t *vm)
+{
+    if (socklist) {
+	st_free_table(socklist);
+	socklist = NULL;
     }
     if (conlist && conlist != conlist_disabled) {
 	st_foreach(conlist, free_conlist, 0);
 	st_free_table(conlist);
 	conlist = NULL;
     }
-    if (uenvarea) {
-	free(uenvarea);
-	uenvarea = NULL;
+}
+
+/* License: Ruby's */
+static void
+install_vm_exit_handler(void)
+{
+    static bool installed = 0;
+
+    if (!installed) {
+	ruby_vm_at_exit(vm_exit_handler);
+	installed = 1;
     }
 }
 
@@ -767,7 +785,7 @@ StartSockets(void) https://github.com/ruby/ruby/blob/trunk/win32/win32.c#L785
 
     InitializeCriticalSection(&select_mutex);
 
-    NtSocketsInitialized = 1;
+    atexit(exit_handler);
 }
 
 #define MAKE_SOCKDATA(af, fl)	((int)((((int)af)<<4)|((fl)&0xFFFF)))
@@ -778,8 +796,10 @@ StartSockets(void) https://github.com/ruby/ruby/blob/trunk/win32/win32.c#L796
 static inline int
 socklist_insert(SOCKET sock, int flag)
 {
-    if (!socklist)
+    if (!socklist) {
 	socklist = st_init_numtable();
+	install_vm_exit_handler();
+    }
     return st_insert(socklist, (st_data_t)sock, (st_data_t)flag);
 }
 
@@ -857,8 +877,6 @@ rb_w32_sysinit(int *argc, char ***argv) https://github.com/ruby/ruby/blob/trunk/win32/win32.c#L877
 
     init_stdhandle();
 
-    atexit(exit_handler);
-
     // Initialize Winsock
     StartSockets();
 }
@@ -6560,6 +6578,7 @@ constat_handle(HANDLE h) https://github.com/ruby/ruby/blob/trunk/win32/win32.c#L6578
 	    return NULL;
 	}
 	conlist = st_init_numtable();
+	install_vm_exit_handler();
     }
     else if (conlist == conlist_disabled) {
 	return NULL;
Index: common.mk
===================================================================
--- common.mk	(revision 60855)
+++ common.mk	(revision 60856)
@@ -844,7 +844,8 @@ compile.$(OBJEXT): {$(VPATH)}opt_sc.inc https://github.com/ruby/ruby/blob/trunk/common.mk#L844
 
 win32/win32.$(OBJEXT): {$(VPATH)}win32/win32.c {$(VPATH)}win32/file.h \
   {$(VPATH)}dln.h {$(VPATH)}dln_find.c {$(VPATH)}encindex.h \
-  {$(VPATH)}internal.h {$(VPATH)}util.h $(RUBY_H_INCLUDES) $(PLATFORM_D)
+  {$(VPATH)}internal.h {$(VPATH)}util.h $(RUBY_H_INCLUDES) \
+  {$(VPATH)}vm.h $(PLATFORM_D)
 win32/file.$(OBJEXT): {$(VPATH)}win32/file.c {$(VPATH)}win32/file.h \
   $(RUBY_H_INCLUDES) $(PLATFORM_D)
 

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]