Add RuntimeBuilder for customizing runtime
This commit is contained in:
parent
5220c16bd1
commit
c70f6e8f9c
|
@ -24,8 +24,40 @@ pub struct RuntimeHandle {
|
||||||
blocking: Executor,
|
blocking: Executor,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct RuntimeBuilder {
|
||||||
|
executor_count: usize,
|
||||||
|
blocking_count: usize,
|
||||||
|
}
|
||||||
|
|
||||||
struct RuntimeToken;
|
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 {
|
impl RuntimeHandle {
|
||||||
pub fn current() -> Self {
|
pub fn current() -> Self {
|
||||||
RUNTIME.with(|runtime| {
|
RUNTIME.with(|runtime| {
|
||||||
|
@ -93,18 +125,12 @@ impl RuntimeState {
|
||||||
RUNTIME.with(|runtime| runtime.borrow_mut().take())
|
RUNTIME.with(|runtime| runtime.borrow_mut().take())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create() -> Runtime {
|
fn create(builder: RuntimeBuilder) -> Runtime {
|
||||||
let executor = Executor::new();
|
let executor = Executor::new();
|
||||||
let blocking = 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();
|
let mut thread_handles = Vec::new();
|
||||||
for _ in 0..base_threads {
|
for _ in 0..builder.executor_count {
|
||||||
let executor = executor.clone();
|
let executor = executor.clone();
|
||||||
let blocking = blocking.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 executor = executor.clone();
|
||||||
let blocking = blocking.clone();
|
let blocking = blocking.clone();
|
||||||
|
|
||||||
|
@ -139,7 +165,7 @@ impl RuntimeState {
|
||||||
|
|
||||||
impl Runtime {
|
impl Runtime {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
RuntimeState::create()
|
RuntimeState::create(RuntimeBuilder::new())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn handle(&self) -> &RuntimeHandle {
|
pub fn handle(&self) -> &RuntimeHandle {
|
||||||
|
@ -172,6 +198,12 @@ impl Runtime {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Default for RuntimeBuilder {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Default for Runtime {
|
impl Default for Runtime {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self::new()
|
Self::new()
|
||||||
|
|
Loading…
Reference in a new issue