FreeRTOS handles were stored plainly and they were deleted in the destructor of classes. This meant that if a class were to be copied, the destructor would be called twice on the same handles and lead to double-free. Seha on Discord suggested to fix this by using `std::unique_ptr` with a custom deletion function. The changes affect: - Thread - Semaphore - Mutex - StreamBuffer - Timer - MessageQueue - EventFlag Thread changes: - Removal of the hack with the `Data` struct - Thread's main body is now just a private static function inside the class. - The C functions were relocated to static class members PubSub changes: - Refactored pubsub into class - Renamed files to `PubSub` instead of `Pubsub` - `PubSubSubscription` is now a private inner struct and `PubSub` only exposes `SubscriptionHandle` Lockable, ScopedLockable, Mutex: - Added `lock()` method that locks indefinitely - Remove deprecated `acquire()` and `release()` methods - Removed `TtWaitForever` in favour of `portMAX_DELAY`
78 lines
2.0 KiB
C++
78 lines
2.0 KiB
C++
#include "Mutex.h"
|
|
|
|
#include "Check.h"
|
|
#include "CoreDefines.h"
|
|
#include "Log.h"
|
|
|
|
namespace tt {
|
|
|
|
#define MUTEX_DEBUGGING false
|
|
|
|
#if MUTEX_DEBUGGING
|
|
#define TAG "mutex"
|
|
void tt_mutex_info(Mutex mutex, const char* label) {
|
|
MutexData* data = (MutexData*)mutex;
|
|
if (data == NULL) {
|
|
TT_LOG_I(TAG, "mutex %s: is NULL", label);
|
|
} else {
|
|
TT_LOG_I(TAG, "mutex %s: handle=%0X type=%d owner=%0x", label, data->handle, data->type, tt_mutex_get_owner(mutex));
|
|
}
|
|
}
|
|
#else
|
|
#define tt_mutex_info(mutex, text)
|
|
#endif
|
|
|
|
static inline SemaphoreHandle_t createSemaphoreHandle(Mutex::Type type) {
|
|
switch (type) {
|
|
case Mutex::Type::Normal:
|
|
return xSemaphoreCreateMutex();
|
|
case Mutex::Type::Recursive:
|
|
return xSemaphoreCreateRecursiveMutex();
|
|
default:
|
|
tt_crash("Mutex type unknown/corrupted");
|
|
}
|
|
}
|
|
|
|
Mutex::Mutex(Type type) : handle(createSemaphoreHandle(type)), type(type) {
|
|
tt_mutex_info(data, "alloc");
|
|
assert(handle != nullptr);
|
|
}
|
|
|
|
bool Mutex::lock(TickType_t timeout) const {
|
|
assert(!TT_IS_IRQ_MODE());
|
|
assert(handle != nullptr);
|
|
tt_mutex_info(mutex, "acquire");
|
|
|
|
switch (type) {
|
|
case Type::Normal:
|
|
return xSemaphoreTake(handle.get(), timeout) == pdPASS;
|
|
case Type::Recursive:
|
|
return xSemaphoreTakeRecursive(handle.get(), timeout) == pdPASS;
|
|
default:
|
|
tt_crash();
|
|
}
|
|
}
|
|
|
|
bool Mutex::unlock() const {
|
|
assert(!TT_IS_IRQ_MODE());
|
|
assert(handle != nullptr);
|
|
tt_mutex_info(mutex, "release");
|
|
|
|
switch (type) {
|
|
case Type::Normal:
|
|
return xSemaphoreGive(handle.get()) == pdPASS;
|
|
case Type::Recursive:
|
|
return xSemaphoreGiveRecursive(handle.get()) == pdPASS;
|
|
default:
|
|
tt_crash();
|
|
}
|
|
}
|
|
|
|
ThreadId Mutex::getOwner() const {
|
|
assert(!TT_IS_IRQ_MODE());
|
|
assert(handle != nullptr);
|
|
return (ThreadId)xSemaphoreGetMutexHolder(handle.get());
|
|
}
|
|
|
|
} // namespace
|