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`
66 lines
1.4 KiB
C++
66 lines
1.4 KiB
C++
/**
|
|
* @file mutex.h
|
|
* Mutex
|
|
*/
|
|
#pragma once
|
|
|
|
#include "Thread.h"
|
|
#include "RtosCompatSemaphore.h"
|
|
#include "Check.h"
|
|
#include "Lockable.h"
|
|
#include <memory>
|
|
|
|
namespace tt {
|
|
|
|
/**
|
|
* Wrapper for FreeRTOS xSemaphoreCreateMutex and xSemaphoreCreateRecursiveMutex
|
|
* Cannot be used in IRQ mode (within ISR context)
|
|
*/
|
|
class Mutex final : public Lockable {
|
|
|
|
public:
|
|
|
|
enum class Type {
|
|
Normal,
|
|
Recursive,
|
|
};
|
|
|
|
private:
|
|
|
|
struct SemaphoreHandleDeleter {
|
|
void operator()(QueueHandle_t handleToDelete) {
|
|
assert(!TT_IS_IRQ_MODE());
|
|
vSemaphoreDelete(handleToDelete);
|
|
}
|
|
};
|
|
|
|
std::unique_ptr<std::remove_pointer_t<QueueHandle_t>, SemaphoreHandleDeleter> handle;
|
|
Type type;
|
|
|
|
public:
|
|
|
|
explicit Mutex(Type type = Type::Normal);
|
|
~Mutex() override = default;
|
|
|
|
/** Attempt to lock the mutex. Blocks until timeout passes or lock is acquired.
|
|
* @param[in] timeout
|
|
* @return success result
|
|
*/
|
|
bool lock(TickType_t timeout) const override;
|
|
|
|
/** Attempt to lock the mutex. Blocks until lock is acquired, without timeout.
|
|
* @return success result
|
|
*/
|
|
bool lock() const override { return lock(portMAX_DELAY); }
|
|
|
|
/** Attempt to unlock the mutex.
|
|
* @return success result
|
|
*/
|
|
bool unlock() const override;
|
|
|
|
/** @return the owner of the thread */
|
|
ThreadId getOwner() const;
|
|
};
|
|
|
|
} // namespace
|