]> gitweb.factorcode.org Git - factor.git/commitdiff
vm: get CtrlHandler working on windows
authorJoe Groff <arcata@gmail.com>
Thu, 27 Oct 2011 03:46:39 +0000 (20:46 -0700)
committerJoe Groff <arcata@gmail.com>
Fri, 28 Oct 2011 04:18:21 +0000 (21:18 -0700)
vm/factor.cpp
vm/os-windows.cpp
vm/vm.hpp

index b2ff2450378ad97356a100d9e5a72b9aa41b7959..c7bb0d2fac686539e29e12ad3f32c6a8d5deb38e 100755 (executable)
@@ -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
index 19e5ed0ab888f937603521bf50af4716372aef41..a93df114b7411276d018601126666d5acd5fbc94 100755 (executable)
@@ -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);
 }
 
index b366f71ea824460bdd214c39bfc61183ce504563..d46a1eab9c9aae65dfbfd0e6569506e64eff28a5 100755 (executable)
--- 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)());