core, fix race condition when waiting for vsync
authorkub <derkub@gmail.com>
Mon, 28 Jun 2021 19:58:01 +0000 (21:58 +0200)
committerkub <derkub@gmail.com>
Mon, 28 Jun 2021 19:58:01 +0000 (21:58 +0200)
platform/common/emu.c

index b6792e0..3f9ccff 100644 (file)
@@ -1358,6 +1358,7 @@ static void emu_loop_prep(void)
 /* our tick here is 1 us right now */\r
 #define ms_to_ticks(x) (unsigned int)(x * 1000)\r
 #define get_ticks() plat_get_ticks_us()\r
+#define vsync_delay_x3 3*ms_to_ticks(1)\r
 \r
 void emu_loop(void)\r
 {\r
@@ -1512,13 +1513,13 @@ void emu_loop(void)
                        diff = timestamp_aim_x3 - timestamp * 3;\r
 \r
                        // sleep or vsync if we are still too fast\r
-                       if (diff > target_frametime_x3 && (currentConfig.EmuOpt & EOPT_VSYNC)) {\r
+                       if (diff > target_frametime_x3 + vsync_delay_x3 && (currentConfig.EmuOpt & EOPT_VSYNC)) {\r
                                // we are too fast\r
                                plat_video_wait_vsync();\r
                                timestamp = get_ticks();\r
                                diff = timestamp * 3 - timestamp_aim_x3;\r
                        }\r
-                       if (diff > target_frametime_x3) {\r
+                       if (diff > target_frametime_x3 + vsync_delay_x3) {\r
                                // still too fast\r
                                plat_wait_till_us(timestamp + (diff - target_frametime_x3) / 3);\r
                        }\r