Skip to content

Commit 83160d5

Browse files
committed
adc: hardware: improve/simplify error handling
Now that errors from threads are properly propagated we can simplify the error handling. Signed-off-by: Leonard Göhrs <l.goehrs@pengutronix.de>
1 parent 0e16a4e commit 83160d5

File tree

1 file changed

+16
-38
lines changed

1 file changed

+16
-38
lines changed

src/adc/iio/hardware.rs

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use std::path::Path;
2222
use std::sync::atomic::{AtomicU16, AtomicU64, Ordering};
2323
use std::time::{Duration, Instant};
2424

25-
use anyhow::{anyhow, Context, Error, Result};
25+
use anyhow::{anyhow, Context, Result};
2626
use async_std::channel::bounded;
2727
use async_std::sync::Arc;
2828

@@ -337,38 +337,27 @@ impl IioThread {
337337
// setup was sucessful.
338338
// This is why we create Self inside the thread and send it back
339339
// to the calling thread via a queue.
340-
let (thread_res_tx, thread_res_rx) = bounded(1);
340+
let (thread_tx, thread_rx) = bounded(1);
341341

342342
// Spawn a high priority thread that updates the atomic values in `thread`.
343343
wtb.spawn_thread(thread_name, move || {
344-
let adc_setup_res = Self::adc_setup(
344+
let (channels, mut buf) = Self::adc_setup(
345345
adc_name,
346346
trigger_name,
347347
sample_rate,
348348
channel_descs,
349349
buffer_len,
350-
);
351-
let (thread, channels, mut buf) = match adc_setup_res {
352-
Ok((channels, buf)) => {
353-
let thread = Arc::new(Self {
354-
ref_instant: Instant::now(),
355-
timestamp: AtomicU64::new(TIMESTAMP_ERROR),
356-
values: channels.iter().map(|_| AtomicU16::new(0)).collect(),
357-
channel_descs,
358-
});
359-
360-
(thread, channels, buf)
361-
}
362-
Err(e) => {
363-
// Can not fail in practice as the queue is known to be empty
364-
// at this point.
365-
thread_res_tx.try_send(Err(e)).unwrap();
366-
return Ok(());
367-
}
368-
};
350+
)?;
351+
352+
let thread = Arc::new(Self {
353+
ref_instant: Instant::now(),
354+
timestamp: AtomicU64::new(TIMESTAMP_ERROR),
355+
values: channels.iter().map(|_| AtomicU16::new(0)).collect(),
356+
channel_descs,
357+
});
369358

370359
let thread_weak = Arc::downgrade(&thread);
371-
let mut signal_ready = Some((thread, thread_res_tx));
360+
let mut signal_ready = Some((thread, thread_tx));
372361

373362
// Stop running as soon as the last reference to this Arc<IioThread>
374363
// is dropped (e.g. the weak reference can no longer be upgraded).
@@ -378,17 +367,7 @@ impl IioThread {
378367

379368
error!("Failed to refill {} ADC buffer: {}", adc_name, e);
380369

381-
// If the ADC has not yet produced any values we still have the
382-
// queue at hand that signals readiness to the main thread.
383-
// This gives us a chance to return an Err from new().
384-
// If the queue was already used just print an error instead.
385-
if let Some((_, tx)) = signal_ready.take() {
386-
// Can not fail in practice as the queue is only .take()n
387-
// once and thus known to be empty.
388-
tx.try_send(Err(Error::new(e))).unwrap();
389-
}
390-
391-
break;
370+
Err(e)?;
392371
}
393372

394373
let values = channels.iter().map(|ch| {
@@ -415,16 +394,15 @@ impl IioThread {
415394
if let Some((content, tx)) = signal_ready.take() {
416395
// Can not fail in practice as the queue is only .take()n
417396
// once and thus known to be empty.
418-
tx.try_send(Ok(content)).unwrap();
397+
tx.try_send(content)
398+
.expect("Queue that should be empty wasn't");
419399
}
420400
}
421401

422402
Ok(())
423403
})?;
424404

425-
let thread = thread_res_rx.recv().await??;
426-
427-
Ok(thread)
405+
Ok(thread_rx.recv().await?)
428406
}
429407

430408
pub async fn new_stm32(wtb: &mut WatchedTasksBuilder) -> Result<Arc<Self>> {

0 commit comments

Comments
 (0)