Skip to content

Commit cf2851d

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 32fdb7c commit cf2851d

File tree

1 file changed

+15
-42
lines changed

1 file changed

+15
-42
lines changed

src/adc/iio/hardware.rs

Lines changed: 15 additions & 42 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,40 +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
366-
.try_send(Err(e))
367-
.expect("Failed to signal ADC setup error due to full queue");
368-
return Ok(());
369-
}
370-
};
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+
});
371358

372359
let thread_weak = Arc::downgrade(&thread);
373-
let mut signal_ready = Some((thread, thread_res_tx));
360+
let mut signal_ready = Some((thread, thread_tx));
374361

375362
// Stop running as soon as the last reference to this Arc<IioThread>
376363
// is dropped (e.g. the weak reference can no longer be upgraded).
@@ -380,18 +367,7 @@ impl IioThread {
380367

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

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

397373
let values = channels.iter().map(|ch| {
@@ -418,17 +394,14 @@ impl IioThread {
418394
if let Some((content, tx)) = signal_ready.take() {
419395
// Can not fail in practice as the queue is only .take()n
420396
// once and thus known to be empty.
421-
tx.try_send(Ok(content))
422-
.expect("Failed to signal ADC setup completion due to full queue");
397+
tx.try_send(content)?;
423398
}
424399
}
425400

426401
Ok(())
427402
})?;
428403

429-
let thread = thread_res_rx.recv().await??;
430-
431-
Ok(thread)
404+
Ok(thread_rx.recv().await?)
432405
}
433406

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

0 commit comments

Comments
 (0)