Counting semaphores; a binary semaphore has limit 1.
More...
Counting semaphores; a binary semaphore has limit 1.
A give hands the token directly to the highest priority waiter, so a woken taker never finds the count stolen. Both operations may be called from ISRs; an ISR never blocks and gets -EBUSY when no token is available.
Signalling a thread from an interrupt:
static void prv_dma_isr(void) {
}
int transfer(void) {
prv_dma_start();
prv_dma_abort();
return -ETIMEDOUT;
}
return 0;
}
void pbl_sem_give(struct pbl_sem *s)
Give a token.
#define PBL_SEM_DEFINE(name, initial, limit)
Define a semaphore, usable without pbl_sem_init().
Definition sem.h:64
int pbl_sem_take(struct pbl_sem *s, pbl_timeout_t timeout)
Take a token.
#define PBL_MSEC(ms)
Timeout in milliseconds, rounded down to whole ticks.
Definition types.h:62
◆ pbl_sem
Counting semaphore.
Usable from ISRs with PBL_NO_WAIT.
| Data Fields |
|
struct pbl_sem_backend |
backend |
Backend state, including the current count. |
|
uint32_t |
initial |
Count restored by pbl_sem_reset(). |
|
uint32_t |
limit |
Maximum count; gives beyond it are dropped. |
◆ PBL_SEM_DEFINE
Define a semaphore, usable without pbl_sem_init().
- Parameters
-
| name | Name of the semaphore variable. |
| initial | Initial count. |
| limit | Maximum count. |
◆ PBL_SEM_INITIALIZER
| #define PBL_SEM_INITIALIZER |
( |
|
init, |
|
|
|
lim |
|
) |
| {.initial = (init), .limit = (lim), .backend = PBL_SEM_BACKEND_INITIALIZER(init)} |
Static initializer for a semaphore.
- Parameters
-
| init | Initial count. |
| lim | Maximum count. |
◆ pbl_sem_count()
| uint32_t pbl_sem_count |
( |
const struct pbl_sem * |
s | ) |
|
Get the current count.
- Parameters
-
- Returns
- Tokens available.
◆ pbl_sem_deinit()
| void pbl_sem_deinit |
( |
struct pbl_sem * |
s | ) |
|
Release a semaphore before its memory is reused.
Required for dynamically allocated semaphores.
- Parameters
-
◆ pbl_sem_give()
| void pbl_sem_give |
( |
struct pbl_sem * |
s | ) |
|
Give a token.
Wakes the highest priority waiter, or increments the count up to the limit. ISR-safe.
- Parameters
-
◆ pbl_sem_init()
| void pbl_sem_init |
( |
struct pbl_sem * |
s, |
|
|
uint32_t |
initial, |
|
|
uint32_t |
limit |
|
) |
| |
Initialize a semaphore in dynamically allocated memory.
- Parameters
-
| [out] | s | Semaphore. |
| initial | Initial count, at most limit. |
| limit | Maximum count, at least 1. |
◆ pbl_sem_reset()
| void pbl_sem_reset |
( |
struct pbl_sem * |
s | ) |
|
Set the count back to its initial value.
Waiters are not woken.
- Parameters
-
◆ pbl_sem_take()
Take a token.
- Parameters
-
| s | Semaphore. |
| timeout | How long to wait for a token; ignored in an ISR, which never waits. |
- Return values
-
| 0 | Token taken. |
| -EAGAIN | Timed out. |
| -EBUSY | No token and timeout is PBL_NO_WAIT, or called from an ISR. |
| -EINTR | The thread was suspended while waiting. |