summaryrefslogtreecommitdiffstats
path: root/toolkit/crashreporter/client/app/src/std/thread.rs
diff options
context:
space:
mode:
Diffstat (limited to 'toolkit/crashreporter/client/app/src/std/thread.rs')
-rw-r--r--toolkit/crashreporter/client/app/src/std/thread.rs45
1 files changed, 45 insertions, 0 deletions
diff --git a/toolkit/crashreporter/client/app/src/std/thread.rs b/toolkit/crashreporter/client/app/src/std/thread.rs
new file mode 100644
index 0000000000..d2dc74702a
--- /dev/null
+++ b/toolkit/crashreporter/client/app/src/std/thread.rs
@@ -0,0 +1,45 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+pub use std::thread::*;
+
+// Mock `spawn` just to issue a warning that mocking within the thread won't work without manual
+// intervention.
+pub fn spawn<F, T>(f: F) -> JoinHandle<T>
+where
+ F: FnOnce() -> T + Send + 'static,
+ T: Send + 'static,
+{
+ eprintln!("warning: mocking won't work in `std::thread::spawn`ed threads by default. Use `std::mock::SharedMockData` if mocking is needed and it's safe to do so.");
+ std::thread::spawn(f)
+}
+
+pub struct Scope<'scope, 'env: 'scope> {
+ mock_data: super::mock::SharedMockData,
+ scope: &'scope std::thread::Scope<'scope, 'env>,
+}
+
+impl<'scope, 'env> Scope<'scope, 'env> {
+ pub fn spawn<F, T>(&self, f: F) -> ScopedJoinHandle<'scope, T>
+ where
+ F: FnOnce() -> T + Send + 'scope,
+ T: Send + 'scope,
+ {
+ let mock_data = self.mock_data.clone();
+ self.scope.spawn(move || {
+ // # Safety
+ // `thread::scope` guarantees that the mock data will outlive the thread.
+ unsafe { mock_data.set() };
+ f()
+ })
+ }
+}
+
+pub fn scope<'env, F, T>(f: F) -> T
+where
+ F: for<'scope> FnOnce(Scope<'scope, 'env>) -> T,
+{
+ let mock_data = super::mock::SharedMockData::new();
+ std::thread::scope(|scope| f(Scope { mock_data, scope }))
+}