1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#![doc(html_root_url = "https://docs.rs/futures-cputask/0.3.0")]
//! futures-cputask allows you to turn long-running CPU tasks into a async function that runs
//! on a secondary threadpool.
//!
//! While futures-preview allows execution of tasks, it runs it on the main threadpool, which clogs up
//! one of the threads while the future is run. Running it on a secondary threadpool allows for potentially
//! higher throughput.
//!
//! This crate supports multiple threadpool implementations, namely the one found in `threadpool` and the one found in `uvth`.
//! You can add support for other threadpool implementations by using the `SyncThreadPool` trait found
//! in this crate
use std::future::Future;
use std::pin::Pin;
use parking_lot::Mutex;
use lazy_static::lazy_static;
mod impls;

/// ThreadPool trait
///
/// `SyncThreadPool` has to be implemented for any kind of underlying threadpool you want to use
/// with this crate. It defines the minimum interface required to be useful.
///
/// This crate implements this trait on the `threadpool` and `uvth` crates, if their corresponding
/// features are enabled.
pub trait SyncThreadPool {
    /// Create a new default thread pool
    ///
    /// The implementor is suggested to dynamically check the amount of processing units available
    /// and spawn that many threads for threadpool usage.
    fn new() -> Self;
    /// Creates a new thread pool with a maximum of `num_threads` threads
    ///
    /// The implementor *shall not* execute more than `num_threads` jobs at the same time, unless
    /// modified. Internal threads that manage the job threads are not counted towards that limit.
    fn with_thread_count(num_threads: usize) -> Self;
    /// Schedules a task on the threadpool
    ///
    /// As soon as the task is scheduled, this function shall return.
    fn execute<F>(&self, fun: F)
    where
        F: FnOnce() + Send + 'static;
}

/// Asynchronous threadpools
///
/// This trait specifies an interface for interacting with asynchronous threadpools, which allow you to
/// await jobs
pub trait AsyncThreadPool {
    /// Create a new default thread pool
    ///
    /// The implementor is suggested to dynamically check the amount of processing units available
    /// and spawn that many threads for threadpool usage.
    fn new() -> Self;
    /// Creates a new thread pool with a maximum of `num_threads` threads
    ///
    /// The implementor *shall not* execute more than `num_threads` jobs at the same time, unless
    /// modified. Internal threads that manage the job threads are not counted towards that limit.
    fn with_thread_count(num_threads: usize) -> Self;
    /// Schedules a task on the threadpool
    ///
    /// As soon as the task is scheduled, this function shall return a future.
    fn execute<F, T>(&self, fun: F) -> Pin<Box<dyn Future<Output = T> + Send + Sync + 'static>>
    where
        F: FnOnce() -> T + Send + 'static,
        T: Sized + Send + Sync + 'static;
}

/// Asynchronous threadpool
///
/// Wrapper around a Synchronous threadpool to provide futures support.
pub struct ThreadPool<P>
where P: SyncThreadPool + Sized
{
    inner: Mutex<P>
}

impl<P> ThreadPool<P>
where P: SyncThreadPool + Sized
{
    /// Wraps around an existing threadpool instance
    pub fn from_threadpool(pool: P) -> Self {
        Self {
            inner: Mutex::new(pool)
        }
    }
    /// Moves the underlying threadpool out
    pub fn into_inner(self) -> P {
        self.inner.into_inner()
    }
}

impl<P> AsyncThreadPool for ThreadPool<P>
where P: SyncThreadPool + Sized
{
    fn new() -> Self {
        Self::from_threadpool(P::new())
    }
    fn with_thread_count(num_threads: usize) -> Self {
        Self::from_threadpool(P::with_thread_count(num_threads))
    }
    fn execute<F, T>(&self, fun: F) -> Pin<Box<dyn Future<Output = T> + Send + Sync + 'static>>
    where
        F: FnOnce() -> T + Send + 'static,
        T: Sized + Send + Sync + 'static
    {
        use futures::prelude::*;
        use futures::channel::oneshot::channel;

        let (tx, rx) = channel();
        self.inner.lock().execute(move || {
            let retval = fun();
            tx.send(retval).ok();
        });
        Box::pin(rx.map(|v| v.unwrap()))
    }
}
#[cfg(feature = "uvth")]
lazy_static! {
    /// Global threadpool
    pub static ref GLOBAL_THREADPOOL: ThreadPool<uvth::ThreadPool> = ThreadPool::new();
}

#[cfg(all(feature = "threadpool", not(feature = "uvth")))]
lazy_static! {
    /// Global threadpool
    pub static ref GLOBAL_THREADPOOL: ThreadPool<threadpool::ThreadPool> = ThreadPool::new();
}

/// Runs a task in an asynchronous threadpool
pub fn run_task_in_pool<R, F, T>(fun: F, thread_pool: &T) -> Pin<Box<dyn Future<Output = R> + Send + Sync + 'static>>
where
    R: Sized + Send + Sync + 'static,
    F: FnOnce() -> R + Send + 'static,
    T: AsyncThreadPool
{
    thread_pool.execute(fun)
}

/// Runs a task in the global threadpool
#[cfg(any(feature = "uvth", feature = "threadpool"))]
pub fn run_task<R, F>(fun: F) -> Pin<Box<dyn Future<Output = R> + Send + Sync + 'static>>
where
    R: Sized + Send + Sync + 'static,
    F: FnOnce() -> R + Send + 'static,
{
    run_task_in_pool(fun, &*GLOBAL_THREADPOOL)
}

#[cfg(feature = "derive")]
pub use futures_cputask_derive::async_task;