ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
static_task.cpp
Go to the documentation of this file.
2
3#ifdef USE_ESP32
4
6
7namespace esphome {
8
9bool StaticTask::create(TaskFunction_t fn, const char *name, uint32_t stack_size, void *param, UBaseType_t priority,
10 bool use_psram, BaseType_t core_id) {
11 if (this->handle_ != nullptr) {
12 // Task is already created; must call destroy() first
13 return false;
14 }
15
16 if (this->stack_buffer_ != nullptr && (stack_size > this->stack_size_ || use_psram != this->use_psram_)) {
17 // Existing buffer is too small or wrong memory type; free it to reallocate below
18 this->stack_buffer_.reset();
19 }
20
21 if (this->stack_buffer_ == nullptr) {
22 this->stack_size_ = stack_size;
23 this->use_psram_ = use_psram;
26 this->stack_buffer_ = allocator.make_unique_array_for_overwrite(stack_size);
27 }
28 if (this->stack_buffer_ == nullptr) {
29 return false;
30 }
31
32 if (this->tcb_ == nullptr) {
34 this->tcb_ = allocator.make_unique();
35 }
36 if (this->tcb_ == nullptr) {
37 this->deallocate();
38 return false;
39 }
40
41 this->handle_ = xTaskCreateStaticPinnedToCore(fn, name, this->stack_size_, param, priority, this->stack_buffer_.get(),
42 this->tcb_.get(), core_id);
43 if (this->handle_ == nullptr) {
44 this->deallocate();
45 return false;
46 }
47 return true;
48}
49
51 if (this->handle_ == nullptr) {
52 return true;
53 }
54
55 // Suspending takes the task off the ready and event lists, so nothing can schedule it again. It only asks
56 // the other core to yield though, so the task may still be running on it for a moment.
57 vTaskSuspend(this->handle_);
58 if (eTaskGetState(this->handle_) != eSuspended) {
59 // The task is still running on the other core and using its stack. Deleting it now would only put it on
60 // the termination list and return, so the caller has to try again once it has been swapped out.
61 return false;
62 }
63
64 // The task cannot run again, so the delete completes right away instead of being left to the idle task.
65 TaskHandle_t handle = this->handle_;
66 this->handle_ = nullptr;
67 vTaskDelete(handle);
68 return true;
69}
70
72 if (!this->destroy()) {
73 return false;
74 }
75 this->stack_buffer_.reset();
76 this->stack_size_ = 0;
77 this->tcb_.reset();
78 return true;
79}
80
81} // namespace esphome
82
83#endif // USE_ESP32
An STL allocator that uses SPI or internal RAM.
Definition helpers.h:2222
RAMUniquePtr< T > make_unique(Args &&...args)
Value initialize one T; empty on exhaustion.
Definition helpers.h:2285
RAMUniquePtr< T[]> make_unique_array_for_overwrite(size_t n)
n elements left uninitialized, as std::make_unique_for_overwrite does; empty on exhaustion,...
Definition helpers.h:2295
RAMUniquePtr< StackType_t[]> stack_buffer_
Definition static_task.h:60
bool destroy()
Delete the task, keeping the TCB and stack buffer allocated for reuse by a subsequent create() call.
bool deallocate()
Delete the task (if created) and free the TCB and stack buffer.
TaskHandle_t handle_
Definition static_task.h:58
RAMUniquePtr< StaticTask_t > tcb_
Definition static_task.h:59
bool create(TaskFunction_t fn, const char *name, uint32_t stack_size, void *param, UBaseType_t priority, bool use_psram, BaseType_t core_id=tskNO_AFFINITY)
Allocate the TCB (always internal RAM) and the stack, then create the task.
uint8_t priority
static void uint32_t
spi_device_handle_t handle