Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Because we fork without execing a lot, we often need to reset things that have references to background threads. This provides a handy wrapper for doing so. If this looks good, I will also apply it to the process_execution::remote::CommandRunner. I can also apply it to the ResettablePool which we already have, for re-use (it would add an additional Arc clone per CpuPool operation, but this is probably fine because the CpuPool operations are already by definition somewhat heavyweight).
- Loading branch information
1 parent
c137fc8
commit 29bdaa6
Showing
12 changed files
with
158 additions
and
29 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[package] | ||
name = "resettable" | ||
version = "0.0.1" | ||
authors = [ "Pants Build <pantsbuild@gmail.com>" ] | ||
|
||
[dependencies] |
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,56 @@ | ||
use std::sync::{Arc, RwLock}; | ||
|
||
/// | ||
/// Resettable is a lazily computed value which can be reset, so that it can be lazily computed | ||
/// again next time it is needed. | ||
/// | ||
/// This is useful because we fork without execing a lot in the engine, and before doing so, we need | ||
/// to reset any references which hide background threads, so that forked processes don't inherit | ||
/// pointers to threads from the parent process which will not exist in the forked process. | ||
/// | ||
#[derive(Clone)] | ||
pub struct Resettable<T> { | ||
val: Arc<RwLock<Option<T>>>, | ||
make: Arc<Fn() -> T>, | ||
} | ||
|
||
unsafe impl<T> Send for Resettable<T> {} | ||
unsafe impl<T> Sync for Resettable<T> {} | ||
|
||
impl<T> Resettable<T> | ||
where | ||
T: Clone + Send + Sync, | ||
{ | ||
// Sadly there is no way to accept an Fn() -> T because it's not Sized, so we need to accept an | ||
// Arc of one. This is not at all ergonomic, but at some point "impl trait" will come along and | ||
// allow us to remove this monstrosity. | ||
pub fn new(make: Arc<Fn() -> T>) -> Resettable<T> { | ||
Resettable { | ||
val: Arc::new(RwLock::new(None)), | ||
make: make, | ||
} | ||
} | ||
|
||
pub fn get(&self) -> T { | ||
{ | ||
if let Some(ref val) = *self.val.read().unwrap() { | ||
return val.clone(); | ||
} | ||
} | ||
{ | ||
let mut maybe_val = self.val.write().unwrap(); | ||
{ | ||
if let Some(ref val) = *maybe_val { | ||
return val.clone(); | ||
} | ||
} | ||
let val = (self.make)(); | ||
*maybe_val = Some(val.clone()); | ||
val | ||
} | ||
} | ||
|
||
pub fn reset(&self) { | ||
*self.val.write().unwrap() = None | ||
} | ||
} |
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