SuperTinyKernel™ RTOS 1.08.x
Lightweight, high-performance, deterministic, bare-metal C++ RTOS for resource-constrained embedded systems. MIT Open Source License.
Loading...
Searching...
No Matches
semaphore.h
Go to the documentation of this file.
1/*
2 * SuperTinyKernel(TM) RTOS: Lightweight High-Performance Deterministic C++ RTOS for Embedded Systems.
3 *
4 * Source: https://github.com/SuperTinyKernel-RTOS
5 *
6 * Copyright (c) 2022-2026 Neutron Code Limited <stk@neutroncode.com>. All Rights Reserved.
7 * License: MIT License, see LICENSE for a full text.
8 */
9
10#ifndef STK_C_SEMAPHORE_H_
11#define STK_C_SEMAPHORE_H_
12
31
32#ifdef __cplusplus
33 #include <cstdint>
34 #include <cstddef>
35 #include <ctime>
36#else
37 #include <stdint.h>
38 #include <stddef.h>
39 #include <time.h>
40#endif
41
42#include <stk_config.h>
43
44// must match the definition used by the C++ kernel API
45#ifndef STK_SYNC_DEBUG_NAMES
46 #if STK_SEGGER_SYSVIEW
47 #define STK_SYNC_DEBUG_NAMES (1)
48 #else
49 #define STK_SYNC_DEBUG_NAMES (0)
50 #endif
51#endif
52
53#ifndef CLOCK_REALTIME
54 #define CLOCK_REALTIME (0)
55#endif
56#ifndef CLOCK_MONOTONIC
57 #define CLOCK_MONOTONIC (1)
58#endif
59
60#ifdef __cplusplus
61extern "C" {
62#endif
63
67#define SEM_VALUE_MAX (2147483647)
68
74#define STK_C_SEM_WORDS (7U + (STK_SYNC_DEBUG_NAMES ? 1U : 0U) + \
75 ((2U * sizeof(uint32_t) + sizeof(uintptr_t) - 1U) / sizeof(uintptr_t)) + 1U)
76
77#if defined(__GNUC__) || defined(__clang__) || defined(__ICCARM__)
78 #define __stk_sem_aligned __attribute__((aligned(sizeof(uintptr_t))))
79#else
80 #define __stk_sem_aligned
81#endif
82
86typedef struct sem_t
87{
88 struct {
91 void *__handle;
93
100int sem_init(sem_t *sem, int pshared, unsigned int value);
101
106int sem_destroy(sem_t *sem);
107
112int sem_wait(sem_t *sem);
113
118int sem_trywait(sem_t *sem);
119
124int sem_timedwait(sem_t *sem, const struct timespec *abstime);
125
130int sem_clockwait(sem_t *sem, clockid_t clock_id, const struct timespec *abstime);
131
136int sem_post(sem_t *sem);
137
143int sem_getvalue(sem_t *sem, int *sval);
144
145#ifdef __cplusplus
146}
147#endif
148
149#endif /* STK_C_SEMAPHORE_H_ */
int sem_post(sem_t *sem)
Release a token (wakes the first waiter, otherwise increments the count).
#define __stk_sem_aligned
Definition semaphore.h:80
#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 name...
Definition semaphore.h:74
int sem_trywait(sem_t *sem)
Take a token without blocking.
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_getvalue(sem_t *sem, int *sval)
Get the current count.
int sem_timedwait(sem_t *sem, const struct timespec *abstime)
Take a token, waiting until the absolute deadline abstime.
int sem_wait(sem_t *sem)
Take a token, blocking until one is available.
int sem_clockwait(sem_t *sem, clockid_t clock_id, const struct timespec *abstime)
sem_timedwait() with an explicit clock (POSIX.1-2024).
POSIX time extensions for STK: clock_gettime(), clock_getres(), nanosleep(), clock_nanosleep(),...
A POSIX counting semaphore.
Definition semaphore.h:87
void * __handle
NULL until sem_init(), then points into __mem.
Definition semaphore.h:91
uintptr_t data[(7U+((0) ? 1U :0U)+((2U *sizeof(uint32_t)+sizeof(uintptr_t) - 1U)/sizeof(uintptr_t))+1U)]
Definition semaphore.h:89
struct sem_t::@301215357362074031213040000172073175337057334267 __mem
In-place storage of the C++ semaphore.