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

ruby-changes:65331

From: Nobuyoshi <ko1@a...>
Date: Wed, 24 Feb 2021 04:02:21 +0900 (JST)
Subject: [ruby-changes:65331] a12e950816 (master): Revert "Enclose crtitical sections in `thread_exclusive` block"

https://git.ruby-lang.org/ruby.git/commit/?id=a12e950816

From a12e950816d4db950d975e062f903a5e8c75f1b6 Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Wed, 24 Feb 2021 03:12:19 +0900
Subject: Revert "Enclose crtitical sections in `thread_exclusive` block"

19cc24b34b0490b7c2779eec521fe0089e05f183 and fixups.
---
 debug.c       |   1 -
 win32/win32.c | 147 ++++++++++++++++++++++++++++++----------------------------
 2 files changed, 75 insertions(+), 73 deletions(-)

diff --git a/debug.c b/debug.c
index e0ff948..75d4cc6 100644
--- a/debug.c
+++ b/debug.c
@@ -263,7 +263,6 @@ ruby_set_debug_option(const char *str) https://github.com/ruby/ruby/blob/trunk/debug.c#L263
 }
 
 #if RUBY_DEVEL
-#undef getenv
 
 // RUBY_DEBUG_LOG features
 // See vm_debug.h comments for details.
diff --git a/win32/win32.c b/win32/win32.c
index 9303a17..c97d935 100644
--- a/win32/win32.c
+++ b/win32/win32.c
@@ -704,11 +704,6 @@ static CRITICAL_SECTION conlist_mutex; https://github.com/ruby/ruby/blob/trunk/win32/win32.c#L704
 static st_table *conlist = NULL;
 #define conlist_disabled ((st_table *)-1)
 
-#define thread_exclusive(obj) \
-    for (bool first = (EnterCriticalSection(&obj##_mutex), true); \
-	 first; first = (LeaveCriticalSection(&obj##_mutex), false))
-
-static CRITICAL_SECTION uenvarea_mutex;
 static char *uenvarea;
 
 /* License: Ruby's */
@@ -733,13 +728,13 @@ free_conlist(st_data_t key, st_data_t val, st_data_t arg) https://github.com/ruby/ruby/blob/trunk/win32/win32.c#L728
 static void
 constat_delete(HANDLE h)
 {
-    thread_exclusive(conlist) {
-	if (conlist && conlist != conlist_disabled) {
-	    st_data_t key = (st_data_t)h, val;
-	    st_delete(conlist, &key, &val);
-	    xfree((struct constat *)val);
-	}
+    EnterCriticalSection(&conlist_mutex);
+    if (conlist && conlist != conlist_disabled) {
+	st_data_t key = (st_data_t)h, val;
+	st_delete(conlist, &key, &val);
+	xfree((struct constat *)val);
     }
+    LeaveCriticalSection(&conlist_mutex);
 }
 
 /* License: Ruby's */
@@ -750,13 +745,10 @@ exit_handler(void) https://github.com/ruby/ruby/blob/trunk/win32/win32.c#L745
     DeleteCriticalSection(&select_mutex);
     DeleteCriticalSection(&socklist_mutex);
     DeleteCriticalSection(&conlist_mutex);
-    thread_exclusive(uenvarea) {
-	if (uenvarea) {
-	    free(uenvarea);
-	    uenvarea = NULL;
-	}
+    if (uenvarea) {
+	free(uenvarea);
+	uenvarea = NULL;
     }
-    DeleteCriticalSection(&uenvarea_mutex);
 }
 
 /* License: Ruby's */
@@ -825,13 +817,13 @@ socklist_insert(SOCKET sock, int flag) https://github.com/ruby/ruby/blob/trunk/win32/win32.c#L817
 {
     int ret;
 
-    thread_exclusive(socklist) {
-	if (!socklist) {
-	    socklist = st_init_numtable();
-	    install_vm_exit_handler();
-	}
-	ret = st_insert(socklist, (st_data_t)sock, (st_data_t)flag);
+    EnterCriticalSection(&socklist_mutex);
+    if (!socklist) {
+	socklist = st_init_numtable();
+	install_vm_exit_handler();
     }
+    ret = st_insert(socklist, (st_data_t)sock, (st_data_t)flag);
+    LeaveCriticalSection(&socklist_mutex);
 
     return ret;
 }
@@ -841,14 +833,18 @@ static inline int https://github.com/ruby/ruby/blob/trunk/win32/win32.c#L833
 socklist_lookup(SOCKET sock, int *flagp)
 {
     st_data_t data;
-    int ret = 0;
+    int ret;
 
-    thread_exclusive(socklist) {
-	if (!socklist) continue;
+    EnterCriticalSection(&socklist_mutex);
+    if (socklist) {
 	ret = st_lookup(socklist, (st_data_t)sock, (st_data_t *)&data);
 	if (ret && flagp)
 	    *flagp = (int)data;
     }
+    else {
+	ret = 0;
+    }
+    LeaveCriticalSection(&socklist_mutex);
 
     return ret;
 }
@@ -859,10 +855,10 @@ socklist_delete(SOCKET *sockp, int *flagp) https://github.com/ruby/ruby/blob/trunk/win32/win32.c#L855
 {
     st_data_t key;
     st_data_t data;
-    int ret = 0;
+    int ret;
 
-    thread_exclusive(socklist) {
-	if (!socklist) continue;
+    EnterCriticalSection(&socklist_mutex);
+    if (socklist) {
 	key = (st_data_t)*sockp;
 	if (flagp)
 	    data = (st_data_t)*flagp;
@@ -873,6 +869,10 @@ socklist_delete(SOCKET *sockp, int *flagp) https://github.com/ruby/ruby/blob/trunk/win32/win32.c#L869
 		*flagp = (int)data;
 	}
     }
+    else {
+	ret = 0;
+    }
+    LeaveCriticalSection(&socklist_mutex);
 
     return ret;
 }
@@ -895,7 +895,6 @@ rb_w32_sysinit(int *argc, char ***argv) https://github.com/ruby/ruby/blob/trunk/win32/win32.c#L895
 #endif
     SetErrorMode(SEM_FAILCRITICALERRORS|SEM_NOGPFAULTERRORBOX);
 
-    InitializeCriticalSection(&uenvarea_mutex);
     get_version();
 
     //
@@ -2915,7 +2914,7 @@ rb_w32_fdisset(int fd, fd_set *set) https://github.com/ruby/ruby/blob/trunk/win32/win32.c#L2914
     SOCKET s = TO_SOCKET(fd);
     if (s == (SOCKET)INVALID_HANDLE_VALUE)
         return 0;
-    RUBY_CRITICAL {ret = __WSAFDIsSet(s, set);}
+    RUBY_CRITICAL(ret = __WSAFDIsSet(s, set));
     return ret;
 }
 
@@ -3119,9 +3118,9 @@ do_select(int nfds, fd_set *rd, fd_set *wr, fd_set *ex, https://github.com/ruby/ruby/blob/trunk/win32/win32.c#L3118
     }
     else {
 	RUBY_CRITICAL {
-	    thread_exclusive(select) {
-		r = select(nfds, rd, wr, ex, timeout);
-	    }
+	    EnterCriticalSection(&select_mutex);
+	    r = select(nfds, rd, wr, ex, timeout);
+	    LeaveCriticalSection(&select_mutex);
 	    if (r == SOCKET_ERROR) {
 		errno = map_errno(WSAGetLastError());
 		r = -1;
@@ -5265,37 +5264,32 @@ w32_getenv(const char *name, UINT cp) https://github.com/ruby/ruby/blob/trunk/win32/win32.c#L5264
 {
     WCHAR *wenvarea, *wenv;
     int len = strlen(name);
-    char *env = NULL;
+    char *env;
     int wlen;
 
     if (len == 0) return NULL;
 
-    thread_exclusive(uenvarea) {
-	if (uenvarea) {
-	    free(uenvarea);
-	    uenvarea = NULL;
-	}
-	wenvarea = GetEnvironmentStringsW();
-	if (!wenvarea) {
-	    map_errno(GetLastError());
-	    continue;
-	}
-	for (wenv = wenvarea, wlen = 1; *wenv; wenv += lstrlenW(wenv) + 1)
-	    wlen += lstrlenW(wenv) + 1;
-	uenvarea = wstr_to_mbstr(cp, wenvarea, wlen, NULL);
-	FreeEnvironmentStringsW(wenvarea);
-	if (!uenvarea)
-	    continue;
-
-	for (env = uenvarea; *env; env += strlen(env) + 1) {
-	    if (strncasecmp(env, name, len) == 0 && *(env + len) == '=') {
-		env += len + 1;
-		break;
-	    }
-	}
+    if (uenvarea) {
+	free(uenvarea);
+	uenvarea = NULL;
+    }
+    wenvarea = GetEnvironmentStringsW();
+    if (!wenvarea) {
+	map_errno(GetLastError());
+	return NULL;
     }
+    for (wenv = wenvarea, wlen = 1; *wenv; wenv += lstrlenW(wenv) + 1)
+	wlen += lstrlenW(wenv) + 1;
+    uenvarea = wstr_to_mbstr(cp, wenvarea, wlen, NULL);
+    FreeEnvironmentStringsW(wenvarea);
+    if (!uenvarea)
+	return NULL;
 
-    return env;
+    for (env = uenvarea; *env; env += strlen(env) + 1)
+	if (strncasecmp(env, name, len) == 0 && *(env + len) == '=')
+	    return env + len + 1;
+
+    return NULL;
 }
 
 /* License: Ruby's */
@@ -6599,19 +6593,19 @@ static struct constat * https://github.com/ruby/ruby/blob/trunk/win32/win32.c#L6593
 constat_handle(HANDLE h)
 {
     st_data_t data;
-    struct constat *p = NULL;
-    thread_exclusive(conlist) {
-	if (!conlist) {
-	    if (console_emulator_p()) {
-		conlist = conlist_disabled;
-		continue;
-	    }
+    struct constat *p;
+
+    EnterCriticalSection(&conlist_mutex);
+    if (!conlist) {
+	if (console_emulator_p()) {
+	    conlist = conlist_disabled;
+	}
+	else {
 	    conlist = st_init_numtable();
 	    install_vm_exit_handler();
 	}
-	else if (conlist == conlist_disabled) {
-	    continue;
-	}
+    }
+    if (conlist != conlist_disabled) {
 	if (st_lookup(conlist, (st_data_t)h, &data)) {
 	    p = (struct constat *)data;
 	}
@@ -6628,6 +6622,11 @@ constat_handle(HANDLE h) https://github.com/ruby/ruby/blob/trunk/win32/win32.c#L6622
 	    st_insert(conlist, (st_data_t)h, (st_data_t)p);
 	}
     }
+    else {
+	p = NULL;
+    }
+    LeaveCriticalSection(&conlist_mutex);
+
     return p;
 }
 
@@ -6637,12 +6636,16 @@ constat_reset(HANDLE h) https://github.com/ruby/ruby/blob/trunk/win32/win32.c#L6636
 {
     st_data_t data;
     struct constat *p;
-    thread_exclusive(conlist) {
-	if (!conlist || conlist == conlist_disabled) continue;
-	if (!st_lookup(conlist, (st_data_t)h, &data)) continue;
+
+    EnterCriticalSection(&conlist_mutex);
+    if (
+	conlist && conlist != conlist_disabled &&
+	st_lookup(conlist, (st_data_t)h, &data)
+    ) {
 	p = (struct constat *)data;
 	p->vt100.state = constat_init;
     }
+    LeaveCriticalSection(&conlist_mutex);
 }
 
 #define FOREGROUND_MASK (FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY)
-- 
cgit v1.1


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

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