VSF Documented
semphr.h
Go to the documentation of this file.
1/*****************************************************************************
2 * Copyright(C)2009-2026 by VSF Team *
3 * *
4 * Licensed under the Apache License, Version 2.0 (the "License"); *
5 * you may not use this file except in compliance with the License. *
6 * You may obtain a copy of the License at *
7 * *
8 * http://www.apache.org/licenses/LICENSE-2.0 *
9 * *
10 * Unless required by applicable law or agreed to in writing, software *
11 * distributed under the License is distributed on an "AS IS" BASIS, *
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13 * See the License for the specific language governing permissions and *
14 * limitations under the License. *
15 * *
16 ****************************************************************************/
17
18// Clean-room FreeRTOS semphr.h shim for VSF.
19//
20// Subset covered (MVP aligned with ESP-IDF v5.x and common middleware):
21// xSemaphoreCreateBinary -> vsf_sem_t (init=0, max=1)
22// xSemaphoreCreateCounting -> vsf_sem_t (init=uxInitial, max=uxMax)
23// xSemaphoreCreateMutex -> vsf_mutex_t
24// xSemaphoreTake -> vsf_thread_sem_pend / mutex_enter
25// xSemaphoreGive -> vsf_eda_sem_post / mutex_leave
26// xSemaphoreTakeFromISR -> not really supported; parameter guard
27// xSemaphoreGiveFromISR -> vsf_eda_sem_post_isr
28// vSemaphoreDelete -> vsf_heap_free
29//
30// Not covered yet: recursive mutex, StaticSemaphore variants, queue-set.
31// The handle is polymorphic; an internal "kind" discriminator selects
32// between sem and mutex backing stores.
33
34#ifndef __VSF_FREERTOS_SEMPHR_H__
35#define __VSF_FREERTOS_SEMPHR_H__
36
37#include "FreeRTOS.h"
38
39// PLOOC vsf_class hook: the shim .c file that implements this module
40// defines __VSF_FREERTOS_SEMPHR_CLASS_IMPLEMENT before including this
41// header to see the real field layout. External (user) consumers see
42// StaticSemaphore_t as a correctly-sized but opaque byte blob.
43#if defined(__VSF_FREERTOS_SEMPHR_CLASS_IMPLEMENT)
44# undef __VSF_FREERTOS_SEMPHR_CLASS_IMPLEMENT
45# define __VSF_CLASS_IMPLEMENT__
46#endif
47
48#include "utilities/ooc_class.h"
49
50#ifdef __cplusplus
51extern "C" {
52#endif
53
54/*============================ TYPES =========================================*/
55
56// Static-create storage. PLOOC private_member() reserves exactly
57// sizeof(struct{...}) bytes for external viewers so the size stays in
58// sync with the internal layout without a magic number. Deviation from
59// upstream FreeRTOS: we do NOT alias StaticSemaphore_t onto StaticQueue_t
60// because the shim uses distinct backing types for the two primitives.
63
65 private_member(
66 uint8_t kind; // __FRT_SEM_KIND_SEM / _MUTEX
67 bool is_static; // caller-owned storage flag
68 union {
69 vsf_sem_t sem; // binary / counting
70 vsf_mutex_t mutex; // mutex
71 } u;
72 )
73};
74
75/*============================ PROTOTYPES ====================================*/
76
77// Binary semaphore. Initial count is 0; the first Give raises it to 1 and
78// subsequent Gives are clamped. Mirrors FreeRTOS semantics.
80
81// Counting semaphore. uxMaxCount is the ceiling; uxInitialCount is the
82// initial availability. NULL is returned on invalid args / heap exhaustion.
84 UBaseType_t uxInitialCount);
85
86// Non-recursive mutex with priority inheritance (inherited from vsf_mutex_t).
88
89// Zero-heap variants. The caller owns the storage; vSemaphoreDelete
90// will NOT release the buffer. Returns NULL on invalid args.
92 StaticSemaphore_t *pxSemaphoreBuffer);
94 UBaseType_t uxMaxCount,
95 UBaseType_t uxInitialCount,
96 StaticSemaphore_t *pxSemaphoreBuffer);
98 StaticSemaphore_t *pxSemaphoreBuffer);
99
100// Free a semaphore / mutex. Waiters are cancelled before the storage is
101// released. vSemaphoreDelete(NULL) is a no-op.
102extern void vSemaphoreDelete(SemaphoreHandle_t xSemaphore);
103
104// Acquire. xTicksToWait == 0 means non-blocking; portMAX_DELAY means wait
105// forever. Returns pdTRUE on success / pdFALSE on timeout or invalid args.
107 TickType_t xTicksToWait);
108
109// Release. For a binary semaphore the count is clamped to 1.
110// Returns pdTRUE on success; pdFALSE on invalid args or (for a mutex) when
111// the caller does not own the lock.
113
114// ISR-context give. Mirrors xQueueSendFromISR semantics: the
115// pxHigherPriorityTaskWoken output is best-effort informational.
117 BaseType_t *pxHigherPriorityTaskWoken);
118
119// ISR-context take. Only the parameter-validation path is supported; the
120// FreeRTOS contract calls for this to run inside an actual interrupt
121// handler, which cannot block on a vsf_sync. Returns pdFAIL with
122// pxHigherPriorityTaskWoken set to pdFALSE. Provided for API compatibility.
124 BaseType_t *pxHigherPriorityTaskWoken);
125
126#ifdef __cplusplus
127}
128#endif
129
130#endif // __VSF_FREERTOS_SEMPHR_H__
Definition semphr.h:64
Definition vsf_eda.h:895
Definition vsf_eda.h:867
long BaseType_t
Definition esp_ringbuf.h:50
union @1078::@1090::@1091 u
#define vsf_dcl_class
Definition ooc_class.h:50
#define vsf_class(__name)
Definition ooc_class.h:52
uint32_t TickType_t
Definition rtos_al.h:59
uint32_t UBaseType_t
Definition rtos_al.h:60
BaseType_t xSemaphoreTakeFromISR(SemaphoreHandle_t xSemaphore, BaseType_t *pxHigherPriorityTaskWoken)
Definition freertos_semphr_port.c:248
SemaphoreHandle_t xSemaphoreCreateBinary(void)
Definition freertos_semphr_port.c:96
SemaphoreHandle_t xSemaphoreCreateCountingStatic(UBaseType_t uxMaxCount, UBaseType_t uxInitialCount, StaticSemaphore_t *pxSemaphoreBuffer)
Definition freertos_semphr_port.c:137
SemaphoreHandle_t xSemaphoreCreateBinaryStatic(StaticSemaphore_t *pxSemaphoreBuffer)
Definition freertos_semphr_port.c:127
SemaphoreHandle_t xSemaphoreCreateMutex(void)
Definition freertos_semphr_port.c:114
SemaphoreHandle_t xSemaphoreCreateCounting(UBaseType_t uxMaxCount, UBaseType_t uxInitialCount)
Definition freertos_semphr_port.c:102
BaseType_t xSemaphoreGive(SemaphoreHandle_t xSemaphore)
Definition freertos_semphr_port.c:198
BaseType_t xSemaphoreGiveFromISR(SemaphoreHandle_t xSemaphore, BaseType_t *pxHigherPriorityTaskWoken)
Definition freertos_semphr_port.c:222
BaseType_t xSemaphoreTake(SemaphoreHandle_t xSemaphore, TickType_t xTicksToWait)
Definition freertos_semphr_port.c:177
void vSemaphoreDelete(SemaphoreHandle_t xSemaphore)
Definition freertos_semphr_port.c:161
SemaphoreHandle_t xSemaphoreCreateMutexStatic(StaticSemaphore_t *pxSemaphoreBuffer)
Definition freertos_semphr_port.c:151
unsigned char uint8_t
Definition stdint.h:5
Definition mutex.h:10
Generated from commit: vsfteam/vsf@015f4d1