|
SuperTinyKernel™ RTOS 1.08.x
Lightweight, high-performance, deterministic, bare-metal C++ RTOS for resource-constrained embedded systems. MIT Open Source License.
|
POSIX-named counting semaphores (<semaphore.h>) for STK, backed by stk::sync::SemaphoreT. Implemented by stk_c_pthread.cpp (the same shim as <pthread.h>, no dependency on the STK C API); stk_pthread_bind_kernel() is not required, only a running kernel with KERNEL_SYNC. More...
#include <cstdint>#include <cstddef>#include <ctime>#include <stk_config.h>Go to the source code of this file.
Classes | |
| struct | sem_t |
| A POSIX counting semaphore. More... | |
Macros | |
| #define | STK_SYNC_DEBUG_NAMES (0) |
| #define | CLOCK_REALTIME (0) |
| #define | CLOCK_MONOTONIC (1) |
| #define | SEM_VALUE_MAX (2147483647) |
| Maximum value of a semaphore (the counter is 32-bit, POSIX requires at least 32767). | |
| #define | STK_C_SEM_WORDS |
| Size (in machine words) of the opaque storage of a sem_t: the C++ object (7 words + 1 with debug names) plus two 32-bit counters and alignment slack. Verified at compile time against sizeof() of the real object by stk_c_pthread.cpp. | |
| #define | __stk_sem_aligned |
Typedefs | |
| typedef struct sem_t | sem_t |
| A POSIX counting semaphore. | |
Functions | |
| int | sem_init (sem_t *sem, int pshared, unsigned int value) |
| Initialize an unnamed semaphore. | |
| int | sem_destroy (sem_t *sem) |
| Destroy a semaphore. | |
| int | sem_wait (sem_t *sem) |
| Take a token, blocking until one is available. | |
| int | sem_trywait (sem_t *sem) |
| Take a token without blocking. | |
| int | sem_timedwait (sem_t *sem, const struct timespec *abstime) |
| Take a token, waiting until the absolute deadline abstime. | |
| int | sem_clockwait (sem_t *sem, clockid_t clock_id, const struct timespec *abstime) |
| sem_timedwait() with an explicit clock (POSIX.1-2024). | |
| int | sem_post (sem_t *sem) |
| Release a token (wakes the first waiter, otherwise increments the count). | |
| int | sem_getvalue (sem_t *sem, int *sval) |
| Get the current count. | |
POSIX-named counting semaphores (<semaphore.h>) for STK, backed by stk::sync::SemaphoreT. Implemented by stk_c_pthread.cpp (the same shim as <pthread.h>, no dependency on the STK C API); stk_pthread_bind_kernel() is not required, only a running kernel with KERNEL_SYNC.
Unnamed semaphores only: sem_init/destroy/wait/trywait/timedwait/clockwait/post/getvalue. Named semaphores (sem_open/close/unlink) are not provided, STK has no named kernel objects.
Like POSIX, the functions return 0 on success and -1 on failure with errno set:
Definition in file semaphore.h.
| #define __stk_sem_aligned |
Definition at line 80 of file semaphore.h.
| #define CLOCK_MONOTONIC (1) |
Definition at line 57 of file semaphore.h.
Referenced by stk::pthread::IsSupportedClock().
| #define CLOCK_REALTIME (0) |
Definition at line 54 of file semaphore.h.
Referenced by stk::pthread::IsSupportedClock(), pthread_condattr_init(), and timespec_get().
| #define SEM_VALUE_MAX (2147483647) |
Maximum value of a semaphore (the counter is 32-bit, POSIX requires at least 32767).
Definition at line 67 of file semaphore.h.
Referenced by sem_init(), and sysconf().
| #define STK_C_SEM_WORDS |
Size (in machine words) of the opaque storage of a sem_t: the C++ object (7 words + 1 with debug names) plus two 32-bit counters and alignment slack. Verified at compile time against sizeof() of the real object by stk_c_pthread.cpp.
Definition at line 74 of file semaphore.h.
| #define STK_SYNC_DEBUG_NAMES (0) |
Definition at line 49 of file semaphore.h.
| typedef struct sem_t sem_t |
A POSIX counting semaphore.
| int sem_clockwait | ( | sem_t * | sem, |
| clockid_t | clock_id, | ||
| const struct timespec * | abstime ) |
sem_timedwait() with an explicit clock (POSIX.1-2024).
| [in] | clock_id | CLOCK_REALTIME or CLOCK_MONOTONIC, both refer to the same STK tick timeline. |
Definition at line 2930 of file stk_c_pthread.cpp.
References stk::pthread::IsSupportedClock(), sem_timedwait(), and SemFail().
| int sem_destroy | ( | sem_t * | sem | ) |
Destroy a semaphore.
Definition at line 2881 of file stk_c_pthread.cpp.
References sem_t::__handle, IsSemReady(), SemFail(), and SemOf().
| int sem_getvalue | ( | sem_t * | sem, |
| int * | sval ) |
Get the current count.
| [out] | sval | Receives the count (0 if tasks are waiting, STK does not report negative values). |
Definition at line 2952 of file stk_c_pthread.cpp.
References stk::sync::SemaphoreT< T >::GetCount(), IsSemReady(), SemFail(), and SemOf().
| int sem_init | ( | sem_t * | sem, |
| int | pshared, | ||
| unsigned int | value ) |
Initialize an unnamed semaphore.
| [in] | sem | Semaphore to initialize. |
| [in] | pshared | Must be 0 (process-shared semaphores are not supported). |
| [in] | value | Initial count, at most SEM_VALUE_MAX. |
Definition at line 2855 of file stk_c_pthread.cpp.
References sem_t::__handle, sem_t::__mem, stk::ConstructIn(), SEM_VALUE_MAX, and SemFail().
| int sem_post | ( | sem_t * | sem | ) |
Release a token (wakes the first waiter, otherwise increments the count).
Definition at line 2935 of file stk_c_pthread.cpp.
References IsSemReady(), SemFail(), SemOf(), and stk::sync::SemaphoreT< T >::TrySignal().
| int sem_timedwait | ( | sem_t * | sem, |
| const struct timespec * | abstime ) |
Take a token, waiting until the absolute deadline abstime.
Definition at line 2911 of file stk_c_pthread.cpp.
References IsSemReady(), stk::pthread::IsValidTimespec(), SemFail(), SemOf(), SemWaitResult(), stk::pthread::TestCancelPoint(), and stk::pthread::TimespecToRelativeTimeout().
Referenced by sem_clockwait().
| int sem_trywait | ( | sem_t * | sem | ) |
Take a token without blocking.
Definition at line 2906 of file stk_c_pthread.cpp.
References IsSemReady(), stk::NO_WAIT, SemFail(), SemOf(), and SemWaitResult().
| int sem_wait | ( | sem_t * | sem | ) |
Take a token, blocking until one is available.
Definition at line 2899 of file stk_c_pthread.cpp.
References IsSemReady(), SemFail(), SemOf(), SemWaitResult(), stk::pthread::TestCancelPoint(), and stk::WAIT_INFINITE.