diff --git a/miosix/kernel/kernel.h b/miosix/kernel/kernel.h index 92682e30d428a660726621d0955c8ae197476ab1..98782a5015f2b904c80c3daea713e7a218a46988 100755 --- a/miosix/kernel/kernel.h +++ b/miosix/kernel/kernel.h @@ -33,7 +33,6 @@ #include "stdlib_integration/libstdcpp_integration.h" #include "intrusive.h" #include "cpu_time_counter_types.h" -#include <functional> /** * \namespace miosix @@ -1239,26 +1238,6 @@ private: #endif //WITH_CPU_TIME_COUNTER }; -/** - * Function object to compare the priority of two threads. - */ -class LowerPriority: public std::binary_function<Thread*,Thread*,bool> -{ -public: - /** - * \param a first thread to compare - * \param b second thread to compare - * \return true if a->getPriority() < b->getPriority() - * - * Can be called when the kernel is paused. or with interrupts disabled - */ - bool operator() (Thread* a, Thread *b) - { - //Relying on PKgetPriority and IRQgetPriority being the same - return a->getPriority().mutexLessOp(b->getPriority()); - } -}; - /** * \internal * This class is used to make a list of sleeping threads. diff --git a/miosix/kernel/sync.cpp b/miosix/kernel/sync.cpp index f8a7a873ccfb8d0cfeaed8fb9dd40e8ff8ec7e93..c27e5655381bf313f67fb5daebfca1b1e4a087c4 100644 --- a/miosix/kernel/sync.cpp +++ b/miosix/kernel/sync.cpp @@ -36,6 +36,17 @@ using namespace std; namespace miosix { +/** + * Helper lambda to sort threads in a min heap to implement priority inheritance + * \param lhs first thread to compare + * \param rhs second thread to compare + * \return true if lhs->getPriority() < rhs->getPriority() + */ +static auto PKlowerPriority=[](Thread *lhs, Thread *rhs) +{ + return lhs->PKgetPriority().mutexLessOp(rhs->PKgetPriority()); +}; + // // class Mutex // @@ -74,8 +85,7 @@ void Mutex::PKlock(PauseKernelLock& dLock) //Add thread to mutex' waiting queue waiting.push_back(p); - LowerPriority l; - push_heap(waiting.begin(),waiting.end(),l); + push_heap(waiting.begin(),waiting.end(),PKlowerPriority); //Handle priority inheritance if(p->mutexWaiting!=nullptr) errorHandler(UNEXPECTED); @@ -88,7 +98,7 @@ void Mutex::PKlock(PauseKernelLock& dLock) Scheduler::PKsetPriority(walk,p->PKgetPriority()); if(walk->mutexWaiting==nullptr) break; make_heap(walk->mutexWaiting->waiting.begin(), - walk->mutexWaiting->waiting.end(),l); + walk->mutexWaiting->waiting.end(),PKlowerPriority); walk=walk->mutexWaiting->owner; } } @@ -127,8 +137,7 @@ void Mutex::PKlockToDepth(PauseKernelLock& dLock, unsigned int depth) //Add thread to mutex' waiting queue waiting.push_back(p); - LowerPriority l; - push_heap(waiting.begin(),waiting.end(),l); + push_heap(waiting.begin(),waiting.end(),PKlowerPriority); //Handle priority inheritance if(p->mutexWaiting!=nullptr) errorHandler(UNEXPECTED); @@ -141,7 +150,7 @@ void Mutex::PKlockToDepth(PauseKernelLock& dLock, unsigned int depth) Scheduler::PKsetPriority(walk,p->PKgetPriority()); if(walk->mutexWaiting==nullptr) break; make_heap(walk->mutexWaiting->waiting.begin(), - walk->mutexWaiting->waiting.end(),l); + walk->mutexWaiting->waiting.end(),PKlowerPriority); walk=walk->mutexWaiting->owner; } } @@ -229,8 +238,7 @@ bool Mutex::PKunlock(PauseKernelLock& dLock) { //There is at least another thread waiting owner=waiting.front(); - LowerPriority l; - pop_heap(waiting.begin(),waiting.end(),l); + pop_heap(waiting.begin(),waiting.end(),PKlowerPriority); waiting.pop_back(); if(owner->mutexWaiting!=this) errorHandler(UNEXPECTED); owner->mutexWaiting=nullptr; @@ -301,8 +309,7 @@ unsigned int Mutex::PKunlockAllDepthLevels(PauseKernelLock& dLock) { //There is at least another thread waiting owner=waiting.front(); - LowerPriority l; - pop_heap(waiting.begin(),waiting.end(),l); + pop_heap(waiting.begin(),waiting.end(),PKlowerPriority); waiting.pop_back(); if(owner->mutexWaiting!=this) errorHandler(UNEXPECTED); owner->mutexWaiting=nullptr;