-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): ✨ added single-threaded mode for the CLI
This can be enabled by setting `PERSEUS_CLI_SEQUENTIAL`. Closes #11.
- Loading branch information
1 parent
379d549
commit 5cb465a
Showing
5 changed files
with
75 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,7 @@ pub mod errors; | |
mod help; | ||
mod prepare; | ||
mod serve; | ||
mod thread; | ||
|
||
mod extraction; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
use std::env; | ||
use std::thread::{self, JoinHandle}; | ||
|
||
/// Spawns a new thread with the given code, or executes it directly if the environment variable `PERSEUS_CLI_SEQUENTIAL` is set to | ||
/// any valid (Unicode) value. Multithreading is the default. | ||
pub fn spawn_thread<F, T>(f: F) -> ThreadHandle<F, T> | ||
where | ||
F: FnOnce() -> T, | ||
F: Send + 'static, | ||
T: Send + 'static, | ||
{ | ||
let single = env::var("PERSEUS_CLI_SEQUENTIAL").is_ok(); | ||
if single { | ||
ThreadHandle { | ||
join_handle: None, | ||
f: Some(f), | ||
} | ||
} else { | ||
let join_handle = thread::spawn(f); | ||
ThreadHandle { | ||
join_handle: Some(join_handle), | ||
f: None, | ||
} | ||
} | ||
} | ||
|
||
/// An abstraction over a `JoinHandle` in a multithreaded case, or just a similar interface that will immediately return if otherwise. | ||
/// This allows the interfaces for multithreading and single-threading to be basically identical. | ||
pub struct ThreadHandle<F, T> | ||
where | ||
F: FnOnce() -> T, | ||
F: Send + 'static, | ||
T: Send + 'static, | ||
{ | ||
/// If multithreaded, this is the join handle. | ||
join_handle: Option<JoinHandle<T>>, | ||
// If single-threaded, this is the output (it's already been executed). | ||
f: Option<F>, | ||
} | ||
impl<F, T> ThreadHandle<F, T> | ||
where | ||
F: FnOnce() -> T, | ||
F: Send + 'static, | ||
T: Send + 'static, | ||
{ | ||
/// Waits for the 'thread' to complete, properly if it's multithreaded, or by direct execution if it's single-threaded. | ||
pub fn join( | ||
self, | ||
) -> Result<T, std::boxed::Box<(dyn std::any::Any + std::marker::Send + 'static)>> { | ||
if let Some(join_handle) = self.join_handle { | ||
join_handle.join() | ||
} else if let Some(f) = self.f { | ||
let output = f(); | ||
Ok(output) | ||
} else { | ||
unreachable!(); | ||
} | ||
} | ||
} |