1use rusty_rtos_core::config::Config;
23use rusty_rtos_core::error::{Error, Result};
24use rusty_rtos_core::handle::{EventGroupHandle, TaskHandle};
25use rusty_rtos_core::hooks::TickHook;
26use rusty_rtos_core::isr::Woken;
27use rusty_rtos_core::list::ListId;
28use rusty_rtos_core::port::Port;
29use rusty_rtos_core::trace::{Event, Trace};
30
31use crate::kernel::{Kernel, OwedTrace};
32use crate::lists_for;
33use crate::queue::Wait::{self, Ready};
34
35#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
38pub struct EventGroup {
39 pub(crate) bits: u32,
41}
42
43const CLEAR_EVENTS_ON_EXIT: u64 = 0x0100_0000;
45const UNBLOCKED_DUE_TO_BIT_SET: u64 = 0x0200_0000;
47const WAIT_FOR_ALL_BITS: u64 = 0x0400_0000;
49pub(crate) const EVENT_BITS_CONTROL_BYTES: u64 = 0xff00_0000;
52
53pub const PENDED_SET_BITS: u16 = u16::MAX;
55pub const PENDED_CLEAR_BITS: u16 = u16::MAX - 1;
58
59impl<
60 C: Config,
61 P: Port,
62 T: Trace,
63 H,
64 const TASKS: usize,
65 const ITEMS: usize,
66 const LISTS: usize,
67 const QUEUES: usize,
68 const SLOTS: usize,
69 const BUFFERS: usize,
70 const BYTES: usize,
71 const TIMERS: usize,
72 const GROUPS: usize,
73> Kernel<C, P, T, H, TASKS, ITEMS, LISTS, QUEUES, SLOTS, BUFFERS, BYTES, TIMERS, GROUPS>
74where
75 H: TickHook<Self>,
76{
77 pub(crate) fn event_group_list(group: EventGroupHandle) -> ListId {
79 let base = u8::try_from(lists_for(C::MAX_PRIORITIES, QUEUES, 0)).unwrap_or(u8::MAX);
80 base.saturating_add(u8::try_from(group.index()).unwrap_or(u8::MAX))
81 }
82
83 const fn wait_condition_met(current: u32, wait_for: u32, all: bool) -> bool {
85 if all {
86 current & wait_for == wait_for
87 } else {
88 current & wait_for != 0
89 }
90 }
91
92 pub fn event_group_create(&mut self) -> Result<EventGroupHandle> {
97 self.account_for_allocation();
98 let group = self
99 .groups
100 .try_insert(EventGroup::default())
101 .map_err(|_| Error::Full)?;
102 let list = Self::event_group_list(group);
106 while let Ok(Some(item)) = self.lists.head(list) {
107 let _ = self.lists.remove(item);
108 }
109 let tick = self.tick;
110 self.trace.note_exits(self.port.exits());
111 self.trace.event(tick, Event::EventGroupCreate { group });
112 Ok(group)
113 }
114
115 pub fn event_group_delete(&mut self, group: EventGroupHandle) -> Result<()> {
124 self.groups.resolve(group)?;
125 self.suspend_all();
126 let list = Self::event_group_list(group);
127 while let Ok(Some(item)) = self.lists.head(list) {
128 self.remove_from_unordered_event_list(item, UNBLOCKED_DUE_TO_BIT_SET)?;
129 }
130 let _ = self.groups.remove(group);
131 let _ = self.resume_all();
132 self.account_for_allocation();
136 Ok(())
137 }
138
139 pub fn event_group_bits(&mut self, group: EventGroupHandle) -> Result<u32> {
146 self.event_group_clear_bits(group, 0)
147 }
148
149 pub fn event_group_bits_from_isr(&self, group: EventGroupHandle) -> Result<u32> {
155 Ok(self.groups.resolve(group)?.bits)
156 }
157
158 pub fn event_group_clear_bits(&mut self, group: EventGroupHandle, bits: u32) -> Result<u32> {
163 self.enter_critical();
164 let result = self.groups.resolve_mut(group).map(|g| {
166 let before = g.bits;
167 g.bits &= !bits;
168 before
169 });
170 self.exit_critical();
171 result
172 }
173
174 pub fn event_group_set_bits(&mut self, group: EventGroupHandle, bits: u32) -> Result<u32> {
180 self.groups.resolve(group)?;
181 let list = Self::event_group_list(group);
182 self.suspend_all();
183 let tick = self.tick;
184 self.trace.note_exits(self.port.exits());
185 self.trace
186 .event(tick, Event::EventGroupSetBits { group, bits });
187 if let Ok(g) = self.groups.resolve_mut(group) {
188 g.bits |= bits;
189 }
190 let mut to_clear: u32 = 0;
191 let mut item = self.lists.head(list)?;
192 while let Some(this) = item {
193 let next = self.lists.next(this)?;
196 let value = self.lists.value(this)?;
197 let control = value & EVENT_BITS_CONTROL_BYTES;
198 let waited_for = (value & !EVENT_BITS_CONTROL_BYTES) as u32;
199 let current = self.groups.resolve(group)?.bits;
200 if Self::wait_condition_met(current, waited_for, control & WAIT_FOR_ALL_BITS != 0) {
201 if control & CLEAR_EVENTS_ON_EXIT != 0 {
202 to_clear |= waited_for;
203 }
204 self.remove_from_unordered_event_list(
205 this,
206 u64::from(current) | UNBLOCKED_DUE_TO_BIT_SET,
207 )?;
208 }
209 item = next;
210 }
211 let result = match self.groups.resolve_mut(group) {
212 Ok(g) => {
213 g.bits &= !to_clear;
214 g.bits
215 }
216 Err(_) => 0,
217 };
218 let _ = self.resume_all();
219 Ok(result)
220 }
221
222 pub fn event_group_set_bits_from_isr(
231 &mut self,
232 group: EventGroupHandle,
233 bits: u32,
234 ) -> Result<(bool, Woken)> {
235 self.pend_group_call(PENDED_SET_BITS, group, bits)
236 }
237
238 pub fn event_group_clear_bits_from_isr(
243 &mut self,
244 group: EventGroupHandle,
245 bits: u32,
246 ) -> Result<(bool, Woken)> {
247 self.pend_group_call(PENDED_CLEAR_BITS, group, bits)
248 }
249
250 fn pend_group_call(
253 &mut self,
254 function: u16,
255 group: EventGroupHandle,
256 bits: u32,
257 ) -> Result<(bool, Woken)> {
258 self.timer_pend_function_call_from_isr(function, u64::from(group.index()), u64::from(bits))
259 }
260
261 pub fn event_group_pended_call(
271 &mut self,
272 function: u16,
273 param1: u64,
274 param2: u64,
275 ) -> Result<()> {
276 let Some(group) = self.groups.handle_at(param1 as u16) else {
277 return Err(Error::Gone);
278 };
279 let bits = param2 as u32;
280 match function {
281 PENDED_SET_BITS => {
282 self.event_group_set_bits(group, bits)?;
283 }
284 PENDED_CLEAR_BITS => {
285 self.event_group_clear_bits(group, bits)?;
286 }
287 _ => {}
288 }
289 Ok(())
290 }
291
292 pub fn event_group_wait_bits(
303 &mut self,
304 group: EventGroupHandle,
305 wait_for: u32,
306 clear_on_exit: bool,
307 wait_for_all: bool,
308 ticks: u64,
309 ) -> Result<Wait<u32>> {
310 let caller = self.current();
311 if self.take_event_resume(caller) {
312 let value = self.reset_event_item_value(caller)?;
313 let bits = self.finish_wait(group, value, wait_for, clear_on_exit, wait_for_all)?;
314 self.trace_wait_bits_end(
315 caller,
316 group,
317 wait_for,
318 value & UNBLOCKED_DUE_TO_BIT_SET == 0,
319 );
320 return Ok(Ready(bits));
321 }
322
323 self.groups.resolve(group)?;
324 self.suspend_all();
325 let current = self.groups.resolve(group)?.bits;
326 let mut blocked = false;
327 let mut timed_out = false;
328 let mut result = current;
329 if Self::wait_condition_met(current, wait_for, wait_for_all) {
330 if clear_on_exit {
331 if let Ok(g) = self.groups.resolve_mut(group) {
332 g.bits &= !wait_for;
333 }
334 }
335 } else if ticks == 0 {
336 timed_out = true;
337 } else {
338 let mut control = 0;
339 if clear_on_exit {
340 control |= CLEAR_EVENTS_ON_EXIT;
341 }
342 if wait_for_all {
343 control |= WAIT_FOR_ALL_BITS;
344 }
345 self.place_on_unordered_event_list(
346 Self::event_group_list(group),
347 u64::from(wait_for) | control,
348 ticks,
349 )?;
350 result = 0;
351 let tick = self.tick;
352 self.trace.note_exits(self.port.exits());
353 self.trace.event(
354 tick,
355 Event::EventGroupWaitBitsBlock {
356 group,
357 bits: wait_for,
358 },
359 );
360 blocked = true;
361 }
362 let already_yielded = self.resume_all();
363 if blocked {
364 if !already_yielded {
365 self.yield_or_owe(caller);
366 }
367 self.set_event_resume(caller);
368 return Ok(Wait::Blocked);
369 }
370 self.trace_wait_bits_end(caller, group, wait_for, timed_out);
371 Ok(Ready(result))
372 }
373
374 pub fn event_group_sync(
385 &mut self,
386 group: EventGroupHandle,
387 set: u32,
388 wait_for: u32,
389 ticks: u64,
390 ) -> Result<Wait<u32>> {
391 let caller = self.current();
392 if self.take_event_resume(caller) {
393 let value = self.reset_event_item_value(caller)?;
394 return Ok(Ready(self.finish_sync(group, value, wait_for)?));
395 }
396
397 self.groups.resolve(group)?;
398 self.suspend_all();
399 let original = self.groups.resolve(group)?.bits;
400 self.event_group_set_bits(group, set)?;
401 let mut blocked = false;
402 let result;
403 if (original | set) & wait_for == wait_for {
404 result = original | set;
405 if let Ok(g) = self.groups.resolve_mut(group) {
406 g.bits &= !wait_for;
407 }
408 } else if ticks == 0 {
409 result = self.groups.resolve(group)?.bits;
410 } else {
411 self.place_on_unordered_event_list(
412 Self::event_group_list(group),
413 u64::from(wait_for) | CLEAR_EVENTS_ON_EXIT | WAIT_FOR_ALL_BITS,
414 ticks,
415 )?;
416 result = 0;
417 blocked = true;
418 }
419 let already_yielded = self.resume_all();
420 if blocked {
421 if !already_yielded {
422 self.yield_or_owe(caller);
423 }
424 self.set_event_resume(caller);
425 return Ok(Wait::Blocked);
426 }
427 Ok(Ready(result))
428 }
429
430 fn finish_wait(
434 &mut self,
435 group: EventGroupHandle,
436 value: u64,
437 wait_for: u32,
438 clear_on_exit: bool,
439 wait_for_all: bool,
440 ) -> Result<u32> {
441 if value & UNBLOCKED_DUE_TO_BIT_SET != 0 {
442 return Ok((value & !EVENT_BITS_CONTROL_BYTES) as u32);
443 }
444 self.enter_critical();
445 let bits = self.groups.resolve(group).map(|g| g.bits);
446 if let Ok(bits) = bits {
447 if Self::wait_condition_met(bits, wait_for, wait_for_all) && clear_on_exit {
448 if let Ok(g) = self.groups.resolve_mut(group) {
449 g.bits &= !wait_for;
450 }
451 }
452 }
453 self.exit_critical();
454 Ok((u64::from(bits?) & !EVENT_BITS_CONTROL_BYTES) as u32)
455 }
456
457 fn finish_sync(&mut self, group: EventGroupHandle, value: u64, wait_for: u32) -> Result<u32> {
460 if value & UNBLOCKED_DUE_TO_BIT_SET != 0 {
461 return Ok((value & !EVENT_BITS_CONTROL_BYTES) as u32);
462 }
463 self.enter_critical();
464 let bits = self.groups.resolve(group).map(|g| g.bits);
465 if let Ok(bits) = bits {
466 if bits & wait_for == wait_for {
467 if let Ok(g) = self.groups.resolve_mut(group) {
468 g.bits &= !wait_for;
469 }
470 }
471 }
472 self.exit_critical();
473 Ok((u64::from(bits?) & !EVENT_BITS_CONTROL_BYTES) as u32)
474 }
475
476 fn trace_wait_bits_end(
479 &mut self,
480 caller: TaskHandle,
481 group: EventGroupHandle,
482 bits: u32,
483 timed_out: bool,
484 ) {
485 self.trace_failure_or_owe(
486 caller,
487 OwedTrace::EventGroupWaitBitsEnd {
488 group,
489 bits,
490 timed_out,
491 },
492 );
493 }
494}