Updated implementation of barrier pattern

This commit is contained in:
dario
2024-04-18 21:35:27 +02:00
parent 1a8f8c5dff
commit ad3e3c44ee
4 changed files with 152 additions and 20 deletions

View File

@@ -11,20 +11,71 @@
#include <sta/rtos/mutex.hpp>
#include <sta/rtos/event.hpp>
#include <set>
namespace sta
{
class Barrier {
public:
Barrier(const char* name, uint8_t n_threads);
/**
* @brief Construct a new Barrier object
*
* @param name The name of the mutex used for this barrier.
* @param waitTime The minimum time that needs to pass until the barrier can open again.
*/
Barrier(const char* name, uint32_t waitTime = 0);
/**
* @brief Adds the currently running thread to the threads the barrier waits for.
*
*/
void subscribe();
/**
*
* @return uint8_t Returns the number of threads currently subscribed to this barrier
*/
uint8_t getNumSubscribedThreads();
/**
*
* @return uint8_t Returns the number of threads that have entered the barrier and are waiting now.
*/
uint8_t getNumEntered();
/**
*
* @return uint8_t Returns the number of threads that have left the barrier so far.
*/
uint8_t getNumLeft();
void setMinimumWaitDuration(uint32_t duration);
/**
* @brief Wait for the barrier to open again. Sets this state into waiting state.
*
*/
void wait();
/**
* @brief Method for an external thread to wait for the barrier to open without subscribing to it.
*
* @note This means that the barrier doesn't wait for this thread.
*
*/
void externalWait();
private:
RtosMutex mutex_;
RtosEvent flag_;
const uint8_t n_threads_;
uint8_t n_entered_;
uint8_t n_left_;
uint32_t waitTime_;
uint32_t lastTime_;
std::set<osThreadId_t> threads_;
uint8_t nThreads_;
uint8_t nEntered_;
uint8_t nLeft_;
};
} // namespace sta

View File

@@ -82,6 +82,13 @@ namespace sta
*/
RtosThread(const char* name, osPriority_t prio, uint32_t stack_size = 0, uint32_t cb_size = 0);
/**
* @brief Static method for obtaining the ID of the currently running RtosThread.
*
* @return osThreadId_t The ID of the currently running Rtos-Thread.
*/
static osThreadId_t getCurrentlyRunningThreadID();
/**
* @brief Get the currently running instance.
*