fsfw/src/fsfw/osal/freertos/CountingSemaphore.cpp

50 lines
1.6 KiB
C++
Raw Normal View History

2021-07-14 00:54:39 +02:00
#include "fsfw/osal/freertos/CountingSemaphore.h"
2020-09-18 13:15:14 +02:00
#include "FreeRTOS.h"
2022-02-02 10:29:30 +01:00
#include "fsfw/osal/freertos/TaskManagement.h"
2022-05-18 10:51:38 +02:00
#include "fsfw/serviceinterface.h"
#include "semphr.h"
2020-09-18 13:15:14 +02:00
// Make sure #define configUSE_COUNTING_SEMAPHORES 1 is set in
// free FreeRTOSConfig.h file.
2022-02-02 10:29:30 +01:00
CountingSemaphore::CountingSemaphore(const uint8_t maxCount, uint8_t initCount)
: maxCount(maxCount), initCount(initCount) {
if (initCount > maxCount) {
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::error << "CountingSemaphoreUsingTask: Max count bigger than "
"intial cout. Setting initial count to max count."
<< std::endl;
#endif
2022-02-02 10:29:30 +01:00
initCount = maxCount;
}
2020-09-18 13:15:14 +02:00
2022-02-02 10:29:30 +01:00
handle = xSemaphoreCreateCounting(maxCount, initCount);
if (handle == nullptr) {
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::error << "CountingSemaphore: Creation failure" << std::endl;
#endif
2022-02-02 10:29:30 +01:00
}
2020-09-18 13:15:14 +02:00
}
2022-02-02 10:29:30 +01:00
CountingSemaphore::CountingSemaphore(CountingSemaphore&& other)
: maxCount(other.maxCount), initCount(other.initCount) {
handle = xSemaphoreCreateCounting(other.maxCount, other.initCount);
if (handle == nullptr) {
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::error << "CountingSemaphore: Creation failure" << std::endl;
#endif
2022-02-02 10:29:30 +01:00
}
2020-09-18 13:15:14 +02:00
}
2022-02-02 10:29:30 +01:00
CountingSemaphore& CountingSemaphore::operator=(CountingSemaphore&& other) {
handle = xSemaphoreCreateCounting(other.maxCount, other.initCount);
if (handle == nullptr) {
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::error << "CountingSemaphore: Creation failure" << std::endl;
#endif
2022-02-02 10:29:30 +01:00
}
return *this;
2020-09-18 13:15:14 +02:00
}
2022-02-02 10:29:30 +01:00
uint8_t CountingSemaphore::getMaxCount() const { return maxCount; }