]> gitweb.factorcode.org Git - factor.git/blobdiff - vm/os-windows.cpp
Put brackets around ipv6 addresses in `inet6 present`
[factor.git] / vm / os-windows.cpp
index c22f37d8bb0c5796d5f7876b7956cd15241e7248..a5df4dc8647b56b5b2a69b705ec69494897317ce 100644 (file)
@@ -37,15 +37,14 @@ void factor_vm::ffi_dlclose(dll* dll) {
 
 BOOL factor_vm::windows_stat(vm_char* path) {
   BY_HANDLE_FILE_INFORMATION bhfi;
-  HANDLE h = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
+  HANDLE h = CreateFileW(path, FILE_READ_ATTRIBUTES, 0, NULL,
                          OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
 
   if (h == INVALID_HANDLE_VALUE) {
-    /* FindFirstFile is the only call that can stat c:\pagefile.sys */
+    // FindFirstFile is the only call that can stat c:\pagefile.sys
     WIN32_FIND_DATA st;
-    HANDLE h;
-
-    if (INVALID_HANDLE_VALUE == (h = FindFirstFile(path, &st)))
+    h = FindFirstFile(path, &st);
+    if (h == INVALID_HANDLE_VALUE)
       return false;
     FindClose(h);
     return true;
@@ -55,7 +54,7 @@ BOOL factor_vm::windows_stat(vm_char* path) {
   return ret;
 }
 
-/* You must free() this yourself. */
+// You must free() this yourself.
 const vm_char* factor_vm::default_image_path() {
   vm_char full_path[MAX_UNICODE_PATH];
   vm_char* ptr;
@@ -76,7 +75,7 @@ const vm_char* factor_vm::default_image_path() {
   return safe_strdup(temp_path);
 }
 
-/* You must free() this yourself. */
+// You must free() this yourself.
 const vm_char* factor_vm::vm_executable_path() {
   vm_char full_path[MAX_UNICODE_PATH];
   if (!GetModuleFileName(NULL, full_path, MAX_UNICODE_PATH))
@@ -126,13 +125,13 @@ long getpagesize() {
 
 
 bool move_file(const vm_char* path1, const vm_char* path2) {
-  /* MoveFileEx returns FALSE on fail. */
+  // MoveFileEx returns FALSE on fail.
   BOOL val = MoveFileEx((path1), (path2), MOVEFILE_REPLACE_EXISTING);
   if (val == FALSE) {
-    /* MoveFileEx doesn't set errno, which primitive_save_image()
-       reads the error code from. Instead of converting from
-       GetLastError() to errno values, we ust set it to the generic
-       EIO value. */
+    // MoveFileEx doesn't set errno, which primitive_save_image()
+    // reads the error code from. Instead of converting from
+    // GetLastError() to errno values, we ust set it to the generic
+    // EIO value.
     errno = EIO;
   }
   return val == TRUE;
@@ -151,12 +150,12 @@ uint64_t nano_count() {
   static uint32_t hi = 0;
   static uint32_t lo = 0;
 
-  /* Note: on older systems QueryPerformanceCounter may be unreliable
-     until you add /usepmtimer to Boot.ini. I had an issue where two
-     nano_count calls would show a difference of about 1 second,
-     while actually about 80 seconds have passed. The /usepmtimer
-     switch cured the issue on that PC (WinXP Pro SP3 32-bit).
-     See also http://www.virtualdub.org/blog/pivot/entry.php?id=106 */
+  // Note: on older systems QueryPerformanceCounter may be unreliable
+  // until you add /usepmtimer to Boot.ini. I had an issue where two
+  // nano_count calls would show a difference of about 1 second,
+  // while actually about 80 seconds have passed. The /usepmtimer
+  // switch cured the issue on that PC (WinXP Pro SP3 32-bit).
+  // See also http://www.virtualdub.org/blog/pivot/entry.php?id=106
   LARGE_INTEGER count;
   BOOL ret = QueryPerformanceCounter(&count);
   if (ret == 0)
@@ -173,8 +172,8 @@ uint64_t nano_count() {
 #ifdef FACTOR_64
   hi = count.HighPart;
 #else
-  /* On VirtualBox, QueryPerformanceCounter does not increment
-       the high part every time the low part overflows.  Workaround. */
+  // On VirtualBox, QueryPerformanceCounter does not increment
+  // the high part every time the low part overflows.  Workaround.
   if (lo > count.LowPart)
     hi++;
 #endif
@@ -185,20 +184,22 @@ uint64_t nano_count() {
 
 void sleep_nanos(uint64_t nsec) { Sleep((DWORD)(nsec / 1000000)); }
 
+#ifndef EXCEPTION_DISPOSITION
 typedef enum _EXCEPTION_DISPOSITION {
   ExceptionContinueExecution = 0,
   ExceptionContinueSearch = 1,
   ExceptionNestedException = 2,
   ExceptionCollidedUnwind = 3
 } EXCEPTION_DISPOSITION;
+#endif
 
 LONG factor_vm::exception_handler(PEXCEPTION_RECORD e, void* frame, PCONTEXT c,
                                   void* dispatch) {
+  (void)frame;
+  (void)dispatch;
   switch (e->ExceptionCode) {
     case EXCEPTION_ACCESS_VIOLATION:
-      signal_fault_addr = e->ExceptionInformation[1];
-      signal_fault_pc = c->EIP;
-      verify_memory_protection_error(signal_fault_addr);
+      set_memory_protection_error(e->ExceptionInformation[1], c->EIP);
       dispatch_signal_handler((cell*)&c->ESP, (cell*)&c->EIP,
                               (cell)factor::memory_signal_handler_impl);
       break;
@@ -217,7 +218,7 @@ LONG factor_vm::exception_handler(PEXCEPTION_RECORD e, void* frame, PCONTEXT c,
 #else
       signal_fpu_status = fpu_status(X87SW(c) | MXCSR(c));
 
-      /* This seems to have no effect */
+      // This seems to have no effect
       X87SW(c) = 0;
 #endif
       MXCSR(c) &= 0xffffffc0;
@@ -241,18 +242,18 @@ VM_C_API LONG exception_handler(PEXCEPTION_RECORD e, void* frame, PCONTEXT c,
   return vm->exception_handler(e, frame, c, dispatch);
 }
 
-/* On Unix SIGINT (ctrl-c) automatically interrupts blocking io system
-   calls. It doesn't on Windows, so we need to manually send some
-   cancellation requests to unblock the thread. */
-VOID CALLBACK dummy_cb (ULONG_PTR dwParam) { }
+// On Unix SIGINT (ctrl-c) automatically interrupts blocking io system
+// calls. It doesn't on Windows, so we need to manually send some
+// cancellation requests to unblock the thread.
+VOID CALLBACK dummy_cb(ULONG_PTR dwParam) { (void)dwParam; }
 
-/* CancelSynchronousIo is not in Windows XP */
+// CancelSynchronousIo is not in Windows XP
 #if _WIN32_WINNT >= 0x0600
 static void wake_up_thread(HANDLE thread) {
   if (!CancelSynchronousIo(thread)) {
     DWORD err = GetLastError();
-    /* CancelSynchronousIo() didn't find anything to cancel, let's try
-       with QueueUserAPC() instead. */
+    // CancelSynchronousIo() didn't find anything to cancel, let's try
+    // with QueueUserAPC() instead.
     if (err == ERROR_NOT_FOUND) {
       if (!QueueUserAPC(&dummy_cb, thread, NULL)) {
         fatal_error("QueueUserAPC() failed", GetLastError());
@@ -263,22 +264,21 @@ static void wake_up_thread(HANDLE thread) {
   }
 }
 #else
-static void wake_up_thread(HANDLE thread) {}
+static void wake_up_thread(HANDLE thread) { (void)thread; }
 #endif
 
 static BOOL WINAPI ctrl_handler(DWORD dwCtrlType) {
   switch (dwCtrlType) {
     case CTRL_C_EVENT: {
-      /* 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. */
+      // 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.
       FACTOR_ASSERT(thread_vms.size() == 1);
       factor_vm* vm = thread_vms.begin()->second;
       vm->enqueue_fep();
 
-      /* Before leaving the ctrl_handler, try and wake up the main
-         thread. */
+      // Before leaving the ctrl_handler, try and wake up the main thread.
       wake_up_thread(factor::boot_thread);
       return TRUE;
     }
@@ -297,6 +297,50 @@ void handle_ctrl_c() {
   SetConsoleCtrlHandler(factor::ctrl_handler, TRUE);
 }
 
+const int ctrl_break_sleep = 10; /* msec */
+
+static DWORD WINAPI ctrl_break_thread_proc(LPVOID parent_vm) {
+  bool ctrl_break_handled = false;
+  factor_vm* vm = static_cast<factor_vm*>(parent_vm);
+  while (vm->stop_on_ctrl_break) {
+    if (GetAsyncKeyState(VK_CANCEL) >= 0) { /* Ctrl-Break is released. */
+      ctrl_break_handled = false;  /* Wait for the next press. */
+    } else if (!ctrl_break_handled) {
+      /* Check if the VM thread has the same Id as the thread Id of the
+         currently active window. Note that thread Id is not a handle. */
+      DWORD fg_thd_id = GetWindowThreadProcessId(GetForegroundWindow(), NULL);
+      if ((fg_thd_id == vm->thread_id) && !vm->fep_p) {
+        vm->enqueue_fep();
+        ctrl_break_handled = true;
+      }
+    }
+    Sleep(ctrl_break_sleep);
+  }
+  return 0;
+}
+
+void factor_vm::primitive_disable_ctrl_break() {
+  stop_on_ctrl_break = false;
+  if (ctrl_break_thread != NULL) {
+    DWORD wait_result = WaitForSingleObject(ctrl_break_thread,
+                                            2 * ctrl_break_sleep);
+    if (wait_result != WAIT_OBJECT_0)
+      TerminateThread(ctrl_break_thread, 0);
+    CloseHandle(ctrl_break_thread);
+    ctrl_break_thread = NULL;
+  }
+}
+
+void factor_vm::primitive_enable_ctrl_break() {
+  stop_on_ctrl_break = true;
+  if (ctrl_break_thread == NULL) {
+    DisableProcessWindowsGhosting();
+    ctrl_break_thread = CreateThread(NULL, 0, factor::ctrl_break_thread_proc,
+                                     static_cast<LPVOID>(this), 0, NULL);
+    SetThreadPriority(ctrl_break_thread, THREAD_PRIORITY_ABOVE_NORMAL);
+  }
+}
+
 void lock_console() {}
 
 void unlock_console() {}