|
16 | 16 | // under the License.
|
17 | 17 |
|
18 | 18 | use compio::buf::IoBuf;
|
| 19 | +use compio::runtime::RuntimeBuilder; |
| 20 | +use futures::channel::mpsc::SendError; |
| 21 | +use futures::channel::{mpsc, oneshot}; |
| 22 | +use futures::future::LocalBoxFuture; |
| 23 | +use futures::{SinkExt, StreamExt}; |
| 24 | +use std::future::Future; |
| 25 | +use std::thread::JoinHandle; |
19 | 26 |
|
20 | 27 | use crate::Buffer;
|
21 | 28 |
|
| 29 | +/// This is arbitrary, but since all tasks are spawned instantly, we shouldn't need a too big buffer. |
| 30 | +const CHANNEL_SIZE: usize = 4; |
| 31 | + |
| 32 | +fn task<F, Fut, T>(func: F) -> (Task<T>, SpawnTask) |
| 33 | +where |
| 34 | + F: (FnOnce() -> Fut) + Send + 'static, |
| 35 | + Fut: Future<Output = T>, |
| 36 | + T: Send + 'static, |
| 37 | +{ |
| 38 | + let (tx, recv) = oneshot::channel(); |
| 39 | + |
| 40 | + let boxed = Box::new(|| { |
| 41 | + Box::pin(async move { |
| 42 | + let res = func().await; |
| 43 | + tx.send(res).ok(); |
| 44 | + }) as _ |
| 45 | + }); |
| 46 | + |
| 47 | + (Task(recv), SpawnTask(boxed)) |
| 48 | +} |
| 49 | + |
| 50 | +/// A task handle that can be used to retrieve result spawned into [`CompioThread`]. |
| 51 | +pub struct Task<T>(oneshot::Receiver<T>); |
| 52 | + |
| 53 | +/// Type erased task that can be spawned into a [`CompioThread`]. |
| 54 | +struct SpawnTask(Box<dyn (FnOnce() -> LocalBoxFuture<'static, ()>) + Send>); |
| 55 | + |
| 56 | +impl SpawnTask { |
| 57 | + fn call(self) -> LocalBoxFuture<'static, ()> { |
| 58 | + (self.0)() |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +#[derive(Debug)] |
| 63 | +pub struct CompioThread { |
| 64 | + thread: JoinHandle<()>, |
| 65 | + handle: SpawnHandle, |
| 66 | +} |
| 67 | + |
| 68 | +impl CompioThread { |
| 69 | + pub fn new(builder: RuntimeBuilder) -> Self { |
| 70 | + let (send, mut recv) = mpsc::channel(CHANNEL_SIZE); |
| 71 | + let handle = SpawnHandle(send); |
| 72 | + let thread = std::thread::spawn(move || { |
| 73 | + let rt = builder.build().expect("failed to create runtime"); |
| 74 | + rt.block_on(async { |
| 75 | + while let Some(task) = recv.next().await { |
| 76 | + rt.spawn(task.call()).detach(); |
| 77 | + } |
| 78 | + }); |
| 79 | + }); |
| 80 | + Self { thread, handle } |
| 81 | + } |
| 82 | + |
| 83 | + pub async fn spawn<F, Fut, T>(&self, func: F) -> Result<Task<T>, SendError> |
| 84 | + where |
| 85 | + F: (FnOnce() -> Fut) + Send + 'static, |
| 86 | + Fut: Future<Output = T>, |
| 87 | + T: Send + 'static, |
| 88 | + { |
| 89 | + self.handle.clone().spawn(func).await |
| 90 | + } |
| 91 | + |
| 92 | + pub fn handle(&self) -> SpawnHandle { |
| 93 | + self.handle.clone() |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +#[derive(Debug, Clone)] |
| 98 | +pub struct SpawnHandle(mpsc::Sender<SpawnTask>); |
| 99 | + |
| 100 | +impl SpawnHandle { |
| 101 | + pub async fn spawn<F, Fut, T>(&mut self, func: F) -> Result<Task<T>, SendError> |
| 102 | + where |
| 103 | + F: (FnOnce() -> Fut) + Send + 'static, |
| 104 | + Fut: Future<Output = T>, |
| 105 | + T: Send + 'static, |
| 106 | + { |
| 107 | + let (task, spawn) = task(func); |
| 108 | + self.0.send(spawn).await?; |
| 109 | + Ok(task) |
| 110 | + } |
| 111 | +} |
| 112 | + |
22 | 113 | unsafe impl IoBuf for Buffer {
|
23 | 114 | fn as_buf_ptr(&self) -> *const u8 {
|
24 | 115 | self.current().as_ptr()
|
|
0 commit comments