From 75ce3a1f3c4bf01d2595f81bf2012fb7a8fa7f81 Mon Sep 17 00:00:00 2001
From: ConfuSomu
Date: Sun, 28 Mar 2021 14:12:21 -0400
Subject: Fix possible race condition when switching app

---
 pico-watch.cpp | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

(limited to 'pico-watch.cpp')

diff --git a/pico-watch.cpp b/pico-watch.cpp
index 1469856..87d0965 100644
--- a/pico-watch.cpp
+++ b/pico-watch.cpp
@@ -13,6 +13,7 @@
 int current_app = 0;
 bool is_sleeping = false;
 bool app_ready = true;
+bool app_rendering = false;
 Api app_api;
 
 #define NUMBER_OF_APPS 2
@@ -77,6 +78,7 @@ bool repeating_callback(struct repeating_timer *t) {
 
 void app_switch(int old_appid, int new_appid) {
     app_ready = false;
+    while (app_rendering); // Wait for the app to finish rendering cycle
     if (APPS_DESTROY_ON_EXIT[old_appid]) {
         app_destroy(old_appid);
     }
@@ -97,8 +99,10 @@ int main() {
 
     while (1) {
         if (app_ready && !is_sleeping) {
-            app_render(current_app); // FIXME: This may cause race conditions when switching app
+            app_rendering = true;
+            app_render(current_app);
             app_api.display_write_backbuffer();
+            app_rendering = false;
         }
         sleep_ms(app_api.performance_render_interval_get());
     }
-- 
cgit v1.2.3-54-g00ecf