summaryrefslogtreecommitdiffstats
path: root/toolkit/crashreporter/client/app/src/std/thread.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-15 03:34:50 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-15 03:34:50 +0000
commitdef92d1b8e9d373e2f6f27c366d578d97d8960c6 (patch)
tree2ef34b9ad8bb9a9220e05d60352558b15f513894 /toolkit/crashreporter/client/app/src/std/thread.rs
parentAdding debian version 125.0.3-1. (diff)
downloadfirefox-def92d1b8e9d373e2f6f27c366d578d97d8960c6.tar.xz
firefox-def92d1b8e9d373e2f6f27c366d578d97d8960c6.zip
Merging upstream version 126.0.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
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 }))
+}