I think I double taped this thing

This commit is contained in:
Quaternions 2023-10-04 03:33:06 -07:00
parent f320520292
commit 77d060b90c
2 changed files with 33 additions and 22 deletions

View File

@ -34,7 +34,7 @@ pub enum InputInstruction {
//for interpolation / networking / playback reasons, most playback heads will always want //for interpolation / networking / playback reasons, most playback heads will always want
//to be 1 instruction ahead to generate the next state for interpolation. //to be 1 instruction ahead to generate the next state for interpolation.
} }
#[derive(Clone,Debug)]
pub struct Body { pub struct Body {
position: glam::Vec3,//I64 where 2^32 = 1 u position: glam::Vec3,//I64 where 2^32 = 1 u
velocity: glam::Vec3,//I64 where 2^32 = 1 u/s velocity: glam::Vec3,//I64 where 2^32 = 1 u/s

View File

@ -2,35 +2,34 @@ use std::thread;
use std::sync::{mpsc,Arc}; use std::sync::{mpsc,Arc};
use parking_lot::{Mutex,Condvar}; use parking_lot::{Mutex,Condvar};
struct Worker<Task:Send> { //The goal here is to have a worker thread that parks itself when it runs out of work.
//The worker thread publishes the result of its work back to the worker object for every item in the work queue.
//The physics (target use case) knows when it has not changed the body, so not updating the value is also an option.
struct Worker<Task:Send,Value:Clone> {
sender: mpsc::Sender<Task>, sender: mpsc::Sender<Task>,
receiver: Arc<(Mutex<mpsc::Receiver<Task>>,Condvar)>, receiver: Arc<(Mutex<mpsc::Receiver<Task>>,Condvar)>,
value:Arc<Mutex<Value>>,
} }
impl<Task:Send+'static> Worker<Task> { impl<Task:Send+'static,Value:Clone+Send+'static> Worker<Task,Value> {
fn new() -> Self { fn new<F:Fn(Task)->Value+Send+'static>(value:Value,f:F) -> Self {
let (sender, receiver) = mpsc::channel::<Task>(); let (sender, receiver) = mpsc::channel::<Task>();
Self { let ret=Self {
sender, sender,
receiver:Arc::new((Mutex::new(receiver),Condvar::new())), receiver:Arc::new((Mutex::new(receiver),Condvar::new())),
} value:Arc::new(Mutex::new(value)),
} };
let receiver=ret.receiver.clone();
fn send(&self,task:Task)->Result<(), mpsc::SendError<Task>>{ let value=ret.value.clone();
let ret=self.sender.send(task);
self.receiver.1.notify_one();
ret
}
fn start(&self) {
let receiver=self.receiver.clone();
thread::spawn(move || { thread::spawn(move || {
loop{ loop{
loop { loop {
match receiver.0.lock().recv() { match receiver.0.lock().recv() {
Ok(_task) => { Ok(task) => {
println!("Worker got a task"); println!("Worker got a task");
// Process the task // Process the task
*value.lock()=f(task);
} }
Err(_) => { Err(_) => {
println!("Worker stopping.",); println!("Worker stopping.",);
@ -41,17 +40,27 @@ impl<Task:Send+'static> Worker<Task> {
receiver.1.wait(&mut receiver.0.lock()); receiver.1.wait(&mut receiver.0.lock());
} }
}); });
ret
}
fn send(&self,task:Task)->Result<(), mpsc::SendError<Task>>{
let ret=self.sender.send(task);
self.receiver.1.notify_one();
ret
}
fn grab_clone(&self)->Value{
self.value.lock().clone()
} }
} }
#[test] #[test]
fn test_worker() { fn test_worker() {
println!("hiiiii");
// Create the worker thread // Create the worker thread
let worker = Worker::new(); let worker = Worker::new(crate::body::Body::with_pva(glam::Vec3::ZERO,glam::Vec3::ZERO,glam::Vec3::ZERO),
|_|crate::body::Body::with_pva(glam::Vec3::ONE,glam::Vec3::ONE,glam::Vec3::ONE)
// Start the worker thread );
worker.start();
// Send tasks to the worker // Send tasks to the worker
for i in 0..5 { for i in 0..5 {
@ -74,4 +83,6 @@ fn test_worker() {
instruction:crate::body::PhysicsInstruction::StrafeTick, instruction:crate::body::PhysicsInstruction::StrafeTick,
}; };
worker.send(task).unwrap(); worker.send(task).unwrap();
println!("value={:?}",worker.grab_clone());
} }