10#ifndef STK_SYNC_SEMAPHORE_H_
11#define STK_SYNC_SEMAPHORE_H_
73 STK_STATIC_ASSERT_DESC(
static_cast<T
>(-1) >
static_cast<T
>(0),
"SemaphoreT: T must be an unsigned integer type");
82 static constexpr T
COUNT_MAX =
static_cast<T
>(~static_cast<T>(0U));
212#if (__cplusplus < 201703L)
245 else if (timeout_ticks !=
NO_WAIT)
#define STK_UNUSED(X)
Explicitly marks a variable as unused to suppress compiler warnings.
#define __stk_forceinline
Forces compiler to always inline the decorated function, regardless of optimisation level.
#define STK_NONCOPYABLE_CLASS(TYPE)
Deletes (= delete) the copy constructor and copy assignment operator of a class.
#define STK_ASSERT(e)
Runtime assertion. Halts execution if the expression e evaluates to false.
#define STK_STATIC_ASSERT_DESC(X, DESC)
Compile-time assertion with a custom error description. Produces a compilation error if X is false.
#define STK_VIRT_DTOR
Expands to nothing if STK_STRICT_COMPLIANCY=0 (non-virtual destructors, smaller objects),...
Implementation of synchronization primitive: stk::sync::ScopedCriticalSection.
Namespace of STK package.
static constexpr Timeout NO_WAIT
Timeout value: return immediately if the synchronization object is not yet signaled (non-blocking pol...
EWaitResult
Wait result (see IKernelService::Wait).
@ WAIT_RESULT_TIMEOUT
The wake was caused by a timeout expiry.
@ WAIT_RESULT_SIGNAL
The wake was caused by a signal.
int32_t Timeout
Timeout time (ticks).
static constexpr Timeout WAIT_INFINITE
Timeout value: block indefinitely until the synchronization object is signaled.
bool IsInsideISR()
Check whether the CPU is currently executing inside a hardware interrupt service routine (ISR).
Synchronization primitives for task coordination and resource protection.
SemaphoreT< uint16_t > Semaphore
Default counting semaphore with a 16-bit counter (max count 0xFFFF).
static IKernelService * GetInstance()
Get CPU-local instance of the kernel service.
virtual EWaitResult Wait(ISyncObject *sobj, IMutex *mutex, Timeout timeout)=0
Put calling process into a waiting state until synchronization object is signaled or timeout occurs.
SyncObjectBase()
Constructor.
IWaitObject::ListHeadType m_wait_list
void WakeOne() override
Wake the first task in the wait list (FIFO order).
RAII-style low-level synchronization primitive for atomic code execution. Used as building brick for ...
Counting semaphore primitive for resource management and signaling, with a selectable counter type.
static constexpr uint16_t COUNT_MAX
bool TrySignalLimited(const T limit)
Common signal path: hand off to a waiter or increment up to limit.
T CounterValueType
Counter value type.
T GetCount() const
Get current counter value.
T ReadCount(ReadTag< false >) const
Counter fits the native word: a single load is atomic.
bool Wait(Timeout timeout_ticks=WAIT_INFINITE)
Wait for a signal (decrement counter).
bool TrySignal()
Try to post a signal without exceeding max_count.
T ReadCount(ReadTag< true >) const
Counter is wider than the native word: a plain load may be split into several, guard it.
bool TrySignal(const T ceiling)
Try to post a signal without exceeding a caller-supplied ceiling.
void Signal()
Post a signal (increment counter).
bool TryWait()
Poll the semaphore without blocking (decrement counter if available).
EWaitResult WaitEx(Timeout timeout_ticks=WAIT_INFINITE)
Wait for a signal (decrement counter), preserving the full wait outcome.
SemaphoreT(T initial_count=0U, T max_count=COUNT_MAX)
Constructor.
Selects how the counter is read: WIDE is true if T is wider than a pointer.