From ca0dfc5730cf930497be32c65360d596e8d1a545 Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Wed, 26 Oct 2011 20:46:39 -0700 Subject: [PATCH] vm: get CtrlHandler working on windows --- vm/factor.cpp | 2 +- vm/os-windows.cpp | 38 ++++++++++++++++++++++++++------------ vm/vm.hpp | 1 - 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/vm/factor.cpp b/vm/factor.cpp index b2ff245037..c7bb0d2fac 100755 --- a/vm/factor.cpp +++ b/vm/factor.cpp @@ -34,7 +34,7 @@ void factor_vm::default_parameters(vm_parameters *p) p->signals = true; #ifdef WINDOWS - p->console = false; + p->console = GetConsoleWindow() != NULL; #else p->console = true; #endif diff --git a/vm/os-windows.cpp b/vm/os-windows.cpp index 19e5ed0ab8..a93df114b7 100755 --- a/vm/os-windows.cpp +++ b/vm/os-windows.cpp @@ -266,32 +266,46 @@ LONG factor_vm::exception_handler(PEXCEPTION_RECORD e, void *frame, PCONTEXT c, VM_C_API LONG exception_handler(PEXCEPTION_RECORD e, void *frame, PCONTEXT c, void *dispatch) { - return current_vm()->exception_handler(e,frame,c,dispatch); + factor_vm *vm = current_vm_p(); + if (vm) + return vm->exception_handler(e,frame,c,dispatch); + else + { + fatal_error("Foreign thread received exception ", e->ExceptionCode); + return 0; // to placate MSVC + } } -BOOL factor_vm::ctrl_handler(DWORD dwCtrlType) +static BOOL WINAPI ctrl_handler(DWORD dwCtrlType) { switch (dwCtrlType) { case CTRL_C_EVENT: case CTRL_BREAK_EVENT: - enqueue_safepoint_fep(); + { + /* The CtrlHandler runs in its own thread without stopping the main thread. + Since in practice nobody uses the multi-VM stuff yet, we just grab the first + VM we can get. This will not be a good idea when we actually support native + threads. */ + assert(thread_vms.size() == 1); + THREADHANDLE vm_thread = thread_vms.begin()->first; + factor_vm *vm = thread_vms.begin()->second; + + assert(SuspendThread(vm_thread) == 0); + std::cout << "handling ctrl-c" << std::endl; + + vm->enqueue_safepoint_fep(); + MemoryBarrier(); + assert(ResumeThread(vm_thread) == 1); return TRUE; + } default: return FALSE; } } -VM_C_API BOOL ctrl_handler(DWORD dwCtrlType) -{ - factor_vm *vm = current_vm_p(); - if (vm != NULL) - return vm->ctrl_handler(dwCtrlType); - else - return FALSE; -} - void factor_vm::open_console() { + std::cout << "setting ctrl handler\n"; SetConsoleCtrlHandler(factor::ctrl_handler, TRUE); } diff --git a/vm/vm.hpp b/vm/vm.hpp index b366f71ea8..d46a1eab9c 100755 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -720,7 +720,6 @@ struct factor_vm void open_console(); LONG exception_handler(PEXCEPTION_RECORD e, void *frame, PCONTEXT c, void *dispatch); - BOOL ctrl_handler(DWORD dwCtrlType); #else // UNIX void dispatch_signal(void *uap, void (handler)());