VSF Documented
task.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 task.h shim for VSF.
19//
20// Scope of the initial MVP:
21// - vTaskDelay / vTaskDelayUntil (cooperative sleep via vsf_thread)
22// - xTaskGetTickCount (vsf_systimer_get_ms)
23// - taskYIELD (vsf_thread_yield)
24// - xTaskCreate / xTaskCreateStatic (spawn a vsf_thread worker)
25// - vTaskDelete (self-termination / best-effort)
26//
27// Suspend/Resume/Notify and the full task API surface are deferred; they
28// will be layered on top of the same vsf_thread worker model.
29
30#ifndef __VSF_FREERTOS_TASK_H__
31#define __VSF_FREERTOS_TASK_H__
32
33#include "FreeRTOS.h"
34
35#if defined(__VSF_FREERTOS_TASK_CLASS_IMPLEMENT)
36# undef __VSF_FREERTOS_TASK_CLASS_IMPLEMENT
37# define __VSF_CLASS_IMPLEMENT__
38#endif
39#include "utilities/ooc_class.h"
40
41#ifdef __cplusplus
42extern "C" {
43#endif
44
45/*============================ TYPES =========================================*/
46
49typedef void (*TaskFunction_t)(void *);
50
51// Conditional field helpers. vsf_class(...) { private_member(...) } is a
52// single macro invocation, so we cannot place #if inside its argument list
53// portably; these stage the optional members into empty / non-empty text.
54#if VSF_KERNEL_CFG_EDA_SUPPORT_SUB_CALL == ENABLED
55# define __VSF_FREERTOS_TASK_THREAD_CB_FIELD vsf_thread_cb_t thread_cb;
56#else
57# define __VSF_FREERTOS_TASK_THREAD_CB_FIELD
58#endif
59#if VSF_FREERTOS_CFG_USE_NOTIFY == ENABLED
60# define __VSF_FREERTOS_TASK_NOTIFY_FIELDS \
61 vsf_sem_t notify_sem; \
62 uint32_t notify_value; \
63 bool notify_pending;
64#else
65# define __VSF_FREERTOS_TASK_NOTIFY_FIELDS
66#endif
67
68// Task control block exposed to both the task port and the notify port.
69// The vsf_thread_t MUST remain the FIRST member: xTaskGetCurrentTaskHandle
70// returns (TaskHandle_t)vsf_thread_get_cur() and the notify port casts
71// that back to StaticTask_t * -- the offset-0 upcast is what makes this
72// round-trip sound.
74 private_member(
75 vsf_thread_t thread;
77 void (*entry)(void *);
78 void * arg;
79 void * stack;
80 uint32_t stack_bytes;
81 // Ownership flags for zero-heap xTaskCreateStatic: when set, the
82 // corresponding storage was supplied by the caller and must NOT
83 // be released to the heap.
84 bool is_static;
85 bool is_stack_static;
87 )
88};
89
90/*============================ API ===========================================*/
91
92// Delay the calling task by xTicksToDelay ticks. In this shim, 1 tick equals
93// 1 ms (see pdMS_TO_TICKS); the underlying sleep uses vsf_thread_delay.
94// MUST be called from a vsf_thread context.
95extern void vTaskDelay(const TickType_t xTicksToDelay);
96
97// Absolute-deadline variant. pxPreviousWakeTime is updated on return.
98extern void vTaskDelayUntil(TickType_t * const pxPreviousWakeTime,
99 const TickType_t xTimeIncrement);
100
101// Returns the current kernel tick count. Mapped to vsf_systimer milliseconds
102// to keep pdMS_TO_TICKS(ms) semantics consistent.
103extern TickType_t xTaskGetTickCount(void);
105
106// Yield the CPU to any equal-priority ready task.
107#define taskYIELD() vTaskYield()
108extern void vTaskYield(void);
109
110// Creates a FreeRTOS task backed by a vsf_thread worker. pvParameters is
111// passed to pxTaskCode. usStackDepth is interpreted as bytes (see StackType_t
112// typedef). uxPriority is mapped to VSF_FREERTOS_CFG_DEFAULT_VSF_PRIO in the
113// MVP; board overrides can refine the mapping.
114// Returns pdPASS on success, pdFAIL on error.
115extern BaseType_t xTaskCreate(TaskFunction_t pxTaskCode,
116 const char * const pcName,
117 const uint32_t usStackDepth,
118 void * const pvParameters,
119 UBaseType_t uxPriority,
120 TaskHandle_t * const pxCreatedTask);
121
122// Deletes a task. xTaskToDelete == NULL means self-delete.
123// NOTE: self-delete from inside the task body is the only supported form in
124// the MVP; deleting another task is best-effort and may leak the stack.
125extern void vTaskDelete(TaskHandle_t xTaskToDelete);
126
127// Static allocation variant. The caller supplies BOTH the stack buffer
128// (ulStackDepth bytes -- the shim defines StackType_t as uint8_t so the
129// unit is always bytes) and the task control block storage. Neither is
130// ever passed to the heap; vTaskDelete(NULL) self-exits the thread but
131// the caller-owned storage is NOT freed.
132//
133// Requirements on the caller:
134// - puxStackBuffer must be 8-byte aligned (StackType_t is uint8_t so
135// this is the caller's responsibility on declaration).
136// - ulStackDepth must cover the VSF page + guardian size minimum.
137// Returns the task handle on success, NULL on argument errors.
139 const char * const pcName,
140 const uint32_t ulStackDepth,
141 void * const pvParameters,
142 UBaseType_t uxPriority,
143 StackType_t * const puxStackBuffer,
144 StaticTask_t * const pxTaskBuffer);
145
146// Returns the handle of the calling task, or NULL if called from outside a
147// vsf_thread context.
149
150/*============================ SCHEDULER / CRITICAL ==========================*/
151
152// Disable / re-enable preemptive task switching. Unlike the critical
153// section pair, these do NOT mask interrupts; they only prevent the
154// scheduler from picking a different task until the matching resume.
155// Nesting is permitted; only the outermost pair actually engages the
156// underlying vsf_sched_lock.
157//
158// xTaskResumeAll returns pdTRUE if a context switch was forced on the
159// way out and pdFALSE otherwise. The shim always returns pdFALSE --
160// callers that drive their own yield based on the return value will
161// still work because the very next cooperative yield point will pick
162// up any newly-ready tasks.
163extern void vTaskSuspendAll(void);
164extern BaseType_t xTaskResumeAll(void);
165
166// Scheduler-level critical section. Nestable. The FromISR variants
167// return / accept the saved scheduler state so that the caller can
168// keep it on the local stack -- matching FreeRTOS semantics on
169// architectures where the state is a pushed PRIMASK.
170extern void vTaskEnterCritical(void);
171extern void vTaskExitCritical(void);
173extern void vTaskExitCriticalFromISR(UBaseType_t uxSavedInterruptState);
174
175/*============================ NOTIFICATIONS =================================*/
176
177// FreeRTOS task notifications. The shim implements a single-slot model:
178// every task owns a (sem, value, pending) triple and xTaskNotify updates
179// value according to eNotifyAction before signalling the sem.
180
181typedef enum {
188
189// Update a task's notification value and wake it. Returns pdPASS, or
190// pdFAIL when eSetValueWithoutOverwrite is used on a task that already
191// has a pending unclaimed notification.
192extern BaseType_t xTaskNotify(TaskHandle_t xTaskToNotify,
193 uint32_t ulValue,
194 eNotifyAction eAction);
195
196// ISR-context variant. pxHigherPriorityTaskWoken is informational.
197extern BaseType_t xTaskNotifyFromISR(TaskHandle_t xTaskToNotify,
198 uint32_t ulValue,
199 eNotifyAction eAction,
200 BaseType_t *pxHigherPriorityTaskWoken);
201
202// Convenience shortcuts: xTaskNotify(task, 0, eIncrement).
203extern BaseType_t xTaskNotifyGive(TaskHandle_t xTaskToNotify);
204extern void vTaskNotifyGiveFromISR(TaskHandle_t xTaskToNotify,
205 BaseType_t *pxHigherPriorityTaskWoken);
206
207// Block the calling task until a notification arrives. Returns the value
208// of the notification at the point the wait completed; if
209// xClearCountOnExit == pdTRUE the value is zeroed on successful exit,
210// otherwise it is decremented by one (counting semaphore semantics).
211extern uint32_t ulTaskNotifyTake(BaseType_t xClearCountOnExit,
212 TickType_t xTicksToWait);
213
214// Block with bitmask semantics. On entry the bits in
215// ulBitsToClearOnEntry are cleared; on successful return the bits in
216// ulBitsToClearOnExit are cleared. pulNotificationValue receives the
217// value observed at wait completion (may be NULL).
218// Returns pdPASS on success, pdFAIL on timeout / invalid context.
219extern BaseType_t xTaskNotifyWait(uint32_t ulBitsToClearOnEntry,
220 uint32_t ulBitsToClearOnExit,
221 uint32_t *pulNotificationValue,
222 TickType_t xTicksToWait);
223
224#ifdef __cplusplus
225}
226#endif
227
228#endif // __VSF_FREERTOS_TASK_H__
Definition task.h:73
long BaseType_t
Definition esp_ringbuf.h:50
#define vsf_dcl_class
Definition ooc_class.h:50
#define vsf_class(__name)
Definition ooc_class.h:52
uint8_t StackType_t
Definition portmacro.h:53
uint32_t TickType_t
Definition rtos_al.h:59
uint32_t UBaseType_t
Definition rtos_al.h:60
unsigned uint32_t
Definition stdint.h:9
void vTaskSuspendAll(void)
Definition freertos_critical_port.c:158
void vTaskNotifyGiveFromISR(TaskHandle_t xTaskToNotify, BaseType_t *pxHigherPriorityTaskWoken)
Definition freertos_notify_port.c:180
BaseType_t xTaskCreate(TaskFunction_t pxTaskCode, const char *const pcName, const uint32_t usStackDepth, void *const pvParameters, UBaseType_t uxPriority, TaskHandle_t *const pxCreatedTask)
Definition freertos_task_port.c:176
uint32_t ulTaskNotifyTake(BaseType_t xClearCountOnExit, TickType_t xTicksToWait)
Definition freertos_notify_port.c:187
UBaseType_t vTaskEnterCriticalFromISR(void)
Definition freertos_critical_port.c:134
TaskHandle_t xTaskGetCurrentTaskHandle(void)
Definition freertos_task_port.c:292
BaseType_t xTaskNotifyFromISR(TaskHandle_t xTaskToNotify, uint32_t ulValue, eNotifyAction eAction, BaseType_t *pxHigherPriorityTaskWoken)
Definition freertos_notify_port.c:147
TickType_t xTaskGetTickCount(void)
Definition freertos_task_port.c:113
BaseType_t xTaskNotifyWait(uint32_t ulBitsToClearOnEntry, uint32_t ulBitsToClearOnExit, uint32_t *pulNotificationValue, TickType_t xTicksToWait)
Definition freertos_notify_port.c:236
BaseType_t xTaskNotifyGive(TaskHandle_t xTaskToNotify)
Definition freertos_notify_port.c:175
void vTaskDelay(const TickType_t xTicksToDelay)
Definition freertos_task_port.c:91
void(* TaskFunction_t)(void *)
Definition task.h:49
void vTaskExitCritical(void)
Definition freertos_critical_port.c:95
void vTaskDelayUntil(TickType_t *const pxPreviousWakeTime, const TickType_t xTimeIncrement)
Definition freertos_task_port.c:97
void vTaskYield(void)
Definition freertos_task_port.c:123
TickType_t xTaskGetTickCountFromISR(void)
Definition freertos_task_port.c:118
BaseType_t xTaskResumeAll(void)
Definition freertos_critical_port.c:167
eNotifyAction
Definition task.h:181
@ eIncrement
Definition task.h:184
@ eSetValueWithOverwrite
Definition task.h:185
@ eSetBits
Definition task.h:183
@ eNoAction
Definition task.h:182
@ eSetValueWithoutOverwrite
Definition task.h:186
#define __VSF_FREERTOS_TASK_NOTIFY_FIELDS
Definition task.h:60
TaskHandle_t xTaskCreateStatic(TaskFunction_t pxTaskCode, const char *const pcName, const uint32_t ulStackDepth, void *const pvParameters, UBaseType_t uxPriority, StackType_t *const puxStackBuffer, StaticTask_t *const pxTaskBuffer)
Definition freertos_task_port.c:225
void vTaskExitCriticalFromISR(UBaseType_t uxSavedInterruptState)
Definition freertos_critical_port.c:139
void vTaskEnterCritical(void)
Definition freertos_critical_port.c:84
BaseType_t xTaskNotify(TaskHandle_t xTaskToNotify, uint32_t ulValue, eNotifyAction eAction)
Definition freertos_notify_port.c:127
void vTaskDelete(TaskHandle_t xTaskToDelete)
Definition freertos_task_port.c:280
#define __VSF_FREERTOS_TASK_THREAD_CB_FIELD
Definition task.h:55
Generated from commit: vsfteam/vsf@015f4d1