diff --git a/src/runtime.rs b/src/runtime.rs index d3bb618..ca758f8 100644 --- a/src/runtime.rs +++ b/src/runtime.rs @@ -24,8 +24,40 @@ pub struct RuntimeHandle { blocking: Executor, } +pub struct RuntimeBuilder { + executor_count: usize, + blocking_count: usize, +} + struct RuntimeToken; +impl RuntimeBuilder { + pub fn new() -> Self { + let executor_count = std::thread::available_parallelism() + .map(usize::from) + .unwrap_or(1); + + Self { + executor_count, + blocking_count: executor_count * 5, + } + } + + pub fn executor_count(mut self, count: usize) -> Self { + self.executor_count = count; + self + } + + pub fn blocking_count(mut self, count: usize) -> Self { + self.blocking_count = count; + self + } + + pub fn build(self) -> Runtime { + RuntimeState::create(self) + } +} + impl RuntimeHandle { pub fn current() -> Self { RUNTIME.with(|runtime| { @@ -93,18 +125,12 @@ impl RuntimeState { RUNTIME.with(|runtime| runtime.borrow_mut().take()) } - fn create() -> Runtime { + fn create(builder: RuntimeBuilder) -> Runtime { let executor = Executor::new(); let blocking = Executor::new(); - let base_threads = std::thread::available_parallelism() - .map(usize::from) - .unwrap_or(1); - - let blocking_threads = base_threads * 5; - let mut thread_handles = Vec::new(); - for _ in 0..base_threads { + for _ in 0..builder.executor_count { let executor = executor.clone(); let blocking = blocking.clone(); @@ -117,7 +143,7 @@ impl RuntimeState { })); } - for _ in 0..blocking_threads { + for _ in 0..builder.blocking_count { let executor = executor.clone(); let blocking = blocking.clone(); @@ -139,7 +165,7 @@ impl RuntimeState { impl Runtime { pub fn new() -> Self { - RuntimeState::create() + RuntimeState::create(RuntimeBuilder::new()) } pub fn handle(&self) -> &RuntimeHandle { @@ -172,6 +198,12 @@ impl Runtime { } } +impl Default for RuntimeBuilder { + fn default() -> Self { + Self::new() + } +} + impl Default for Runtime { fn default() -> Self { Self::new()