83 lines
1.7 KiB
C++

/*
* barrier.hpp
*
* Created on: Mar 16, 2024
* Author: Dario
*/
#ifndef RTOS2_UTILS_STA_PATTERNS_BARRIER_HPP
#define RTOS2_UTILS_STA_PATTERNS_BARRIER_HPP
#include <sta/rtos/mutex.hpp>
#include <sta/rtos/event.hpp>
#include <set>
namespace sta
{
class Barrier {
public:
/**
* @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_;
uint32_t waitTime_;
uint32_t lastTime_;
std::set<osThreadId_t> threads_;
uint8_t nThreads_;
uint8_t nEntered_;
uint8_t nLeft_;
};
} // namespace sta
#endif /* RTOS2_UTILS_STA_PATTERNS_BARRIER_HPP_ */