@@ -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
@@ -275,40 +275,27 @@ impl IioThread {
275
275
// setup was sucessful.
276
276
// This is why we create Self inside the thread and send it back
277
277
// to the calling thread via a queue.
278
- let ( thread_res_tx , thread_res_rx ) = bounded ( 1 ) ;
278
+ let ( thread_tx , thread_rx ) = bounded ( 1 ) ;
279
279
280
280
// Spawn a high priority thread that updates the atomic values in `thread`.
281
281
wtb. spawn_thread ( thread_name, move || {
282
- let adc_setup_res = Self :: adc_setup (
282
+ let ( channels , mut buf ) = Self :: adc_setup (
283
283
adc_name,
284
284
trigger_name,
285
285
sample_rate,
286
286
channel_descs,
287
287
buffer_len,
288
- ) ;
289
- let ( thread, channels, mut buf) = match adc_setup_res {
290
- Ok ( ( channels, buf) ) => {
291
- let thread = Arc :: new ( Self {
292
- ref_instant : Instant :: now ( ) ,
293
- timestamp : AtomicU64 :: new ( TIMESTAMP_ERROR ) ,
294
- values : channels. iter ( ) . map ( |_| AtomicU16 :: new ( 0 ) ) . collect ( ) ,
295
- channel_descs,
296
- } ) ;
297
-
298
- ( thread, channels, buf)
299
- }
300
- Err ( e) => {
301
- // Can not fail in practice as the queue is known to be empty
302
- // at this point.
303
- thread_res_tx
304
- . try_send ( Err ( e) )
305
- . expect ( "Failed to signal ADC setup error due to full queue" ) ;
306
- return Ok ( ( ) ) ;
307
- }
308
- } ;
288
+ ) ?;
289
+
290
+ let thread = Arc :: new ( Self {
291
+ ref_instant : Instant :: now ( ) ,
292
+ timestamp : AtomicU64 :: new ( TIMESTAMP_ERROR ) ,
293
+ values : channels. iter ( ) . map ( |_| AtomicU16 :: new ( 0 ) ) . collect ( ) ,
294
+ channel_descs,
295
+ } ) ;
309
296
310
297
let thread_weak = Arc :: downgrade ( & thread) ;
311
- let mut signal_ready = Some ( ( thread, thread_res_tx ) ) ;
298
+ let mut signal_ready = Some ( ( thread, thread_tx ) ) ;
312
299
313
300
// Stop running as soon as the last reference to this Arc<IioThread>
314
301
// is dropped (e.g. the weak reference can no longer be upgraded).
@@ -318,18 +305,7 @@ impl IioThread {
318
305
319
306
error ! ( "Failed to refill {} ADC buffer: {}" , adc_name, e) ;
320
307
321
- // If the ADC has not yet produced any values we still have the
322
- // queue at hand that signals readiness to the main thread.
323
- // This gives us a chance to return an Err from new().
324
- // If the queue was already used just print an error instead.
325
- if let Some ( ( _, tx) ) = signal_ready. take ( ) {
326
- // Can not fail in practice as the queue is only .take()n
327
- // once and thus known to be empty.
328
- tx. try_send ( Err ( Error :: new ( e) ) )
329
- . expect ( "Failed to signal ADC setup error due to full queue" ) ;
330
- }
331
-
332
- break ;
308
+ Err ( e) ?;
333
309
}
334
310
335
311
let values = channels. iter ( ) . map ( |ch| {
@@ -356,17 +332,14 @@ impl IioThread {
356
332
if let Some ( ( content, tx) ) = signal_ready. take ( ) {
357
333
// Can not fail in practice as the queue is only .take()n
358
334
// once and thus known to be empty.
359
- tx. try_send ( Ok ( content) )
360
- . expect ( "Failed to signal ADC setup completion due to full queue" ) ;
335
+ tx. try_send ( content) ?;
361
336
}
362
337
}
363
338
364
339
Ok ( ( ) )
365
340
} ) ?;
366
341
367
- let thread = thread_res_rx. recv ( ) . await ??;
368
-
369
- Ok ( thread)
342
+ Ok ( thread_rx. recv ( ) . await ?)
370
343
}
371
344
372
345
pub async fn new_stm32 (
0 commit comments