14static const char *
const TAG =
"scheduler";
16static const uint32_t MAX_LOGICALLY_DELETED_ITEMS = 10;
21#ifdef ESPHOME_DEBUG_SCHEDULER
23static void validate_static_string(
const char *name) {
29 uintptr_t addr =
reinterpret_cast<uintptr_t
>(name);
33 uintptr_t stack_addr =
reinterpret_cast<uintptr_t
>(&stack_var);
37 if (addr > (stack_addr - 0x2000) && addr < (stack_addr + 0x2000)) {
39 "WARNING: Scheduler name '%s' at %p appears to be on the stack - this is unsafe!\n"
40 " Stack reference at %p",
41 name, name, &stack_var);
46 static const char *static_str =
"test";
47 uintptr_t static_addr =
reinterpret_cast<uintptr_t
>(static_str);
50 if (addr > static_addr + 0x100000 || (static_addr > 0x100000 && addr < static_addr - 0x100000)) {
51 ESP_LOGW(TAG,
"WARNING: Scheduler name '%s' at %p might be on heap (static ref at %p)", name, name, static_str);
63 const void *name_ptr, uint32_t
delay, std::function<
void()> func) {
65 const char *name_cstr = this->get_name_cstr_(is_static_string, name_ptr);
67 if (
delay == SCHEDULER_DONT_RUN) {
70 this->cancel_item_locked_(component, name_cstr,
type);
75 auto item = make_unique<SchedulerItem>();
76 item->component = component;
77 item->set_name(name_cstr, !is_static_string);
79 item->callback = std::move(func);
82#if !defined(USE_ESP8266) && !defined(USE_RP2040)
88 this->cancel_item_locked_(component, name_cstr,
type);
89 this->defer_queue_.push_back(std::move(item));
94 const auto now = this->
millis_();
98 item->interval =
delay;
101 item->next_execution_ = now + offset;
104 item->next_execution_ = now +
delay;
107#ifdef ESPHOME_DEBUG_SCHEDULER
109 if (is_static_string && name_cstr !=
nullptr) {
110 validate_static_string(name_cstr);
116 ESP_LOGD(TAG,
"set_%s(name='%s/%s', %s=%" PRIu32
")", type_str, item->get_source(),
117 name_cstr ? name_cstr :
"(null)", type_str,
delay);
119 ESP_LOGD(TAG,
"set_%s(name='%s/%s', %s=%" PRIu32
", offset=%" PRIu32
")", type_str, item->get_source(),
120 name_cstr ? name_cstr :
"(null)", type_str,
delay,
static_cast<uint32_t
>(item->next_execution_ - now));
127 this->cancel_item_locked_(component, name_cstr,
type);
130 this->to_add_.push_back(std::move(item));
138 std::function<
void()> func) {
148 std::function<
void()> func) {
153 std::function<
void()> func) {
165 uint8_t retry_countdown;
166 uint32_t current_interval;
169 float backoff_increase_factor;
173static void retry_handler(
const std::shared_ptr<RetryArgs> &args) {
174 RetryResult const retry_result = args->func(--args->retry_countdown);
178 args->scheduler->set_timeout(args->component, args->name, args->current_interval, [args]() { retry_handler(args); });
180 args->current_interval *= args->backoff_increase_factor;
184 uint8_t max_attempts, std::function<
RetryResult(uint8_t)> func,
185 float backoff_increase_factor) {
189 if (initial_wait_time == SCHEDULER_DONT_RUN)
192 ESP_LOGVV(TAG,
"set_retry(name='%s', initial_wait_time=%" PRIu32
", max_attempts=%u, backoff_factor=%0.1f)",
193 name.c_str(), initial_wait_time, max_attempts, backoff_increase_factor);
195 if (backoff_increase_factor < 0.0001) {
197 "set_retry(name='%s'): backoff_factor cannot be close to zero nor negative (%0.1f). Using 1.0 instead",
198 name.c_str(), backoff_increase_factor);
199 backoff_increase_factor = 1;
202 auto args = std::make_shared<RetryArgs>();
203 args->func = std::move(func);
204 args->retry_countdown = max_attempts;
205 args->current_interval = initial_wait_time;
206 args->component = component;
207 args->name =
"retry$" + name;
208 args->backoff_increase_factor = backoff_increase_factor;
209 args->scheduler =
this;
212 this->
set_timeout(component, args->name, 0, [args]() { retry_handler(args); });
224 auto &item = this->items_[0];
225 const auto now = this->
millis_();
226 if (item->next_execution_ < now)
228 return item->next_execution_ - now;
231#if !defined(USE_ESP8266) && !defined(USE_RP2040)
245 while (!this->defer_queue_.empty()) {
249 std::unique_ptr<SchedulerItem> item;
252 item = std::move(this->defer_queue_.front());
253 this->defer_queue_.pop_front();
258 if (!this->should_skip_item_(item.get())) {
259 this->execute_item_(item.get());
264 const auto now = this->
millis_();
267#ifdef ESPHOME_DEBUG_SCHEDULER
268 static uint64_t last_print = 0;
270 if (now - last_print > 2000) {
272 std::vector<std::unique_ptr<SchedulerItem>> old_items;
273 ESP_LOGD(TAG,
"Items: count=%zu, now=%" PRIu64
" (%u, %" PRIu32
")", this->items_.size(), now, this->millis_major_,
275 while (!this->empty_()) {
276 std::unique_ptr<SchedulerItem> item;
279 item = std::move(this->items_[0]);
283 const char *name = item->get_name();
284 ESP_LOGD(TAG,
" %s '%s/%s' interval=%" PRIu32
" next_execution in %" PRIu64
"ms at %" PRIu64,
285 item->get_type_str(), item->get_source(), name ? name :
"(null)", item->interval,
286 item->next_execution_ - now, item->next_execution_);
288 old_items.push_back(std::move(item));
294 this->items_ = std::move(old_items);
302 if (this->to_remove_ > MAX_LOGICALLY_DELETED_ITEMS) {
310 std::vector<std::unique_ptr<SchedulerItem>> valid_items;
313 for (
auto &item : this->items_) {
315 valid_items.push_back(std::move(item));
320 this->items_ = std::move(valid_items);
323 this->to_remove_ = 0;
326 while (!this->empty_()) {
330 auto &item = this->items_[0];
331 if (item->next_execution_ > now) {
336 if (item->component !=
nullptr && item->component->is_failed()) {
341#ifdef ESPHOME_DEBUG_SCHEDULER
342 const char *item_name = item->get_name();
343 ESP_LOGV(TAG,
"Running %s '%s/%s' with interval=%" PRIu32
" next_execution=%" PRIu64
" (now=%" PRIu64
")",
344 item->get_type_str(), item->get_source(), item_name ? item_name :
"(null)", item->interval,
345 item->next_execution_, now);
351 this->execute_item_(item.get());
358 auto item = std::move(this->items_[0]);
370 item->next_execution_ = now + item->interval;
373 this->to_add_.push_back(std::move(item));
382 for (
auto &it : this->to_add_) {
387 this->items_.push_back(std::move(it));
390 this->to_add_.clear();
400 if (this->to_remove_ == 0)
412 while (!this->items_.empty()) {
413 auto &item = this->items_[0];
422 this->items_.pop_back();
426void HOT Scheduler::execute_item_(SchedulerItem *item) {
429 uint32_t now_ms =
millis();
436bool HOT Scheduler::cancel_item_(Component *component,
bool is_static_string,
const void *name_ptr,
437 SchedulerItem::Type
type) {
439 const char *name_cstr = this->get_name_cstr_(is_static_string, name_ptr);
442 LockGuard guard{this->lock_};
443 return this->cancel_item_locked_(component, name_cstr,
type);
447bool HOT Scheduler::cancel_item_locked_(Component *component,
const char *name_cstr, SchedulerItem::Type
type) {
449 if (name_cstr ==
nullptr || name_cstr[0] ==
'\0') {
453 size_t total_cancelled = 0;
456#if !defined(USE_ESP8266) && !defined(USE_RP2040)
459 for (
auto &item : this->defer_queue_) {
460 if (this->matches_item_(item, component, name_cstr,
type)) {
469 for (
auto &item : this->items_) {
470 if (this->matches_item_(item, component, name_cstr,
type)) {
478 for (
auto &item : this->to_add_) {
479 if (this->matches_item_(item, component, name_cstr,
type)) {
486 return total_cancelled > 0;
491 const uint32_t now =
millis();
493 if (now < this->last_millis_) {
495 this->millis_major_++;
496 ESP_LOGD(TAG,
"Incrementing scheduler major at %" PRIu64
"ms",
497 now + (
static_cast<uint64_t
>(this->millis_major_) << 32));
499 this->last_millis_ = now;
501 return now + (
static_cast<uint64_t
>(this->millis_major_) << 32);
505 const std::unique_ptr<SchedulerItem> &b) {
506 return a->next_execution_ > b->next_execution_;
void set_current_component(Component *component)
Helper class that wraps a mutex with a RAII-style API.
bool cancel_retry(Component *component, const std::string &name)
void set_timer_common_(Component *component, SchedulerItem::Type type, bool is_static_string, const void *name_ptr, uint32_t delay, std::function< void()> func)
void set_retry(Component *component, const std::string &name, uint32_t initial_wait_time, uint8_t max_attempts, std::function< RetryResult(uint8_t)> func, float backoff_increase_factor=1.0f)
bool cancel_timeout(Component *component, const std::string &name)
bool cancel_interval(Component *component, const std::string &name)
void set_timeout(Component *component, const std::string &name, uint32_t timeout, std::function< void()> func)
void set_interval(Component *component, const std::string &name, uint32_t interval, std::function< void()> func)
optional< uint32_t > next_schedule_in()
Providing packet encoding functions for exchanging data with a remote host.
uint32_t random_uint32()
Return a random 32-bit unsigned integer.
void IRAM_ATTR HOT delay(uint32_t ms)
uint32_t IRAM_ATTR HOT millis()
Application App
Global storage of Application pointer - only one Application can exist.
static bool cmp(const std::unique_ptr< SchedulerItem > &a, const std::unique_ptr< SchedulerItem > &b)