]> git.proxmox.com Git - rustc.git/blame - vendor/rustc-rayon-core/src/scope/internal.rs
New upstream version 1.51.0+dfsg1
[rustc.git] / vendor / rustc-rayon-core / src / scope / internal.rs
CommitLineData
2c00a5a8
XL
1#![cfg(rayon_unstable)]
2
e74abb32 3use super::{Scope, ScopeBase};
532ac7d7 4use internal::task::{ScopeHandle, Task, ToScopeHandle};
2c00a5a8
XL
5use std::any::Any;
6use std::mem;
7use std::sync::Arc;
2c00a5a8
XL
8
9impl<'scope> ToScopeHandle<'scope> for Scope<'scope> {
10 type ScopeHandle = LocalScopeHandle<'scope>;
11
12 fn to_scope_handle(&self) -> Self::ScopeHandle {
13 unsafe { LocalScopeHandle::new(self) }
14 }
15}
16
17#[derive(Debug)]
18pub struct LocalScopeHandle<'scope> {
e74abb32 19 scope: *const ScopeBase<'scope>,
2c00a5a8
XL
20}
21
22impl<'scope> LocalScopeHandle<'scope> {
23 /// Caller guarantees that `*scope` will remain valid
24 /// until the scope completes. Since we acquire a ref,
25 /// that means it will remain valid until we release it.
26 unsafe fn new(scope: &Scope<'scope>) -> Self {
e74abb32
XL
27 scope.base.increment();
28 LocalScopeHandle { scope: &scope.base }
2c00a5a8
XL
29 }
30}
31
32impl<'scope> Drop for LocalScopeHandle<'scope> {
33 fn drop(&mut self) {
34 unsafe {
35 if !self.scope.is_null() {
36 (*self.scope).job_completed_ok();
37 }
38 }
39 }
40}
41
42/// We assert that the `Self` type remains valid until a
43/// method is called, and that `'scope` will not end until
44/// that point.
45unsafe impl<'scope> ScopeHandle<'scope> for LocalScopeHandle<'scope> {
46 unsafe fn spawn_task<T: Task + 'scope>(&self, task: Arc<T>) {
47 let scope = &*self.scope;
48 scope.registry.submit_task(task);
49 }
50
51 fn ok(self) {
52 mem::drop(self);
53 }
54
55 fn panicked(self, err: Box<Any + Send>) {
56 unsafe {
57 (*self.scope).job_panicked(err);
58 mem::forget(self); // no need to run dtor now
59 }
60 }
61}