tin  1.5.9
app.h
Go to the documentation of this file.
1 // Copyright (c) 2026 Tinverse LLC. All rights reserved.
2 // SPDX-License-Identifier: LicenseRef-Tinverse-Commercial
3 
16 
17 #pragma once
18 
19 #include <cstddef>
20 #include <cstdint>
21 #include <utility>
22 
23 #include "tsm/runtime/executor.h"
24 #include "tsm/runtime/runtime.h"
25 #include "tsm/ticks.h"
26 
27 namespace tsm::runtime {
28 
29 template<typename Definition,
30  typename RuntimePolicy,
31  template<typename...> class Executor = cooperative_executor,
32  std::size_t TimerSlots = 0U>
33 class app
34 {
35  public:
36  using definition = Definition;
38  using executor_type = Executor<runtime_type>;
39  using timer_type =
41  using machine_type = typename runtime_type::machine_type;
42  using context_type = typename runtime_type::context_type;
43 
44  app()
45  : executor_(runtime_)
46  , timers_(runtime_)
47  {
48  }
49 
50  [[nodiscard]] runtime_type& runtime() noexcept
51  {
52  return runtime_;
53  }
54  [[nodiscard]] runtime_type const& runtime() const noexcept
55  {
56  return runtime_;
57  }
58 
59  [[nodiscard]] executor_type& executor() noexcept
60  {
61  return executor_;
62  }
63  [[nodiscard]] executor_type const& executor() const noexcept
64  {
65  return executor_;
66  }
67 
68  [[nodiscard]] auto spawner()
69  {
70  return executor_.spawner();
71  }
72 
73  [[nodiscard]] machine_type& machine() noexcept
74  {
75  return runtime_.machine();
76  }
77  [[nodiscard]] machine_type const& machine() const noexcept
78  {
79  return runtime_.machine();
80  }
81 
82  [[nodiscard]] context_type& context() noexcept
83  {
84  return runtime_.context();
85  }
86  [[nodiscard]] context_type const& context() const noexcept
87  {
88  return runtime_.context();
89  }
90 
91  [[nodiscard]] bool step()
92  {
93  return executor_.step();
94  }
95  std::size_t run_ready()
96  {
97  return executor_.run_ready();
98  }
99  std::size_t tick(tsm::tick_rep elapsed_ticks = 1U)
100  {
101  std::size_t work = executor_.tick(elapsed_ticks);
102  const std::size_t timer_work = timers_.tick(elapsed_ticks);
103  work += timer_work;
104  if (timer_work > 0U) {
105  work += executor_.run_ready();
106  }
107  return work;
108  }
109 
110  std::size_t tick(tsm::tick_count elapsed_ticks)
111  {
112  return tick(elapsed_ticks.count());
113  }
114 
115  template<typename Event>
116  [[nodiscard]] bool send_event(Event&& event)
117  {
118  return runtime_.send_event(std::forward<Event>(event));
119  }
120 
121  template<typename Event>
122  [[nodiscard]] bool after_ticks(Event&& event, tsm::tick_rep ticks)
123  {
124  static_assert(TimerSlots > 0U,
125  "tsm: app::after_ticks requires a non-zero TimerSlots "
126  "template argument");
127  return timers_.after_ticks(std::forward<Event>(event), ticks);
128  }
129 
130  template<typename Event>
131  [[nodiscard]] bool after_ticks(Event&& event, tick_count ticks)
132  {
133  return after_ticks(std::forward<Event>(event), ticks.count());
134  }
135 
136  template<typename Event>
137  [[nodiscard]] bool every_ticks(Event&& event, tsm::tick_rep ticks)
138  {
139  static_assert(TimerSlots > 0U,
140  "tsm: app::every_ticks requires a non-zero TimerSlots "
141  "template argument");
142  return timers_.every_ticks(std::forward<Event>(event), ticks);
143  }
144 
145  template<typename Event>
146  [[nodiscard]] bool every_ticks(Event&& event, tick_count ticks)
147  {
148  return every_ticks(std::forward<Event>(event), ticks.count());
149  }
150 
151  private:
152  runtime_type runtime_{};
153  executor_type executor_;
154  timer_type timers_;
155 };
156 
157 } // namespace tsm::runtime
158 
159 namespace tsm {
160 
161 template<typename Definition,
162  typename RuntimePolicy,
163  template<typename...> class Executor = runtime::cooperative_executor,
164  std::size_t TimerSlots = 0U>
166 
167 } // namespace tsm
Definition: app.h:34
typename runtime_type::context_type context_type
Definition: app.h:42
Executor< runtime_type > executor_type
Definition: app.h:38
runtime_type const & runtime() const noexcept
Definition: app.h:54
delayed_event_timer< runtime_type, TimerSlots==0U ? 1U :TimerSlots > timer_type
Definition: app.h:40
bool every_ticks(Event &&event, tick_count ticks)
Definition: app.h:146
machine_type const & machine() const noexcept
Definition: app.h:77
std::size_t tick(tsm::tick_rep elapsed_ticks=1U)
Definition: app.h:99
app()
Definition: app.h:44
context_type const & context() const noexcept
Definition: app.h:86
bool send_event(Event &&event)
Definition: app.h:116
Runtime< Definition, RuntimePolicy > runtime_type
Definition: app.h:37
bool every_ticks(Event &&event, tsm::tick_rep ticks)
Definition: app.h:137
runtime_type & runtime() noexcept
Definition: app.h:50
bool after_ticks(Event &&event, tick_count ticks)
Definition: app.h:131
bool step()
Definition: app.h:91
typename runtime_type::machine_type machine_type
Definition: app.h:41
bool after_ticks(Event &&event, tsm::tick_rep ticks)
Definition: app.h:122
std::size_t tick(tsm::tick_count elapsed_ticks)
Definition: app.h:110
Definition definition
Definition: app.h:36
context_type & context() noexcept
Definition: app.h:82
machine_type & machine() noexcept
Definition: app.h:73
std::size_t run_ready()
Definition: app.h:95
auto spawner()
Definition: app.h:68
executor_type const & executor() const noexcept
Definition: app.h:63
executor_type & executor() noexcept
Definition: app.h:59
Definition: executor.h:361
std::size_t tick(tsm::tick_rep elapsed_ticks=1U)
Definition: executor.h:408
Target-independent executors for tsm runtimes.
Definition: freertos.h:26
detail::runtime_impl< Definition, Policy, MachinePolicy > Runtime
Definition: runtime.h:531
cooperative_executor(Tasks &...) -> cooperative_executor< Tasks... >
Definition: bare_metal.h:20
constexpr tick_count ticks(tick_rep value) noexcept
Definition: ticks.h:85
TSM_TICK_REP tick_rep
Definition: ticks.h:35
Strong value type for semantic scheduler ticks.
Definition: ticks.h:54
constexpr tick_rep count() const noexcept
Definition: ticks.h:73
Target-neutral tick value type.
Runtime implementations for direct, composite-queue, and per-region dispatch.