diff --git a/win32/win32.c b/win32/win32.c index cc822fef9b..050582211c 100644 --- a/win32/win32.c +++ b/win32/win32.c @@ -704,6 +704,11 @@ static CRITICAL_SECTION conlist_mutex; 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) 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) { 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 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) { 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) 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) 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, } 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;