mirror of https://github.com/stjepang/smol
Formatting
This commit is contained in:
parent
c041d946eb
commit
6de2ffabbb
27
src/lib.rs
27
src/lib.rs
|
@ -77,13 +77,14 @@ pub mod prelude {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Spawns a task onto the single-threaded global executor.
|
/// Spawns a task onto the global executor (single-threaded by default).
|
||||||
///
|
///
|
||||||
/// There is a (single-threaded by default) global executor that gets lazily initialized on first use.
|
/// There is a global executor that gets lazily initialized on first use. It is included in this
|
||||||
/// It is advisable to use it in tests or small programs, but it is otherwise a better idea to define
|
/// library for convenience when writing unit tests and small programs, but it is otherwise
|
||||||
/// your own [`Executor`]s.
|
/// more advisable to create your own [`Executor`].
|
||||||
/// You can configure the number of threads of the global executor using the `SMOL_THREADS` environment
|
///
|
||||||
/// variable.
|
/// By default, the global executor is run by a single background thread, but you can also
|
||||||
|
/// configure the number of threads by setting the `SMOL_THREADS` environment variable.
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
|
@ -99,22 +100,28 @@ pub mod prelude {
|
||||||
pub fn spawn<T: Send + 'static>(future: impl Future<Output = T> + Send + 'static) -> Task<T> {
|
pub fn spawn<T: Send + 'static>(future: impl Future<Output = T> + Send + 'static) -> Task<T> {
|
||||||
static GLOBAL: Lazy<Executor> = Lazy::new(|| {
|
static GLOBAL: Lazy<Executor> = Lazy::new(|| {
|
||||||
let num_threads = {
|
let num_threads = {
|
||||||
// Parse SMOL_THREADS or run a monothreaded executor.
|
// Parse SMOL_THREADS or default to 1.
|
||||||
std::env::var("SMOL_THREADS")
|
std::env::var("SMOL_THREADS")
|
||||||
.ok()
|
.ok()
|
||||||
.and_then(|s| s.parse().ok())
|
.and_then(|s| s.parse().ok())
|
||||||
.unwrap_or(1)
|
.unwrap_or(1)
|
||||||
};
|
};
|
||||||
|
|
||||||
for n in 1..=num_threads {
|
for n in 1..=num_threads {
|
||||||
thread::Builder::new()
|
thread::Builder::new()
|
||||||
.name(format!("smol-{}", n))
|
.name(format!("smol-{}", n))
|
||||||
.spawn(|| loop {
|
.spawn(|| {
|
||||||
let _ =
|
loop {
|
||||||
catch_unwind(|| async_io::block_on(GLOBAL.run(future::pending::<()>())));
|
let _ = catch_unwind(|| {
|
||||||
|
async_io::block_on(GLOBAL.run(future::pending::<()>()))
|
||||||
|
});
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.expect("cannot spawn executor thread");
|
.expect("cannot spawn executor thread");
|
||||||
}
|
}
|
||||||
|
|
||||||
Executor::new()
|
Executor::new()
|
||||||
});
|
});
|
||||||
|
|
||||||
GLOBAL.spawn(future)
|
GLOBAL.spawn(future)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue