@@ -22,7 +22,7 @@ use std::path::Path;
22
22
use std:: sync:: atomic:: { AtomicU16 , AtomicU64 , Ordering } ;
23
23
use std:: time:: { Duration , Instant } ;
24
24
25
- use anyhow:: { anyhow, Context , Error , Result } ;
25
+ use anyhow:: { anyhow, Context , Result } ;
26
26
use async_std:: channel:: bounded;
27
27
use async_std:: sync:: Arc ;
28
28
@@ -337,40 +337,27 @@ impl IioThread {
337
337
// setup was sucessful.
338
338
// This is why we create Self inside the thread and send it back
339
339
// 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 ) ;
341
341
342
342
// Spawn a high priority thread that updates the atomic values in `thread`.
343
343
wtb. spawn_thread ( thread_name, move || {
344
- let adc_setup_res = Self :: adc_setup (
344
+ let ( channels , mut buf ) = Self :: adc_setup (
345
345
adc_name,
346
346
trigger_name,
347
347
sample_rate,
348
348
channel_descs,
349
349
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
+ } ) ;
371
358
372
359
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 ) ) ;
374
361
375
362
// Stop running as soon as the last reference to this Arc<IioThread>
376
363
// is dropped (e.g. the weak reference can no longer be upgraded).
@@ -380,18 +367,7 @@ impl IioThread {
380
367
381
368
error ! ( "Failed to refill {} ADC buffer: {}" , adc_name, e) ;
382
369
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) ?;
395
371
}
396
372
397
373
let values = channels. iter ( ) . map ( |ch| {
@@ -418,17 +394,14 @@ impl IioThread {
418
394
if let Some ( ( content, tx) ) = signal_ready. take ( ) {
419
395
// Can not fail in practice as the queue is only .take()n
420
396
// 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) ?;
423
398
}
424
399
}
425
400
426
401
Ok ( ( ) )
427
402
} ) ?;
428
403
429
- let thread = thread_res_rx. recv ( ) . await ??;
430
-
431
- Ok ( thread)
404
+ Ok ( thread_rx. recv ( ) . await ?)
432
405
}
433
406
434
407
pub async fn new_stm32 ( wtb : & mut WatchedTasksBuilder ) -> Result < Arc < Self > > {
0 commit comments