1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//! # Tracing
//!
//! The `trace` feature enables a number of callbacks that can be used to track the
//! lifecycle of tasks and/or executors.
//!
//! The passed IDs are only guaranteed to be unique for the duration of time the item is valid. If a task
//! ends, and is re-spawned, it MAY or MAY NOT have the same ID. While a task is active, the id will not change.
//! For executors, the same applies, but the IDs will be stable for practical embedded programs.
//!
//! Callbacks can be used by enabling the `trace` feature, implementing the [`Trace`]
//! trait, and registering the implementation with the [`trace_impl!`](crate::trace_impl) macro.
//! All callbacks must be implemented.
//!
//! ## Task Tracing lifecycle
//!
//! ```text
//! ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─
//! │(1) │
//! │ │
//! ╔════▼════╗ (2) ┌─────────┐ (3) ┌─────────┐ │
//! │ ║ SPAWNED ║────▶│ WAITING │────▶│ RUNNING │
//! ╚═════════╝ └─────────┘ └─────────┘ │
//! │ ▲ ▲ │ │ │
//! │ (4) │ │(6) │
//! │ │(7) └ ─ ─ ┘ │ │
//! │ │ │ │
//! │ ┌──────┐ (5) │ │ ┌─────┐
//! │ IDLE │◀────────────────┘ └─▶│ END │ │
//! │ └──────┘ └─────┘
//! ┌──────────────────────┐ │
//! └ ┤ Task Trace Lifecycle │─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─
//! └──────────────────────┘
//! ```
//!
//! 1. A task is spawned, [`task_new`](Trace::task_new) is called
//! 2. A task is enqueued for the first time, [`task_ready_begin`](Trace::task_ready_begin) is called
//! 3. A task is polled, [`task_exec_begin`](Trace::task_exec_begin) is called
//! 4. WHILE a task is polled, the task is re-awoken, and [`task_ready_begin`](Trace::task_ready_begin) is
//! called. The task does not IMMEDIATELY move state, until polling is complete and the
//! RUNNING state is exited. [`task_exec_end`](Trace::task_exec_end) is called when polling is
//! complete, marking the transition to WAITING
//! 5. Polling is complete, [`task_exec_end`](Trace::task_exec_end) is called
//! 6. The task has completed, and [`task_end`](Trace::task_end) is called
//! 7. A task is awoken, [`task_ready_begin`](Trace::task_ready_begin) is called
//!
//! ## Executor Tracing lifecycle
//!
//! ```text
//! ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─
//! │(1) │
//! │ │
//! ╔═══▼══╗ (2) ┌────────────┐ (3) ┌─────────┐ │
//! │ ║ IDLE ║──────────▶│ SCHEDULING │──────▶│ POLLING │
//! ╚══════╝ └────────────┘ └─────────┘ │
//! │ ▲ │ ▲ │
//! │ (5) │ │ (4) │ │
//! │ └──────────────┘ └────────────┘
//! ┌──────────────────────────┐ │
//! └ ┤ Executor Trace Lifecycle │─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─
//! └──────────────────────────┘
//! ```
//!
//! 1. The executor is started (no associated trace)
//! 2. A task on this executor is awoken. [`task_ready_begin`](Trace::task_ready_begin) is called
//! when this occurs, and [`poll_start`](Trace::poll_start) is called when the executor
//! actually begins running
//! 3. The executor has decided a task to poll. [`task_exec_begin`](Trace::task_exec_begin) is called
//! 4. The executor finishes polling the task. [`task_exec_end`](Trace::task_exec_end) is called
//! 5. The executor has finished polling tasks. [`executor_idle`](Trace::executor_idle) is called
//!
//! ## Idle
//!
//! [`executor_idle`](Trace::executor_idle) only means that a single executor has run out of work. With
//! multiple executors (e.g. a thread-mode executor plus one or more interrupt
//! executors), an interrupt executor going idle returns to the preempted
//! lower-priority context, which keeps running. The current thread/core is only
//! idle when its thread-mode executor reaches its sleep site, at which point
//! [`idle`](Trace::idle) is called. In multi-core chips, or when using threads under std or an
//! RTOS, this does not mean the entire system is idle.
use crateExecutorId;
use crateTaskRef;
unitrait!