From e33b1690d06f867e45750bd8e3e8b06d78b5bc26 Mon Sep 17 00:00:00 2001 From: nobu Date: Mon, 20 Nov 2017 01:17:43 +0000 Subject: 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. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60856 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- win32/win32.c | 45 ++++++++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 13 deletions(-) (limited to 'win32/win32.c') diff --git a/win32/win32.c b/win32/win32.c index 3887193116..c6183091fb 100644 --- a/win32/win32.c +++ b/win32/win32.c @@ -49,6 +49,7 @@ #include #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, int line, const char *exe, const char #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) { 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) 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) 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) init_stdhandle(); - atexit(exit_handler); - // Initialize Winsock StartSockets(); } @@ -6560,6 +6578,7 @@ constat_handle(HANDLE h) return NULL; } conlist = st_init_numtable(); + install_vm_exit_handler(); } else if (conlist == conlist_disabled) { return NULL; -- cgit v1.2.3