@@ -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,38 +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. 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
+ } ) ;
369
358
370
359
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 ) ) ;
372
361
373
362
// Stop running as soon as the last reference to this Arc<IioThread>
374
363
// is dropped (e.g. the weak reference can no longer be upgraded).
@@ -378,17 +367,7 @@ impl IioThread {
378
367
379
368
error ! ( "Failed to refill {} ADC buffer: {}" , adc_name, e) ;
380
369
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) ?;
392
371
}
393
372
394
373
let values = channels. iter ( ) . map ( |ch| {
@@ -415,16 +394,15 @@ impl IioThread {
415
394
if let Some ( ( content, tx) ) = signal_ready. take ( ) {
416
395
// Can not fail in practice as the queue is only .take()n
417
396
// 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" ) ;
419
399
}
420
400
}
421
401
422
402
Ok ( ( ) )
423
403
} ) ?;
424
404
425
- let thread = thread_res_rx. recv ( ) . await ??;
426
-
427
- Ok ( thread)
405
+ Ok ( thread_rx. recv ( ) . await ?)
428
406
}
429
407
430
408
pub async fn new_stm32 ( wtb : & mut WatchedTasksBuilder ) -> Result < Arc < Self > > {
0 commit comments