Counting Semaphore of the QXK preemptive kernel. More...
#include "qxk.h"
Public Member Functions | |
void | QXSemaphore_init (QXSemaphore *const me, uint_fast8_t const count, uint_fast8_t const max_count) |
bool | QXSemaphore_wait (QXSemaphore *const me, QTimeEvtCtr const nTicks) |
bool | QXSemaphore_tryWait (QXSemaphore *const me) |
bool | QXSemaphore_signal (QXSemaphore *const me) |
Private Attributes | |
QPSet | waitSet |
uint8_t volatile | count |
uint8_t | max_count |
Counting Semaphore of the QXK preemptive kernel.
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.
The following example illustrates how to instantiate and use the semaphore in your application.
void QXSemaphore_init | ( | QXSemaphore *const | me, |
uint_fast8_t const | count, | ||
uint_fast8_t const | max_count ) |
Initialize the counting semaphore
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,out] | me | current instance pointer (see oop) |
[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. |
qxk_sema:100
bool QXSemaphore_wait | ( | QXSemaphore *const | me, |
QTimeEvtCtr const | nTicks ) |
Wait (block) on the semaphore
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,out] | me | current instance pointer (see oop) |
[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. |
qxk_sema:200
qxk_sema:201
Definition at line 94 of file qxk_sema.c.
bool QXSemaphore_tryWait | ( | QXSemaphore *const | me | ) |
Try wait on the semaphore (non-blocking)
This function checks if the semaphore counter is greater than 0, in which case the counter is decremented.
[in,out] | me | current instance pointer (see oop) |
qxk_sema:300
Definition at line 181 of file qxk_sema.c.
bool QXSemaphore_signal | ( | QXSemaphore *const | me | ) |
Signal (unblock) the semaphore
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.
[in,out] | me | current instance pointer (see oop) |
qxk_sema:400
Definition at line 223 of file qxk_sema.c.
|
private |
|
private |