2021-07-14 10:50:44 +02:00
|
|
|
#include "fsfw/osal/windows/winTaskHelpers.h"
|
|
|
|
|
2022-09-27 21:46:11 +02:00
|
|
|
#include <windows.h>
|
2021-03-21 16:20:13 +01:00
|
|
|
|
2022-11-09 17:54:08 +01:00
|
|
|
#include <mutex>
|
|
|
|
|
2021-03-21 16:20:13 +01:00
|
|
|
TaskPriority tasks::makeWinPriority(PriorityClass prioClass, PriorityNumber prioNumber) {
|
2022-02-02 10:29:30 +01:00
|
|
|
return (static_cast<uint16_t>(prioClass) << 16) | static_cast<uint16_t>(prioNumber);
|
2021-03-21 16:20:13 +01:00
|
|
|
}
|
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
void tasks::getWinPriorityParameters(TaskPriority priority, DWORD& priorityClass,
|
|
|
|
int& priorityNumber) {
|
|
|
|
PriorityClass classInternal = static_cast<PriorityClass>(priority >> 16 & 0xff);
|
|
|
|
PriorityNumber numberInternal = static_cast<PriorityNumber>(priority & 0xff);
|
|
|
|
switch (classInternal) {
|
|
|
|
case (CLASS_IDLE): {
|
|
|
|
priorityClass = IDLE_PRIORITY_CLASS;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case (CLASS_BELOW_NORMAL): {
|
|
|
|
priorityClass = BELOW_NORMAL_PRIORITY_CLASS;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case (CLASS_NORMAL): {
|
|
|
|
priorityClass = NORMAL_PRIORITY_CLASS;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case (CLASS_ABOVE_NORMAL): {
|
|
|
|
priorityClass = ABOVE_NORMAL_PRIORITY_CLASS;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case (CLASS_HIGH): {
|
|
|
|
priorityClass = HIGH_PRIORITY_CLASS;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case (CLASS_REALTIME): {
|
|
|
|
priorityClass = REALTIME_PRIORITY_CLASS;
|
|
|
|
break;
|
2021-03-21 16:20:13 +01:00
|
|
|
}
|
|
|
|
default: {
|
2022-02-02 10:29:30 +01:00
|
|
|
priorityClass = NORMAL_PRIORITY_CLASS;
|
2021-03-21 16:20:13 +01:00
|
|
|
}
|
2022-02-02 10:29:30 +01:00
|
|
|
}
|
2021-03-21 16:20:13 +01:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
switch (numberInternal) {
|
|
|
|
case (IDLE): {
|
|
|
|
priorityNumber = THREAD_PRIORITY_IDLE;
|
|
|
|
break;
|
2021-03-21 16:20:13 +01:00
|
|
|
}
|
2022-02-02 10:29:30 +01:00
|
|
|
case (LOWEST): {
|
|
|
|
priorityNumber = THREAD_PRIORITY_LOWEST;
|
|
|
|
break;
|
2021-03-21 16:20:13 +01:00
|
|
|
}
|
2022-02-02 10:29:30 +01:00
|
|
|
case (BELOW_NORMAL): {
|
|
|
|
priorityNumber = THREAD_PRIORITY_BELOW_NORMAL;
|
|
|
|
break;
|
2021-03-21 16:20:13 +01:00
|
|
|
}
|
2022-02-02 10:29:30 +01:00
|
|
|
case (NORMAL): {
|
|
|
|
priorityNumber = THREAD_PRIORITY_NORMAL;
|
|
|
|
break;
|
2021-03-21 16:20:13 +01:00
|
|
|
}
|
2022-02-02 10:29:30 +01:00
|
|
|
case (ABOVE_NORMAL): {
|
|
|
|
priorityNumber = THREAD_PRIORITY_ABOVE_NORMAL;
|
|
|
|
break;
|
2021-03-21 16:20:13 +01:00
|
|
|
}
|
2022-02-02 10:29:30 +01:00
|
|
|
case (HIGHEST): {
|
|
|
|
priorityNumber = THREAD_PRIORITY_HIGHEST;
|
|
|
|
break;
|
2021-03-21 16:20:13 +01:00
|
|
|
}
|
2022-02-02 10:29:30 +01:00
|
|
|
case (CRITICAL): {
|
|
|
|
priorityNumber = THREAD_PRIORITY_TIME_CRITICAL;
|
|
|
|
break;
|
2021-03-21 16:20:13 +01:00
|
|
|
}
|
|
|
|
default: {
|
2022-02-02 10:29:30 +01:00
|
|
|
priorityNumber = THREAD_PRIORITY_NORMAL;
|
2021-03-21 16:20:13 +01:00
|
|
|
}
|
2022-02-02 10:29:30 +01:00
|
|
|
}
|
2021-03-21 16:20:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t tasks::setTaskPriority(HANDLE nativeHandle, TaskPriority priority) {
|
2022-02-02 10:29:30 +01:00
|
|
|
/* List of possible priority classes:
|
|
|
|
https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-setpriorityclass
|
|
|
|
And respective thread priority numbers:
|
|
|
|
https://docs.microsoft.com/en-us/windows/win32/procthread/scheduling-priorities
|
|
|
|
*/
|
|
|
|
DWORD dwPriorityClass = 0;
|
|
|
|
int nPriorityNumber = 0;
|
|
|
|
tasks::getWinPriorityParameters(priority, dwPriorityClass, nPriorityNumber);
|
|
|
|
int result = SetPriorityClass(reinterpret_cast<HANDLE>(nativeHandle), dwPriorityClass);
|
|
|
|
if (result != 0) {
|
2021-03-21 16:20:13 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::error << "PeriodicTask: Windows SetPriorityClass failed with code " << GetLastError()
|
|
|
|
<< std::endl;
|
2022-08-15 20:28:16 +02:00
|
|
|
return returnvalue::FAILED;
|
2021-03-21 16:20:13 +01:00
|
|
|
#endif
|
2022-02-02 10:29:30 +01:00
|
|
|
}
|
|
|
|
result = SetThreadPriority(reinterpret_cast<HANDLE>(nativeHandle), nPriorityNumber);
|
|
|
|
if (result != 0) {
|
2021-03-21 16:20:13 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::error << "PeriodicTask: Windows SetPriorityClass failed with code " << GetLastError()
|
|
|
|
<< std::endl;
|
2022-08-15 20:28:16 +02:00
|
|
|
return returnvalue::FAILED;
|
2021-03-21 16:20:13 +01:00
|
|
|
#endif
|
2022-02-02 10:29:30 +01:00
|
|
|
}
|
2022-08-15 20:28:16 +02:00
|
|
|
return returnvalue::OK;
|
2021-03-21 16:20:13 +01:00
|
|
|
}
|