| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // ====================================================================== | ||
| 2 | // \title Os/CountingSemaphore.hpp | ||
| 3 | // \brief common function definitions for Os::CountingSemaphore | ||
| 4 | // ====================================================================== | ||
| 5 | #ifndef OS_COUNTING_SEMAPHORE_HPP_ | ||
| 6 | #define OS_COUNTING_SEMAPHORE_HPP_ | ||
| 7 | #include "Fw/Time/TimeInterval.hpp" | ||
| 8 | #include "Os/Os.hpp" | ||
| 9 | |||
| 10 | namespace Os { | ||
| 11 | |||
| 12 | class CountingSemaphoreHandle {}; | ||
| 13 | |||
| 14 | class CountingSemaphoreInterface { | ||
| 15 | public: | ||
| 16 | enum Status { | ||
| 17 | OP_OK, //!< Operation was successful | ||
| 18 | ERROR_TIMEOUT, //!< Timeout occurred during wait | ||
| 19 | ERROR_INVALID, //!< Invalid semaphore or argument | ||
| 20 | ERROR_NOT_IMPLEMENTED, //!< Feature not implemented | ||
| 21 | NOT_SUPPORTED, //!< CountingSemaphore does not support operation | ||
| 22 | ERROR_OTHER //!< All other errors | ||
| 23 | }; | ||
| 24 | |||
| 25 | //! \brief default constructor | ||
| 26 | ✗ | CountingSemaphoreInterface() = default; | |
| 27 | |||
| 28 | //! \brief default virtual destructor | ||
| 29 | ✗ | virtual ~CountingSemaphoreInterface() = default; | |
| 30 | |||
| 31 | //! \brief copy constructor is forbidden | ||
| 32 | CountingSemaphoreInterface(const CountingSemaphoreInterface& other) = delete; | ||
| 33 | |||
| 34 | //! \brief assignment operator is forbidden | ||
| 35 | CountingSemaphoreInterface& operator=(const CountingSemaphoreInterface& other) = delete; | ||
| 36 | |||
| 37 | //! \brief wait (decrement) the semaphore, blocking if count is zero | ||
| 38 | //! \return status of the operation | ||
| 39 | virtual Status wait() = 0; | ||
| 40 | |||
| 41 | //! \brief wait on the semaphore with a timeout | ||
| 42 | //! \param interval maximum time to wait | ||
| 43 | //! \return OP_OK on success, ERROR_TIMEOUT if the interval elapsed | ||
| 44 | virtual Status waitTimeout(const Fw::TimeInterval& interval) = 0; | ||
| 45 | |||
| 46 | //! \brief non-blocking attempt to decrement the semaphore | ||
| 47 | //! \return OP_OK if decremented, ERROR_TIMEOUT if count was zero | ||
| 48 | virtual Status tryWait() = 0; | ||
| 49 | |||
| 50 | //! \brief post (increment) the semaphore, potentially waking a waiting thread | ||
| 51 | //! \return status of the operation | ||
| 52 | virtual Status post() = 0; | ||
| 53 | |||
| 54 | //! \brief return the underlying semaphore handle (implementation specific) | ||
| 55 | //! \return internal semaphore handle representation | ||
| 56 | virtual CountingSemaphoreHandle* getHandle() = 0; | ||
| 57 | |||
| 58 | //! \brief provide a pointer to a CountingSemaphore delegate object | ||
| 59 | static CountingSemaphoreInterface* getDelegate(CountingSemaphoreHandleStorage& aligned_new_memory, | ||
| 60 | U32 initial_count); | ||
| 61 | }; | ||
| 62 | |||
| 63 | class CountingSemaphore final : public CountingSemaphoreInterface { | ||
| 64 | public: | ||
| 65 | explicit CountingSemaphore(U32 initial_count); //!< Constructor with initial count | ||
| 66 | |||
| 67 | ~CountingSemaphore() final; //!< Destructor | ||
| 68 | |||
| 69 | CountingSemaphore(const CountingSemaphoreInterface& other) = delete; | ||
| 70 | |||
| 71 | CountingSemaphore(const CountingSemaphore& other) = delete; | ||
| 72 | CountingSemaphore& operator=(const CountingSemaphore& other) = delete; | ||
| 73 | CountingSemaphore(CountingSemaphore&& other) = delete; | ||
| 74 | CountingSemaphore& operator=(CountingSemaphore&& other) = delete; | ||
| 75 | |||
| 76 | Status wait() override; //!< wait (decrement), blocking if count is zero | ||
| 77 | |||
| 78 | Status waitTimeout(const Fw::TimeInterval& interval) override; //!< wait with timeout | ||
| 79 | |||
| 80 | Status tryWait() override; | ||
| 81 | |||
| 82 | Status post() override; | ||
| 83 | |||
| 84 | CountingSemaphoreHandle* getHandle() override; | ||
| 85 | |||
| 86 | private: | ||
| 87 | alignas(FW_HANDLE_ALIGNMENT) CountingSemaphoreHandleStorage m_handle_storage; | ||
| 88 | CountingSemaphoreInterface& m_delegate; | ||
| 89 | }; | ||
| 90 | } // namespace Os | ||
| 91 | #endif // OS_COUNTING_SEMAPHORE_HPP_ | ||
| 92 |