zephyr: terminated log processing gently

Added log processing thread gently termination
before chain-load the application.

Signed-off-by: Andrzej Puzdrowski <andrzej.puzdrowski@nordicsemi.no>
diff --git a/boot/zephyr/main.c b/boot/zephyr/main.c
index 2fd80ee..47e118c 100644
--- a/boot/zephyr/main.c
+++ b/boot/zephyr/main.c
@@ -57,13 +57,17 @@
 /* log are processing in custom routine */
 K_THREAD_STACK_DEFINE(boot_log_stack, BOOT_LOG_STACK_SIZE);
 struct k_thread boot_log_thread;
+volatile bool boot_log_stop = false;
+K_SEM_DEFINE(boot_log_sem, 1, 1);
 
 /* log processing need to be initalized by the application */
 #define ZEPHYR_BOOT_LOG_START() zephyr_boot_log_start()
+#define ZEPHYR_BOOT_LOG_STOP() zephyr_boot_log_stop()
 #endif /* CONFIG_LOG_PROCESS_THREAD */
 #else
 /* synchronous log mode doesn't need to be initalized by the application */
 #define ZEPHYR_BOOT_LOG_START() do { } while (false)
+#define ZEPHYR_BOOT_LOG_STOP() do { } while (false)
 #endif /* defined(CONFIG_LOG) && !defined(CONFIG_LOG_IMMEDIATE) */
 
 #ifdef CONFIG_SOC_FAMILY_NRF
@@ -217,9 +221,14 @@
 
      while (1) {
              if (log_process(false) == false) {
+                    if (boot_log_stop) {
+                        break;
+                    }
                     k_sleep(BOOT_LOG_PROCESSING_INTERVAL);
              }
      }
+
+     k_sem_give(&boot_log_sem);
 }
 
 void zephyr_boot_log_start(void)
@@ -233,6 +242,18 @@
 
         k_thread_name_set(&boot_log_thread, "logging");
 }
+
+void zephyr_boot_log_stop(void)
+{
+    boot_log_stop = true;
+
+    /* wait until log procesing thread expired
+     * This can be reworked using a thread_join() API once a such will be
+     * available in zephyr.
+     * see https://github.com/zephyrproject-rtos/zephyr/issues/21500
+     */
+    (void)k_sem_take(&boot_log_sem, K_FOREVER);
+}
 #endif/* defined(CONFIG_LOG) && !defined(CONFIG_LOG_IMMEDIATE) &&\
         !defined(CONFIG_LOG_PROCESS_THREAD) */
 
@@ -322,6 +343,7 @@
                  rsp.br_image_off);
 
     BOOT_LOG_INF("Jumping to the first image slot");
+    ZEPHYR_BOOT_LOG_STOP();
     do_boot(&rsp);
 
     BOOT_LOG_ERR("Never should get here");