@@ -20,9 +20,6 @@ use std::fs::create_dir;
2020use std:: io:: Read ;
2121use std:: path:: Path ;
2222use std:: sync:: atomic:: { AtomicU16 , AtomicU64 , Ordering } ;
23- use std:: sync:: Mutex ;
24- use std:: thread;
25- use std:: thread:: JoinHandle ;
2623use std:: time:: { Duration , Instant } ;
2724
2825use anyhow:: { anyhow, Context , Error , Result } ;
@@ -35,6 +32,7 @@ use log::{debug, error, warn};
3532use thread_priority:: * ;
3633
3734use crate :: measurement:: { Measurement , Timestamp } ;
35+ use crate :: watched_tasks:: WatchedTasksBuilder ;
3836
3937struct ChannelDesc {
4038 kernel_name : & ' static str ,
@@ -255,7 +253,6 @@ pub struct IioThread {
255253 ref_instant : Instant ,
256254 timestamp : AtomicU64 ,
257255 values : Vec < AtomicU16 > ,
258- join : Mutex < Option < JoinHandle < ( ) > > > ,
259256 channel_descs : & ' static [ ChannelDesc ] ,
260257}
261258
@@ -325,7 +322,8 @@ impl IioThread {
325322 }
326323
327324 async fn new (
328- thread_name : & str ,
325+ wtb : & mut WatchedTasksBuilder ,
326+ thread_name : & ' static str ,
329327 adc_name : & ' static str ,
330328 trigger_name : & ' static str ,
331329 sample_rate : i64 ,
@@ -342,9 +340,8 @@ impl IioThread {
342340 let ( thread_res_tx, thread_res_rx) = bounded ( 1 ) ;
343341
344342 // Spawn a high priority thread that updates the atomic values in `thread`.
345- let join = thread:: Builder :: new ( )
346- . name ( format ! ( "tacd {thread_name} iio" ) )
347- . spawn ( move || {
343+ wtb. spawn_thread ( thread_name, move || {
344+ {
348345 let adc_setup_res = Self :: adc_setup (
349346 adc_name,
350347 trigger_name,
@@ -358,17 +355,15 @@ impl IioThread {
358355 ref_instant : Instant :: now ( ) ,
359356 timestamp : AtomicU64 :: new ( TIMESTAMP_ERROR ) ,
360357 values : channels. iter ( ) . map ( |_| AtomicU16 :: new ( 0 ) ) . collect ( ) ,
361- join : Mutex :: new ( None ) ,
362358 channel_descs,
363359 } ) ;
364-
365360 ( thread, channels, buf)
366361 }
367362 Err ( e) => {
368363 // Can not fail in practice as the queue is known to be empty
369364 // at this point.
370365 thread_res_tx. try_send ( Err ( e) ) . unwrap ( ) ;
371- return ;
366+ return Ok ( ( ) ) ;
372367 }
373368 } ;
374369
@@ -423,21 +418,20 @@ impl IioThread {
423418 tx. try_send ( Ok ( content) ) . unwrap ( ) ;
424419 }
425420 }
426- } ) ? ;
421+ } ;
427422
428- let thread = thread_res_rx. recv ( ) . await ??;
423+ Ok ( ( ) )
424+ } ) ?;
429425
430- // Locking the Mutex could only fail if the Mutex was poisoned by
431- // a thread that held the lock and panicked.
432- // At this point the Mutex has not yet been locked in another thread.
433- * thread. join . lock ( ) . unwrap ( ) = Some ( join) ;
426+ let thread = thread_res_rx. recv ( ) . await ??;
434427
435428 Ok ( thread)
436429 }
437430
438- pub async fn new_stm32 ( ) -> Result < Arc < Self > > {
431+ pub async fn new_stm32 ( wtb : & mut WatchedTasksBuilder ) -> Result < Arc < Self > > {
439432 Self :: new (
440- "stm32" ,
433+ wtb,
434+ "adc-stm32" ,
441435 "48003000.adc:adc@0" ,
442436 "tim4_trgo" ,
443437 80 ,
@@ -447,14 +441,23 @@ impl IioThread {
447441 . await
448442 }
449443
450- pub async fn new_powerboard ( ) -> Result < Arc < Self > > {
444+ pub async fn new_powerboard ( wtb : & mut WatchedTasksBuilder ) -> Result < Arc < Self > > {
451445 let hr_trigger_path = Path :: new ( TRIGGER_HR_PWR_DIR ) ;
452446
453447 if !hr_trigger_path. is_dir ( ) {
454448 create_dir ( hr_trigger_path) ?;
455449 }
456450
457- Self :: new ( "powerboard" , "lmp92064" , "tacd-pwr" , 20 , CHANNELS_PWR , 1 ) . await
451+ Self :: new (
452+ wtb,
453+ "adc-powerboard" ,
454+ "lmp92064" ,
455+ "tacd-pwr" ,
456+ 20 ,
457+ CHANNELS_PWR ,
458+ 1 ,
459+ )
460+ . await
458461 }
459462
460463 /// Use the channel names defined at the top of the file to get a reference
0 commit comments