From d766506d6ae991f4459838689205ad7d2cbfe5de Mon Sep 17 00:00:00 2001
From: sasan-golchin <ahmad.golchin@mail.polimi.it>
Date: Tue, 8 Nov 2016 11:38:13 +0100
Subject: [PATCH] Control Scheduler FIX - preemption priority - less-than and
 greater-than operators fixed

---
 miosix/kernel/scheduler/control/control_scheduler.cpp  |  3 ++-
 .../kernel/scheduler/control/control_scheduler_types.h | 10 ++++++----
 2 files changed, 8 insertions(+), 5 deletions(-)

diff --git a/miosix/kernel/scheduler/control/control_scheduler.cpp b/miosix/kernel/scheduler/control/control_scheduler.cpp
index a774893f..8952dc4f 100644
--- a/miosix/kernel/scheduler/control/control_scheduler.cpp
+++ b/miosix/kernel/scheduler/control/control_scheduler.cpp
@@ -200,7 +200,8 @@ long long ControlScheduler::IRQgetNextPreemption()
 // Should be called when the current thread is the idle thread
 static inline void IRQsetNextPreemptionForIdle(){
     if (sleepingList->empty())
-        //normally should happen unless an IRQ is set!
+        //normally should not happen unless an IRQ is already set and able to
+        //preempt idle thread
         nextPreemption = numeric_limits<long long>::max(); 
     else
         nextPreemption = sleepingList->front()->wakeup_time;
diff --git a/miosix/kernel/scheduler/control/control_scheduler_types.h b/miosix/kernel/scheduler/control/control_scheduler_types.h
index 3b423156..bcf35f99 100755
--- a/miosix/kernel/scheduler/control/control_scheduler_types.h
+++ b/miosix/kernel/scheduler/control/control_scheduler_types.h
@@ -125,13 +125,15 @@ private:
 
 inline bool operator <(ControlSchedulerPriority a, ControlSchedulerPriority b)
 {
-    return (b.getRealtime() == 1 && a.getRealtime() != 1) ||
-            (a.getRealtime() != 1 && b.getRealtime() != 1 && a.get() < b.get());
+    //rule 1) Idle thread should be always preempted by any thread!
+    //rule 2) Only REALTIME_PRIORITY_IMMEDIATE threads can preempt other threads
+    //right away, for other real-time priorities, the scheduler does not
+    //require to be called before the end of the current burst!
+    return a.get()==-1 || (a.getRealtime() != 1 && b.getRealtime() == 1);
 }
 
 inline bool operator>(ControlSchedulerPriority a, ControlSchedulerPriority b){
-    return (a.getRealtime() == 1 && b.getRealtime() != 1) ||
-            (a.getRealtime() != 1 && b.getRealtime() != 1 && a.get() > b.get());
+    return b.get()==-1 || (a.getRealtime() == 1 && b.getRealtime() != 1);
 }
 
 inline bool operator ==(ControlSchedulerPriority a, ControlSchedulerPriority b)
-- 
GitLab