Skip to content

Commit 0125256

Browse files
committed
dut_power: simplify the error handling inside the thread
Since we can now return early from `wtb.spawn_thread`-spawned threads, because they handle error propagation out of the thread context for us we no longer have to use queues to send back error results manually. Use that to simplify error handling in the thread setup and the steady state. Signed-off-by: Leonard Göhrs <l.goehrs@pengutronix.de>
1 parent 1e5c050 commit 0125256

File tree

1 file changed

+19
-27
lines changed

1 file changed

+19
-27
lines changed

src/dut_power.rs

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ impl DutPwrThread {
338338
// as well.
339339
// Use a queue to notify the calling thread if the priority setup
340340
// succeeded.
341-
let (thread_res_tx, mut thread_res_rx) = bounded(1);
341+
let (thread_tx, thread_rx) = bounded(1);
342342

343343
// Spawn a high priority thread that handles the power status
344344
// in a realtimey fashion.
@@ -353,24 +353,20 @@ impl DutPwrThread {
353353
let mut volt_filter = MedianFilter::<4>::new();
354354
let mut curr_filter = MedianFilter::<4>::new();
355355

356-
let (tick_weak, request, state) = match realtime_priority() {
357-
Ok(_) => {
358-
let tick = Arc::new(AtomicU32::new(0));
359-
let tick_weak = Arc::downgrade(&tick);
356+
realtime_priority()?;
360357

361-
let request = Arc::new(AtomicU8::new(OutputRequest::Idle as u8));
362-
let state = Arc::new(AtomicU8::new(OutputState::Off as u8));
358+
let (tick_weak, request, state) = {
359+
let tick = Arc::new(AtomicU32::new(0));
360+
let tick_weak = Arc::downgrade(&tick);
363361

364-
thread_res_tx
365-
.try_send(Ok((tick, request.clone(), state.clone())))
366-
.unwrap();
362+
let request = Arc::new(AtomicU8::new(OutputRequest::Idle as u8));
363+
let state = Arc::new(AtomicU8::new(OutputState::Off as u8));
367364

368-
(tick_weak, request, state)
369-
}
370-
Err(e) => {
371-
thread_res_tx.try_send(Err(e)).unwrap();
372-
panic!()
373-
}
365+
thread_tx
366+
.try_send((tick, request.clone(), state.clone()))
367+
.expect("Queue that should be empty wasn't");
368+
369+
(tick_weak, request, state)
374370
};
375371

376372
// The grace period contains the number of loop iterations until
@@ -501,22 +497,18 @@ impl DutPwrThread {
501497
match req {
502498
OutputRequest::Idle => {}
503499
OutputRequest::On => {
504-
discharge_line
505-
.set_value(1 - DISCHARGE_LINE_ASSERTED)
506-
.unwrap();
507-
pwr_line.set_value(PWR_LINE_ASSERTED).unwrap();
500+
discharge_line.set_value(1 - DISCHARGE_LINE_ASSERTED)?;
501+
pwr_line.set_value(PWR_LINE_ASSERTED)?;
508502
state.store(OutputState::On as u8, Ordering::Relaxed);
509503
}
510504
OutputRequest::Off => {
511-
discharge_line.set_value(DISCHARGE_LINE_ASSERTED).unwrap();
512-
pwr_line.set_value(1 - PWR_LINE_ASSERTED).unwrap();
505+
discharge_line.set_value(DISCHARGE_LINE_ASSERTED)?;
506+
pwr_line.set_value(1 - PWR_LINE_ASSERTED)?;
513507
state.store(OutputState::Off as u8, Ordering::Relaxed);
514508
}
515509
OutputRequest::OffFloating => {
516-
discharge_line
517-
.set_value(1 - DISCHARGE_LINE_ASSERTED)
518-
.unwrap();
519-
pwr_line.set_value(1 - PWR_LINE_ASSERTED).unwrap();
510+
discharge_line.set_value(1 - DISCHARGE_LINE_ASSERTED)?;
511+
pwr_line.set_value(1 - PWR_LINE_ASSERTED)?;
520512
state.store(OutputState::OffFloating as u8, Ordering::Relaxed);
521513
}
522514
}
@@ -528,7 +520,7 @@ impl DutPwrThread {
528520
Ok(())
529521
})?;
530522

531-
let (tick, request, state) = thread_res_rx.next().await.unwrap()?;
523+
let (tick, request, state) = thread_rx.recv().await?;
532524

533525
// The request and state topic use the same external path, this way one
534526
// can e.g. publish "On" to the topic and be sure that the output is

0 commit comments

Comments
 (0)