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

ruby-changes:65325

From: Nobuyoshi <ko1@a...>
Date: Tue, 23 Feb 2021 19:04:29 +0900 (JST)
Subject: [ruby-changes:65325] 19cc24b34b (master): Enclose crtitical sections in `thread_exclusive` block

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

From 19cc24b34b0490b7c2779eec521fe0089e05f183 Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Tue, 23 Feb 2021 16:22:43 +0900
Subject: Enclose crtitical sections in `thread_exclusive` block

---
 win32/win32.c | 55 +++++++++++++++++++++++++++----------------------------
 1 file changed, 27 insertions(+), 28 deletions(-)

diff --git a/win32/win32.c b/win32/win32.c
index cc822fe..0505822 100644
--- a/win32/win32.c
+++ b/win32/win32.c
@@ -704,6 +704,11 @@ 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 */
@@ -728,13 +733,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#L733
 static void
 constat_delete(HANDLE h)
 {
-    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);
+    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);
+	}
     }
-    LeaveCriticalSection(&conlist_mutex);
 }
 
 /* License: Ruby's */
@@ -817,13 +822,13 @@ socklist_insert(SOCKET sock, int flag) https://github.com/ruby/ruby/blob/trunk/win32/win32.c#L822
 {
     int ret;
 
-    EnterCriticalSection(&socklist_mutex);
-    if (!socklist) {
-	socklist = st_init_numtable();
-	install_vm_exit_handler();
+    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);
     }
-    ret = st_insert(socklist, (st_data_t)sock, (st_data_t)flag);
-    LeaveCriticalSection(&socklist_mutex);
 
     return ret;
 }
@@ -833,17 +838,14 @@ static inline int https://github.com/ruby/ruby/blob/trunk/win32/win32.c#L838
 socklist_lookup(SOCKET sock, int *flagp)
 {
     st_data_t data;
-    int ret;
+    int ret = 0;
 
-    EnterCriticalSection(&socklist_mutex);
-    if (socklist) {
+    thread_exclusive(socklist) {
+	if (!socklist) continue;
 	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;
 }
@@ -854,10 +856,10 @@ socklist_delete(SOCKET *sockp, int *flagp) https://github.com/ruby/ruby/blob/trunk/win32/win32.c#L856
 {
     st_data_t key;
     st_data_t data;
-    int ret;
+    int ret = 0;
 
-    EnterCriticalSection(&socklist_mutex);
-    if (socklist) {
+    thread_exclusive(socklist) {
+	if (!socklist) continue;
 	key = (st_data_t)*sockp;
 	if (flagp)
 	    data = (st_data_t)*flagp;
@@ -867,10 +869,7 @@ socklist_delete(SOCKET *sockp, int *flagp) https://github.com/ruby/ruby/blob/trunk/win32/win32.c#L869
 	    if (flagp)
 		*flagp = (int)data;
 	}
-    } else {
-	ret = 0;
     }
-    LeaveCriticalSection(&socklist_mutex);
 
     return ret;
 }
@@ -2912,7 +2911,7 @@ rb_w32_fdisset(int fd, fd_set *set) https://github.com/ruby/ruby/blob/trunk/win32/win32.c#L2911
     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;
 }
 
@@ -3116,9 +3115,9 @@ do_select(int nfds, fd_set *rd, fd_set *wr, fd_set *ex, https://github.com/ruby/ruby/blob/trunk/win32/win32.c#L3115
     }
     else {
 	RUBY_CRITICAL {
-	    EnterCriticalSection(&select_mutex);
-	    r = select(nfds, rd, wr, ex, timeout);
-	    LeaveCriticalSection(&select_mutex);
+	    thread_exclusive(select) {
+		r = select(nfds, rd, wr, ex, timeout);
+	    }
 	    if (r == SOCKET_ERROR) {
 		errno = map_errno(WSAGetLastError());
 		r = -1;
-- 
cgit v1.1


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

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