Skip to content

Commit 8a41ec2

Browse files
committed
feat(dispatcher): dispatch blocking fn's
1 parent 277f551 commit 8a41ec2

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

compio-dispatcher/src/lib.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ impl Dispatcher {
123123
///
124124
/// If all threads have panicked, this method will return an error with the
125125
/// sent closure.
126-
pub fn dispatch<Fut, Fn, R>(&self, f: Fn) -> Result<oneshot::Receiver<R>, SendError<Fn>>
126+
pub fn dispatch<Fn, Fut, R>(&self, f: Fn) -> Result<oneshot::Receiver<R>, SendError<Fn>>
127127
where
128128
Fn: (FnOnce() -> Fut) + Send + 'static,
129129
Fut: Future<Output = R> + 'static,
@@ -145,6 +145,25 @@ impl Dispatcher {
145145
}
146146
}
147147

148+
/// Dispatch a blocking task to the threads.
149+
///
150+
/// Blocking pool of the dispatcher will be obtained from the proactor
151+
/// builder. So any configuration of the proactor's blocking pool will be
152+
/// applied to the dispatcher.
153+
///
154+
/// # Error
155+
///
156+
/// If all threads are busy and the thread pool is full, this method will
157+
/// return an error with the original closure. The limit can be configured
158+
/// with [`DispatcherBuilder::proactor_builder`] and
159+
/// [`ProactorBuilder::thread_pool_limit`].
160+
pub fn dispatch_blocking<Fn>(&self, f: Fn) -> Result<(), SendError<Fn>>
161+
where
162+
Fn: FnOnce() + Send + 'static,
163+
{
164+
self.pool.dispatch(f).map_err(|f| SendError(f))
165+
}
166+
148167
/// Stop the dispatcher and wait for the threads to complete. If there is a
149168
/// thread panicked, this method will resume the panic.
150169
pub async fn join(self) -> io::Result<()> {

0 commit comments

Comments
 (0)