From 4b949e6e52a96d4172b314e4b44e49c0d931efe4 Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Thu, 24 Oct 2019 15:39:24 +0200 Subject: [PATCH] foo Signed-off-by: Wolfgang Bumiller --- src/executor/ring.rs | 7 +++---- src/executor/thread_pool.rs | 19 ++++++++++++++----- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/executor/ring.rs b/src/executor/ring.rs index 50bae43..0fe3e22 100644 --- a/src/executor/ring.rs +++ b/src/executor/ring.rs @@ -1,7 +1,6 @@ use std::mem::MaybeUninit; use std::ptr; use std::sync::atomic::{AtomicUsize, Ordering}; -use std::sync::Arc; pub struct Ring { head: usize, @@ -11,7 +10,7 @@ pub struct Ring { } impl Ring { - pub fn new(size: usize) -> Arc { + pub fn new(size: usize) -> Self { if size < 2 || size.count_ones() != 1 { panic!("Ring size must be a power of two!"); } @@ -21,12 +20,12 @@ impl Ring { data.push(MaybeUninit::uninit()) } - Arc::new(Self { + Self { head: 0, tail: 0, mask: size - 1, data: data.into_boxed_slice(), - }) + } } #[inline] diff --git a/src/executor/thread_pool.rs b/src/executor/thread_pool.rs index 823191e..67c228a 100644 --- a/src/executor/thread_pool.rs +++ b/src/executor/thread_pool.rs @@ -4,9 +4,11 @@ use std::sync::{mpsc, Arc, Mutex}; use std::thread::JoinHandle; use super::num_cpus; +use super::ring::Ring; use super::slot_list::SlotList; type BoxFut = Box + Send + 'static>; +type TaskId = usize; pub struct ThreadPool { inner: Arc, @@ -25,6 +27,7 @@ pub struct Thread { pub struct ThreadInner { id: usize, + ring: Ring, } pub struct Work {} @@ -57,12 +60,15 @@ impl ThreadPool { } impl ThreadPoolInner { - fn spawn(&self, future: BoxFut) { - let mut tasks = self.tasks.lock().unwrap(); - self.queue_task(tasks.add(future)); + fn create_task(&self, future: BoxFut) -> TaskId { + self.tasks.lock().unwrap().add(future) } - fn queue_task(&self, task: usize) { + fn spawn(&self, future: BoxFut) { + self.queue_task(self.create_task(future)) + } + + fn queue_task(&self, task: TaskId) { let threads = self.threads.lock().unwrap(); //let shortest = threads // .iter() @@ -75,7 +81,10 @@ impl Thread { fn new(pool: Arc, id: usize) -> Self { let (queue_sender, queue_receiver) = mpsc::channel(); - let inner = Arc::new(ThreadInner { id }); + let inner = Arc::new(ThreadInner { + id, + ring: Ring::new(32), + }); let handle = std::thread::spawn({ let inner = Arc::clone(&inner);