Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2019-10-24 15:39:24 +02:00
parent 34396ad7f3
commit 4b949e6e52
2 changed files with 17 additions and 9 deletions

View File

@ -1,7 +1,6 @@
use std::mem::MaybeUninit;
use std::ptr;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
pub struct Ring<T> {
head: usize,
@ -11,7 +10,7 @@ pub struct Ring<T> {
}
impl<T> Ring<T> {
pub fn new(size: usize) -> Arc<Self> {
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<T> Ring<T> {
data.push(MaybeUninit::uninit())
}
Arc::new(Self {
Self {
head: 0,
tail: 0,
mask: size - 1,
data: data.into_boxed_slice(),
})
}
}
#[inline]

View File

@ -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<dyn Future<Output = ()> + Send + 'static>;
type TaskId = usize;
pub struct ThreadPool {
inner: Arc<ThreadPoolInner>,
@ -25,6 +27,7 @@ pub struct Thread {
pub struct ThreadInner {
id: usize,
ring: Ring<TaskId>,
}
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<ThreadPoolInner>, 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);