diff --git a/miosix/kernel/kernel.cpp b/miosix/kernel/kernel.cpp index 2800811c271b881a56a9626f57285dae74bc42c2..a854c073d14b0ef46e595175ee819ea97d0c4811 100755 --- a/miosix/kernel/kernel.cpp +++ b/miosix/kernel/kernel.cpp @@ -484,25 +484,26 @@ bool Thread::exists(Thread *p) bool Thread::IRQexists(Thread* p) { if(p==nullptr) return false; + //NOTE: the code in all schedulers is currently safe to be called also with + //interrupts disabled return Scheduler::PKexists(p); } Priority Thread::getPriority() { + //NOTE: the code in all schedulers is currently safe to be called either + //with interrupt enabed or not, and with the kernel paused or not, so + //PKgetPriority() and IRQgetPriority() directly call here. If introducing + //changes that break this property, these functions may need to be split return Scheduler::getPriority(this); } -Priority Thread::IRQgetPriority() -{ - return Scheduler::IRQgetPriority(this); -} - void Thread::setPriority(Priority pr) { if(pr.validate()==false) return; PauseKernelLock lock; - Thread *running=getCurrentThread(); + Thread *running=PKgetCurrentThread(); //If thread is locking at least one mutex if(running->mutexLocked!=nullptr) { @@ -516,13 +517,13 @@ void Thread::setPriority(Priority pr) while(walk!=nullptr) { if(walk->waiting.empty()==false) - pr=std::max(pr,walk->waiting.front()->getPriority()); + pr=std::max(pr,walk->waiting.front()->PKgetPriority()); walk=walk->next; } } //If old priority == desired priority, nothing to do. - if(pr==running->getPriority()) return; + if(pr==running->PKgetPriority()) return; Scheduler::PKsetPriority(running,pr); #ifdef SCHED_TYPE_EDF if(isKernelRunning()) yield(); //Another thread might have a closer deadline diff --git a/miosix/kernel/kernel.h b/miosix/kernel/kernel.h index 1f1abb248267759c92c42e272d75abe5c7f4c858..92682e30d428a660726621d0955c8ae197476ab1 100755 --- a/miosix/kernel/kernel.h +++ b/miosix/kernel/kernel.h @@ -786,16 +786,25 @@ public: * function returns the current priority, which can be higher than the * original priority due to priority inheritance. * \return current priority of the thread - * - * Can be called when the kernel is paused. */ Priority getPriority(); /** - * Same as getPriority(), but meant to be used inside an IRQ, when - * interrupts are disabled or when the kernel is paused. + * Same as getPriority(), but meant to be used when the kernel is paused. + */ + Priority PKgetPriority() + { + return getPriority(); //Safe to call directly, see implementation + } + + /** + * Same as getPriority(), but meant to be used inside an IRQ, or when + * interrupts are disabled. */ - Priority IRQgetPriority(); + Priority IRQgetPriority() + { + return getPriority(); //Safe to call directly, see implementation + } /** * Set the priority of this thread.<br> @@ -1245,6 +1254,7 @@ public: */ bool operator() (Thread* a, Thread *b) { + //Relying on PKgetPriority and IRQgetPriority being the same return a->getPriority().mutexLessOp(b->getPriority()); } }; diff --git a/miosix/kernel/scheduler/control/control_scheduler.cpp b/miosix/kernel/scheduler/control/control_scheduler.cpp index b162038882cfbceef1bed6973aed2e1b35602c29..68630cfbea603375722610f664ecd4b76a1b3132 100644 --- a/miosix/kernel/scheduler/control/control_scheduler.cpp +++ b/miosix/kernel/scheduler/control/control_scheduler.cpp @@ -442,7 +442,7 @@ static IntrusiveList<ThreadsListItem>::iterator curInRound=activeThreads.end(); static inline void addThreadToActiveList(ThreadsListItem *atlEntry) { - switch(atlEntry->t->getPriority().getRealtime()) + switch(atlEntry->t->IRQgetPriority().getRealtime()) { case ControlRealtimePriority::REALTIME_PRIORITY_IMMEDIATE: activeThreads.insert(curInRound,atlEntry); diff --git a/miosix/kernel/scheduler/control/control_scheduler.h b/miosix/kernel/scheduler/control/control_scheduler.h index d0875b5c60d1cd202494f87faf2e155eb0a2c364..2c974276329af213939b82a5621392c19531786a 100755 --- a/miosix/kernel/scheduler/control/control_scheduler.h +++ b/miosix/kernel/scheduler/control/control_scheduler.h @@ -88,7 +88,8 @@ public: /** * \internal - * Get the priority of a thread. + * Get the priority of a thread. Must be callable also with kernel paused + * or IRQ disabled. * Note that the meaning of priority is scheduler specific. * \param thread thread whose priority needs to be queried. * \return the priority of thread. @@ -98,17 +99,6 @@ public: return thread->schedData.priority; } - /** - * \internal - * Same as getPriority, but meant to be called with interrupts disabled. - * \param thread thread whose priority needs to be queried. - * \return the priority of thread. - */ - static ControlSchedulerPriority IRQgetPriority(Thread *thread) - { - return thread->schedData.priority; - } - /** * \internal * This is called before the kernel is started to by the kernel. The given diff --git a/miosix/kernel/scheduler/edf/edf_scheduler.h b/miosix/kernel/scheduler/edf/edf_scheduler.h index ab5c4122e47a2004079b970fc068b3accf56821f..4750eefa584b0f5b8df691448cea8a98557d0498 100755 --- a/miosix/kernel/scheduler/edf/edf_scheduler.h +++ b/miosix/kernel/scheduler/edf/edf_scheduler.h @@ -85,7 +85,8 @@ public: /** * \internal - * Get the priority of a thread. + * Get the priority of a thread. Must be callable also with kernel paused + * or IRQ disabled. * Note that the meaning of priority is scheduler specific. * \param thread thread whose priority needs to be queried. * \return the priority of thread. @@ -95,16 +96,6 @@ public: return thread->schedData.deadline; } - /** - * Same as getPriority, but meant to be called with interrupts disabled. - * \param thread thread whose priority needs to be queried. - * \return the priority of thread. - */ - static EDFSchedulerPriority IRQgetPriority(Thread *thread) - { - return thread->schedData.deadline; - } - /** * \internal * This is called before the kernel is started to by the kernel. The given diff --git a/miosix/kernel/scheduler/priority/priority_scheduler.cpp b/miosix/kernel/scheduler/priority/priority_scheduler.cpp index 435451ee684e032d44b6138885f384c217de5db1..ee1425c2d99c88342e4454520c7967839ddf45ef 100644 --- a/miosix/kernel/scheduler/priority/priority_scheduler.cpp +++ b/miosix/kernel/scheduler/priority/priority_scheduler.cpp @@ -140,7 +140,7 @@ void PriorityScheduler::PKremoveDeadThreads() void PriorityScheduler::PKsetPriority(Thread *thread, PrioritySchedulerPriority newPriority) { - PrioritySchedulerPriority oldPriority=getPriority(thread); + PrioritySchedulerPriority oldPriority=thread->PKgetPriority(); //First set priority to the new value thread->schedData.priority=newPriority; //Then remove the thread from its old list diff --git a/miosix/kernel/scheduler/priority/priority_scheduler.h b/miosix/kernel/scheduler/priority/priority_scheduler.h index c5e34cc5a362c1d0db3c6f84fb67d5e403efaaac..b27dd91390cf41ab577e2a243fe092799c120c78 100755 --- a/miosix/kernel/scheduler/priority/priority_scheduler.h +++ b/miosix/kernel/scheduler/priority/priority_scheduler.h @@ -86,7 +86,8 @@ public: /** * \internal - * Get the priority of a thread. + * Get the priority of a thread. Must be callable also with kernel paused + * or IRQ disabled. * Note that the meaning of priority is scheduler specific. * \param thread thread whose priority needs to be queried. * \return the priority of thread. @@ -96,17 +97,6 @@ public: return thread->schedData.priority; } - /** - * \internal - * Same as getPriority, but meant to be called with interrupts disabled. - * \param thread thread whose priority needs to be queried. - * \return the priority of thread. - */ - static PrioritySchedulerPriority IRQgetPriority(Thread *thread) - { - return thread->schedData.priority; - } - /** * \internal * This is called before the kernel is started to by the kernel. The given diff --git a/miosix/kernel/scheduler/scheduler.h b/miosix/kernel/scheduler/scheduler.h index 60a36ff149dbd9cf47d0af3fd476c5d653c0ea6c..0398f161f228d977b4da69e78f7394d86611b318 100755 --- a/miosix/kernel/scheduler/scheduler.h +++ b/miosix/kernel/scheduler/scheduler.h @@ -115,7 +115,8 @@ public: /** * \internal - * Get the priority of a thread. + * Get the priority of a thread. Must be callable also with kernel paused + * or IRQ disabled. * Note that the meaning of priority is scheduler specific. * \param thread thread whose priority needs to be queried. * \return the priority of thread. @@ -125,17 +126,6 @@ public: return T::getPriority(thread); } - /** - * \internal - * Same as getPriority, but meant to be called with interrupts disabled. - * \param thread thread whose priority needs to be queried. - * \return the priority of thread. - */ - static Priority IRQgetPriority(Thread *thread) - { - return T::IRQgetPriority(thread); - } - /** * \internal * This is called before the kernel is started to by the kernel. The given diff --git a/miosix/kernel/sync.cpp b/miosix/kernel/sync.cpp index d893946010221ffce02d70b7a8d0cbef6e473da8..f8a7a873ccfb8d0cfeaed8fb9dd40e8ff8ec7e93 100644 --- a/miosix/kernel/sync.cpp +++ b/miosix/kernel/sync.cpp @@ -53,7 +53,7 @@ void Mutex::PKlock(PauseKernelLock& dLock) owner=p; //Save original thread priority, if the thread has not yet locked //another mutex - if(owner->mutexLocked==nullptr) owner->savedPriority=owner->getPriority(); + if(owner->mutexLocked==nullptr) owner->savedPriority=owner->PKgetPriority(); //Add this mutex to the list of mutexes locked by owner this->next=owner->mutexLocked; owner->mutexLocked=this; @@ -80,12 +80,12 @@ void Mutex::PKlock(PauseKernelLock& dLock) //Handle priority inheritance if(p->mutexWaiting!=nullptr) errorHandler(UNEXPECTED); p->mutexWaiting=this; - if(owner->getPriority().mutexLessOp(p->getPriority())) + if(owner->PKgetPriority().mutexLessOp(p->PKgetPriority())) { Thread *walk=owner; for(;;) { - Scheduler::PKsetPriority(walk,p->getPriority()); + Scheduler::PKsetPriority(walk,p->PKgetPriority()); if(walk->mutexWaiting==nullptr) break; make_heap(walk->mutexWaiting->waiting.begin(), walk->mutexWaiting->waiting.end(),l); @@ -106,7 +106,7 @@ void Mutex::PKlockToDepth(PauseKernelLock& dLock, unsigned int depth) if(recursiveDepth>=0) recursiveDepth=depth; //Save original thread priority, if the thread has not yet locked //another mutex - if(owner->mutexLocked==nullptr) owner->savedPriority=owner->getPriority(); + if(owner->mutexLocked==nullptr) owner->savedPriority=owner->PKgetPriority(); //Add this mutex to the list of mutexes locked by owner this->next=owner->mutexLocked; owner->mutexLocked=this; @@ -133,12 +133,12 @@ void Mutex::PKlockToDepth(PauseKernelLock& dLock, unsigned int depth) //Handle priority inheritance if(p->mutexWaiting!=nullptr) errorHandler(UNEXPECTED); p->mutexWaiting=this; - if(owner->getPriority().mutexLessOp(p->getPriority())) + if(owner->PKgetPriority().mutexLessOp(p->PKgetPriority())) { Thread *walk=owner; for(;;) { - Scheduler::PKsetPriority(walk,p->getPriority()); + Scheduler::PKsetPriority(walk,p->PKgetPriority()); if(walk->mutexWaiting==nullptr) break; make_heap(walk->mutexWaiting->waiting.begin(), walk->mutexWaiting->waiting.end(),l); @@ -159,7 +159,7 @@ bool Mutex::PKtryLock(PauseKernelLock& dLock) owner=p; //Save original thread priority, if the thread has not yet locked //another mutex - if(owner->mutexLocked==nullptr) owner->savedPriority=owner->getPriority(); + if(owner->mutexLocked==nullptr) owner->savedPriority=owner->PKgetPriority(); //Add this mutex to the list of mutexes locked by owner this->next=owner->mutexLocked; owner->mutexLocked=this; @@ -207,7 +207,7 @@ bool Mutex::PKunlock(PauseKernelLock& dLock) if(owner->mutexLocked==nullptr) { //Not locking any other mutex - if(owner->savedPriority!=owner->getPriority()) + if(owner->savedPriority!=owner->PKgetPriority()) Scheduler::PKsetPriority(owner,owner->savedPriority); } else { Priority pr=owner->savedPriority; @@ -217,11 +217,11 @@ bool Mutex::PKunlock(PauseKernelLock& dLock) while(walk!=nullptr) { if(walk->waiting.empty()==false) - if (pr.mutexLessOp(walk->waiting.front()->getPriority())) - pr = walk->waiting.front()->getPriority(); + if(pr.mutexLessOp(walk->waiting.front()->PKgetPriority())) + pr=walk->waiting.front()->PKgetPriority(); walk=walk->next; } - if(pr!=owner->getPriority()) Scheduler::PKsetPriority(owner,pr); + if(pr!=owner->PKgetPriority()) Scheduler::PKsetPriority(owner,pr); } //Choose next thread to lock the mutex @@ -235,15 +235,15 @@ bool Mutex::PKunlock(PauseKernelLock& dLock) if(owner->mutexWaiting!=this) errorHandler(UNEXPECTED); owner->mutexWaiting=nullptr; owner->PKwakeup(); - if(owner->mutexLocked==nullptr) owner->savedPriority=owner->getPriority(); + if(owner->mutexLocked==nullptr) owner->savedPriority=owner->PKgetPriority(); //Add this mutex to the list of mutexes locked by owner this->next=owner->mutexLocked; owner->mutexLocked=this; //Handle priority inheritance of new owner if(waiting.empty()==false && - owner->getPriority().mutexLessOp(waiting.front()->getPriority())) - Scheduler::PKsetPriority(owner,waiting.front()->getPriority()); - return p->getPriority().mutexLessOp(owner->getPriority()); + owner->PKgetPriority().mutexLessOp(waiting.front()->PKgetPriority())) + Scheduler::PKsetPriority(owner,waiting.front()->PKgetPriority()); + return p->PKgetPriority().mutexLessOp(owner->PKgetPriority()); } else { owner=nullptr; //No threads waiting std::vector<Thread *>().swap(waiting); //Save some RAM @@ -279,7 +279,7 @@ unsigned int Mutex::PKunlockAllDepthLevels(PauseKernelLock& dLock) if(owner->mutexLocked==nullptr) { //Not locking any other mutex - if(owner->savedPriority!=owner->getPriority()) + if(owner->savedPriority!=owner->PKgetPriority()) Scheduler::PKsetPriority(owner,owner->savedPriority); } else { Priority pr=owner->savedPriority; @@ -289,11 +289,11 @@ unsigned int Mutex::PKunlockAllDepthLevels(PauseKernelLock& dLock) while(walk!=nullptr) { if(walk->waiting.empty()==false) - if(pr.mutexLessOp(walk->waiting.front()->getPriority())) - pr=walk->waiting.front()->getPriority(); + if(pr.mutexLessOp(walk->waiting.front()->PKgetPriority())) + pr=walk->waiting.front()->PKgetPriority(); walk=walk->next; } - if(pr!=owner->getPriority()) Scheduler::PKsetPriority(owner,pr); + if(pr!=owner->PKgetPriority()) Scheduler::PKsetPriority(owner,pr); } //Choose next thread to lock the mutex @@ -307,14 +307,14 @@ unsigned int Mutex::PKunlockAllDepthLevels(PauseKernelLock& dLock) if(owner->mutexWaiting!=this) errorHandler(UNEXPECTED); owner->mutexWaiting=nullptr; owner->PKwakeup(); - if(owner->mutexLocked==nullptr) owner->savedPriority=owner->getPriority(); + if(owner->mutexLocked==nullptr) owner->savedPriority=owner->PKgetPriority(); //Add this mutex to the list of mutexes locked by owner this->next=owner->mutexLocked; owner->mutexLocked=this; //Handle priority inheritance of new owner if(waiting.empty()==false && - owner->getPriority().mutexLessOp(waiting.front()->getPriority())) - Scheduler::PKsetPriority(owner,waiting.front()->getPriority()); + owner->PKgetPriority().mutexLessOp(waiting.front()->PKgetPriority())) + Scheduler::PKsetPriority(owner,waiting.front()->PKgetPriority()); } else { owner=nullptr; //No threads waiting std::vector<Thread *>().swap(waiting); //Save some RAM @@ -404,7 +404,7 @@ bool ConditionVariable::doBroadcast() Thread *t=condList.front()->thread; condList.pop_front(); t->PKwakeup(); - if(t->getPriority()>Thread::PKgetCurrentThread()->getPriority()) + if(t->PKgetPriority()>Thread::PKgetCurrentThread()->PKgetPriority()) hppw=true; } return hppw;