👕 refactor

main
_ 2023-09-09 10:39:42 -05:00
parent 6ef12c5859
commit d93b3036bb
1 changed files with 21 additions and 8 deletions

View File

@ -1,5 +1,6 @@
use std::{ use std::{
fmt, fmt,
marker::Send,
sync::mpsc, sync::mpsc,
thread, thread,
time::Duration, time::Duration,
@ -26,6 +27,19 @@ struct ThreadSystem <'a>
send: mpsc::SyncSender <TaskOutput>, send: mpsc::SyncSender <TaskOutput>,
} }
impl <'a> ThreadSystem <'a>
{
fn dispatch <F: FnOnce () -> TaskOutput + Send + 'static> (&self, work: F)
{
let send = self.send.clone ();
self.pool.spawn (move ||
{
let output = work ();
send.send (output).ok ();
});
}
}
struct Driver <'a> struct Driver <'a>
{ {
thread_sys: ThreadSystem <'a>, thread_sys: ThreadSystem <'a>,
@ -124,6 +138,7 @@ impl <'a> Driver <'_>
let dur_encode = Duration::from_millis (20); let dur_encode = Duration::from_millis (20);
let dur_transmit = Duration::from_millis (200); let dur_transmit = Duration::from_millis (200);
let thread_sys = &self.thread_sys;
let pool = &self.thread_sys.pool; let pool = &self.thread_sys.pool;
let send = self.thread_sys.send.clone (); let send = self.thread_sys.send.clone ();
@ -131,15 +146,14 @@ impl <'a> Driver <'_>
{ {
TxPipelineEvent::Capture => { TxPipelineEvent::Capture => {
let cap = self.capture.start (())?; let cap = self.capture.start (())?;
thread_sys.dispatch (move ||
pool.spawn (move ||
{ {
thread::sleep (dur_capture); thread::sleep (dur_capture);
let buf_raw = BufRaw let buf_raw = BufRaw
{ {
buf: vec![], buf: vec![],
}; };
send.send (TaskOutput::TxCapture ((cap, buf_raw))).unwrap (); TaskOutput::TxCapture ((cap, buf_raw))
}); });
}, },
TxPipelineEvent::PollEncoder => TxPipelineEvent::PollEncoder =>
@ -161,20 +175,19 @@ impl <'a> Driver <'_>
TxPipelineEvent::Encode (buf_raw) => TxPipelineEvent::Encode (buf_raw) =>
{ {
let mut encoder = self.encoder.start (EncoderTaskMetadata {})?; let mut encoder = self.encoder.start (EncoderTaskMetadata {})?;
thread_sys.dispatch (move ||
pool.spawn (move ||
{ {
thread::sleep (dur_encode); thread::sleep (dur_encode);
encoder.try_handle_insert_data (&buf_raw.buf).unwrap (); encoder.try_handle_insert_data (&buf_raw.buf).unwrap ();
send.send (TaskOutput::TxEncode (encoder)).unwrap (); TaskOutput::TxEncode (encoder)
}); });
}, },
TxPipelineEvent::Transmit (_buf_enc) => { TxPipelineEvent::Transmit (_buf_enc) => {
let tx = self.transmitter.start (())?; let tx = self.transmitter.start (())?;
pool.spawn (move || thread_sys.dispatch (move ||
{ {
thread::sleep (dur_transmit); thread::sleep (dur_transmit);
send.send (TaskOutput::TxTransmit ((tx, false))).unwrap (); TaskOutput::TxTransmit ((tx, false))
}); });
}, },
} }