From e48eb1ea16c01f4cc72e7a365b987d84a4bb55a7 Mon Sep 17 00:00:00 2001
From: Recolic Keghart <root@recolic.net>
Date: Fri, 3 Jan 2020 15:12:04 +0800
Subject: [PATCH] add option to disable async render

---
 nemu/src/device/device.cc | 20 ++++++++++++--------
 nemu/src/device/vga.cc    |  7 ++++++-
 2 files changed, 18 insertions(+), 9 deletions(-)

diff --git a/nemu/src/device/device.cc b/nemu/src/device/device.cc
index 667f0c9..f9a5304 100644
--- a/nemu/src/device/device.cc
+++ b/nemu/src/device/device.cc
@@ -41,11 +41,6 @@ static void timer_sig_handler(int signum) {
 }
 
 void device_update_impl() {
-    // async
-    if(device_update_flag)
-        device_update_flag = false;
-    else
-        return;
   if (update_screen_flag) {
     update_screen();
     update_screen_flag = false;
@@ -75,9 +70,16 @@ void device_update_impl() {
   }
 }
 
-void device_update() {device_update_impl();}
+void device_update() {
+#ifdef ENABLE_ASYNC_RENDER
+#else
+  if(device_update_flag.exchange(false)) {
+    device_update_impl();
+  }
+#endif
+}
 
-static void device_update_thread_daemon() {
+[[maybe_unused]] static void device_update_thread_daemon() {
   while(true) {
     if(device_update_flag.exchange(false)) {
       device_update_impl();
@@ -109,7 +111,9 @@ void init_device() {
   ret = setitimer(ITIMER_VIRTUAL, &it, NULL);
   Assert(ret == 0, "Can not set timer");
 
-  // std::thread(device_update_thread_daemon).detach();
+#ifdef ENABLE_ASYNC_RENDER
+  std::thread(device_update_thread_daemon).detach();
+#endif
 }
 #else
 
diff --git a/nemu/src/device/vga.cc b/nemu/src/device/vga.cc
index 4529e8e..8db9ea5 100644
--- a/nemu/src/device/vga.cc
+++ b/nemu/src/device/vga.cc
@@ -40,7 +40,9 @@ static void init_vga_impl() {
 
 void update_screen() {
 #ifndef DISABLE_MMIO
-  // if(window == nullptr) init_vga_impl();
+#ifdef ENABLE_ASYNC_RENDER
+  if(window == nullptr) init_vga_impl();
+#endif
   SDL_ErrorCheck(SDL_UpdateTexture(texture, NULL, vmem, SCREEN_W * sizeof(vmem[0][0])));
   SDL_ErrorCheck(SDL_RenderClear(renderer));
   SDL_ErrorCheck(SDL_RenderCopy(renderer, texture, NULL, NULL));
@@ -49,9 +51,12 @@ void update_screen() {
 }
 
 void init_vga() {
+#ifdef ENABLE_ASYNC_RENDER
   // Because of fucking SDL design, vga_init should be done in updating thread.
   // Do nothing in main thread.
+#else
   init_vga_impl();
+#endif
 }
 
 #endif	/* HAS_IOE */
-- 
GitLab