From cce174546a58a0d0dc170bc641f89ab86f38547c Mon Sep 17 00:00:00 2001
From: Francisco Molina <femolina@uc.cl>
Date: Sun, 29 Mar 2020 12:11:53 +0200
Subject: [PATCH 04/11] kernel/openos/scheduler: use thread flags, restore
 irq_state

Use thread_flags_wait_any to pause the scheduler. Set flag in
scheduler_push_task to trigger new task handling.

squash! kernel/openos/scheduler: use thread flags
---
 kernel/openos/scheduler.c | 13 ++++++++++++-
 kernel/scheduler.h        |  4 ++--
 2 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/kernel/openos/scheduler.c b/kernel/openos/scheduler.c
index f52f35d4..41be092f 100644
--- a/kernel/openos/scheduler.c
+++ b/kernel/openos/scheduler.c
@@ -10,6 +10,11 @@
 #include "debugpins.h"
 #include "leds.h"
 
+#include "openwsn.h"
+#include "thread.h"
+
+#define OPENWSN_SCHEDULER_FLAG    (1u << 8)
+
 //=========================== variables =======================================
 
 scheduler_vars_t scheduler_vars;
@@ -36,8 +41,11 @@ void scheduler_init(void) {
     SCHEDULER_ENABLE_INTERRUPT();
 }
 
-void scheduler_start(void) {
+void scheduler_start(unsigned state) {
     taskList_item_t* pThisTask;
+
+   irq_restore(state);
+
     while (1) {
         while(scheduler_vars.task_list!=NULL) {
          // there is still at least one task in the linked-list of tasks
@@ -66,6 +74,7 @@ void scheduler_start(void) {
       }
       debugpins_task_clr();
       board_sleep();
+      thread_flags_wait_any(OPENWSN_SCHEDULER_FLAG);
       debugpins_task_set();                      // IAR should halt here if nothing to do
    }
 }
@@ -114,6 +123,8 @@ void scheduler_push_task(task_cbt cb, task_prio_t prio) {
 #endif
 
     ENABLE_INTERRUPTS();
+   thread_t *thread = (thread_t*) thread_get(openwsn_get_pid());
+   thread_flags_set(thread, OPENWSN_SCHEDULER_FLAG);
 }
 
 
diff --git a/kernel/scheduler.h b/kernel/scheduler.h
index e44f7956..51b6d9cb 100644
--- a/kernel/scheduler.h
+++ b/kernel/scheduler.h
@@ -47,7 +47,7 @@ typedef void (*task_cbt)(void);
 //=========================== prototypes ======================================
 
 void scheduler_init(void);
-void scheduler_start(void);
+void scheduler_start(unsigned state);
 void scheduler_push_task(task_cbt task_cb, task_prio_t prio);
 
 #if SCHEDULER_DEBUG_ENABLE
@@ -55,7 +55,7 @@ uint8_t scheduler_debug_get_TasksCur(void);
 uint8_t scheduler_debug_get_TasksMax(void);
 #endif
 
-#include "openos/scheduler_types.h"
+#include "scheduler_types.h"
 
 /**
 \}
-- 
2.28.0