ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
scheduler_callback.h
Go to the documentation of this file.
1#pragma once
2
3#include <concepts>
4#include <cstddef>
5#include <cstdint>
6#include <new>
7#include <type_traits>
8#include <utility>
9
11
12namespace esphome {
13
22 public:
23 static constexpr size_t INLINE_WORDS = 3;
24
25 template<typename F>
26 static constexpr bool FITS_INLINE = sizeof(F) <= INLINE_WORDS * sizeof(uintptr_t) &&
27 alignof(F) <= alignof(uintptr_t) && std::is_trivially_copyable_v<F>;
28
29 template<typename F>
30 requires std::invocable<std::decay_t<F> &>
32 using D = std::decay_t<F>;
33 if constexpr (FITS_INLINE<D>) {
34 this->invoke_ = [](SchedulerCallback *self) { (*std::launder(reinterpret_cast<D *>(self->bytes_)))(); };
35 ::new (this->bytes_) D(std::forward<F>(f));
36 } else {
37 this->invoke_ = &SchedulerCallback::invoke_boxed;
38 this->boxed_.obj = ::new D(std::forward<F>(f));
39 this->boxed_.run = [](void *obj) { (*static_cast<D *>(obj))(); };
40 if constexpr (std::is_trivially_destructible_v<D> && alignof(D) <= __STDCPP_DEFAULT_NEW_ALIGNMENT__) {
41 this->boxed_.release = static_cast<void (*)(void *)>(&::operator delete); // one for all such types
42 } else {
43 this->boxed_.release = [](void *obj) { ::delete static_cast<D *>(obj); };
44 }
45 }
46 }
49#ifdef ESPHOME_DEBUG_SCHEDULER
50 // Catches one dropped before reaching the scheduler, at the call site (also with NDEBUG)
52 if (this->invoke_ != nullptr)
53 report_dropped();
54 }
55#endif
56
57 private:
58 friend class Scheduler;
59
60 SchedulerCallback() : invoke_(nullptr) {} // a SchedulerItem's slot, filled by take_()
61
62 void run_() { this->invoke_(this); }
64 void release_() {
65 const bool boxed = this->invoke_ == &SchedulerCallback::invoke_boxed;
66#ifdef ESPHOME_DEBUG_SCHEDULER
67 this->invoke_ = nullptr; // empty for the destructor's check
68#endif
69 if (boxed)
70 this->boxed_.release(this->boxed_.obj);
71 }
75 void take_(SchedulerCallback &other) {
76 this->invoke_ = other.invoke_;
77 auto *dst = static_cast<unsigned char *>(__builtin_assume_aligned(this->bytes_, alignof(uintptr_t)));
78 const auto *src = static_cast<const unsigned char *>(__builtin_assume_aligned(other.bytes_, alignof(uintptr_t)));
79 for (size_t i = 0; i < sizeof(this->bytes_); i += sizeof(uintptr_t))
80 __builtin_memcpy(dst + i, src + i, sizeof(uintptr_t));
81#ifdef ESPHOME_DEBUG_SCHEDULER
82 other.invoke_ = nullptr; // only the debug destructor check reads other after this
83#endif
84 }
85#ifdef ESPHOME_DEBUG_SCHEDULER
86 [[noreturn]] static void report_dropped(); // defined in scheduler.cpp
87#endif
88 static void invoke_boxed(SchedulerCallback *self) { self->boxed_.run(self->boxed_.obj); }
89
90 void (*invoke_)(SchedulerCallback *self);
91 union {
92 alignas(uintptr_t) unsigned char bytes_[INLINE_WORDS * sizeof(uintptr_t)]; // an inline callable
93 struct {
94 void *obj;
95 void (*run)(void *obj);
96 void (*release)(void *obj);
98 };
99};
100
101static_assert(sizeof(SchedulerCallback) == (SchedulerCallback::INLINE_WORDS + 1) * sizeof(uintptr_t));
102
103} // namespace esphome
Type-erased scheduler callable, built from a callable at the call site: inline when small and trivial...
SchedulerCallback(const SchedulerCallback &)=delete
struct esphome::SchedulerCallback::@233::@235 boxed_
static constexpr size_t INLINE_WORDS
SchedulerCallback & operator=(const SchedulerCallback &)=delete
unsigned char bytes_[INLINE_WORDS *sizeof(uintptr_t)]
static constexpr bool FITS_INLINE
const void * src
Definition hal.h:68