Enclose crtitical sections in `thread_exclusive` block

This commit is contained in:
Nobuyoshi Nakada 2021-02-23 16:22:43 +09:00
Родитель b956efdc1e
Коммит 19cc24b34b
1 изменённых файлов: 27 добавлений и 28 удалений

Просмотреть файл

@ -704,6 +704,11 @@ static CRITICAL_SECTION conlist_mutex;
static st_table *conlist = NULL; static st_table *conlist = NULL;
#define conlist_disabled ((st_table *)-1) #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; static char *uenvarea;
/* License: Ruby's */ /* License: Ruby's */
@ -728,13 +733,13 @@ free_conlist(st_data_t key, st_data_t val, st_data_t arg)
static void static void
constat_delete(HANDLE h) constat_delete(HANDLE h)
{ {
EnterCriticalSection(&conlist_mutex); thread_exclusive(conlist) {
if (conlist && conlist != conlist_disabled) { if (conlist && conlist != conlist_disabled) {
st_data_t key = (st_data_t)h, val; st_data_t key = (st_data_t)h, val;
st_delete(conlist, &key, &val); st_delete(conlist, &key, &val);
xfree((struct constat *)val); xfree((struct constat *)val);
} }
LeaveCriticalSection(&conlist_mutex); }
} }
/* License: Ruby's */ /* License: Ruby's */
@ -817,13 +822,13 @@ socklist_insert(SOCKET sock, int flag)
{ {
int ret; int ret;
EnterCriticalSection(&socklist_mutex); thread_exclusive(socklist) {
if (!socklist) { if (!socklist) {
socklist = st_init_numtable(); socklist = st_init_numtable();
install_vm_exit_handler(); 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; return ret;
} }
@ -833,17 +838,14 @@ static inline int
socklist_lookup(SOCKET sock, int *flagp) socklist_lookup(SOCKET sock, int *flagp)
{ {
st_data_t data; st_data_t data;
int ret; int ret = 0;
EnterCriticalSection(&socklist_mutex); thread_exclusive(socklist) {
if (socklist) { if (!socklist) continue;
ret = st_lookup(socklist, (st_data_t)sock, (st_data_t *)&data); ret = st_lookup(socklist, (st_data_t)sock, (st_data_t *)&data);
if (ret && flagp) if (ret && flagp)
*flagp = (int)data; *flagp = (int)data;
} else {
ret = 0;
} }
LeaveCriticalSection(&socklist_mutex);
return ret; return ret;
} }
@ -854,10 +856,10 @@ socklist_delete(SOCKET *sockp, int *flagp)
{ {
st_data_t key; st_data_t key;
st_data_t data; st_data_t data;
int ret; int ret = 0;
EnterCriticalSection(&socklist_mutex); thread_exclusive(socklist) {
if (socklist) { if (!socklist) continue;
key = (st_data_t)*sockp; key = (st_data_t)*sockp;
if (flagp) if (flagp)
data = (st_data_t)*flagp; data = (st_data_t)*flagp;
@ -867,10 +869,7 @@ socklist_delete(SOCKET *sockp, int *flagp)
if (flagp) if (flagp)
*flagp = (int)data; *flagp = (int)data;
} }
} else {
ret = 0;
} }
LeaveCriticalSection(&socklist_mutex);
return ret; return ret;
} }
@ -2912,7 +2911,7 @@ rb_w32_fdisset(int fd, fd_set *set)
SOCKET s = TO_SOCKET(fd); SOCKET s = TO_SOCKET(fd);
if (s == (SOCKET)INVALID_HANDLE_VALUE) if (s == (SOCKET)INVALID_HANDLE_VALUE)
return 0; return 0;
RUBY_CRITICAL(ret = __WSAFDIsSet(s, set)); RUBY_CRITICAL {ret = __WSAFDIsSet(s, set);}
return ret; return ret;
} }
@ -3116,9 +3115,9 @@ do_select(int nfds, fd_set *rd, fd_set *wr, fd_set *ex,
} }
else { else {
RUBY_CRITICAL { RUBY_CRITICAL {
EnterCriticalSection(&select_mutex); thread_exclusive(select) {
r = select(nfds, rd, wr, ex, timeout); r = select(nfds, rd, wr, ex, timeout);
LeaveCriticalSection(&select_mutex); }
if (r == SOCKET_ERROR) { if (r == SOCKET_ERROR) {
errno = map_errno(WSAGetLastError()); errno = map_errno(WSAGetLastError());
r = -1; r = -1;