summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2021-02-23 21:30:54 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2021-03-08 17:46:07 +0900
commitb7d4dcf3a6a741ba85119b45a786a55e70bb910e (patch)
tree811b3cde80205534e940eaacab92cbda0e31aaf0
parent9299703b390a30246819a60516bdd14e37c597fe (diff)
Make vm_exit_handler installation MT-safe
-rw-r--r--win32/win32.c14
1 files changed, 11 insertions, 3 deletions
diff --git a/win32/win32.c b/win32/win32.c
index 90e109d5a0..e2d4cceeb3 100644
--- a/win32/win32.c
+++ b/win32/win32.c
@@ -781,15 +781,23 @@ vm_exit_handler(ruby_vm_t *vm)
LeaveCriticalSection(&conlist_mutex);
}
+#define ATOMIC_LONG_CAS(var, oldval, newval) InterlockedCompareExchange(&(var), (newval), (oldval))
+
/* License: Ruby's */
static void
install_vm_exit_handler(void)
{
- static bool installed = 0;
+ static LONG installed = 0;
+ LONG i;
- if (!installed) {
+ while ((i = ATOMIC_LONG_CAS(installed, 0, -1)) != 1) {
+ if (i != 0) {
+ Sleep(1);
+ continue;
+ }
ruby_vm_at_exit(vm_exit_handler);
- installed = 1;
+ ATOMIC_LONG_CAS(installed, -1, 1);
+ break;
}
}