Counting Semaphore of the QXK preemptive kernel. More...
#include <qxk.hpp>
Public Member Functions | |
| void | init (std::uint_fast8_t const count, std::uint_fast8_t const max_count=0xFFU) noexcept |
| Initialize the counting semaphore. | |
| bool | wait (QTimeEvtCtr const nTicks=QXTHREAD_NO_TIMEOUT) noexcept |
| Wait (block) on the semaphore. | |
| bool | tryWait () noexcept |
| Try wait on the semaphore (non-blocking). | |
| bool | signal () noexcept |
| Signal (unblock) the semaphore. | |
Private Attributes | |
| QPSet | m_waitSet |
| Set of extended threads waiting on this semaphore. | |
| std::uint8_t | m_count |
| Semaphore up-down counter. | |
| std::uint8_t | m_max_count |
| Maximum value of the semaphore counter (1 for binary semaphore). | |
Friends | |
| class | QS |
Counting Semaphore of the QXK preemptive kernel.
Details
QP::QXSemaphore is a blocking mechanism intended primarily for signaling extended threads. The semaphore is initialized with the maximum count (see QXSemaphore::init()), which allows you to create a binary semaphore (when the maximum count is 1) and counting semaphore when the maximum count is > 1.
Usage
The following example illustrates how to instantiate and use the semaphore in your application.
|
noexcept |
Initialize the counting semaphore.
Details
Initializes a semaphore with the specified count and maximum count. If the semaphore is used for resource sharing, both the initial count and maximum count should be set to the number of identical resources guarded by the semaphore. If the semaphore is used as a signaling mechanism, the initial count should set to 0 and maximum count to 1 (binary semaphore).
| [in] | count | initial value of the semaphore counter |
| [in] | max_count | maximum value of the semaphore counter. The purpose of the max_count is to limit the counter so that the semaphore cannot unblock more times than the maximum. |
Usage
The following example illustrates how to instantiate and use the semaphore in your application.
Definition at line 52 of file qxk_sema.cpp.
|
noexcept |
Wait (block) on the semaphore.
Details
When an extended thread calls QXSemaphore::wait() and the value of the semaphore counter is greater than 0, QXSemaphore::wait() decrements the semaphore counter and returns (true) to its caller. However, if the value of the semaphore counter is 0, the function places the calling thread in the waiting list for the semaphore. The thread waits until the semaphore is signaled by calling QXSemaphore_signal(), or the specified timeout expires. If the semaphore is signaled before the timeout expires, QXK resumes the highest-priority extended thread waiting for the semaphore.
| [in] | nTicks | number of clock ticks (at the associated rate) to wait for the semaphore. The value of ::QXTHREAD_NO_TIMEOUT indicates that no timeout will occur and the semaphore will wait indefinitely. |
Usage
The following example illustrates how to instantiate and use the semaphore in your application.
Definition at line 73 of file qxk_sema.cpp.
|
noexcept |
Try wait on the semaphore (non-blocking).
Details
This function checks if the semaphore counter is greater than 0, in which case the counter is decremented.
Definition at line 152 of file qxk_sema.cpp.
|
noexcept |
Signal (unblock) the semaphore.
Details
If the semaphore counter value is 0 or more, it is incremented, and this function returns to its caller. If the extended threads are waiting for the semaphore to be signaled, QXSemaphore_signal() removes the highest-priority thread waiting for the semaphore from the waiting list and makes this thread ready-to-run. The QXK scheduler is then called to determine if the awakened thread is now the highest-priority thread that is ready-to-run.
Definition at line 186 of file qxk_sema.cpp.
|
private |
|
private |
|
private |