]>
Commit | Line | Data |
---|---|---|
2c00a5a8 XL |
1 | #![cfg(rayon_unstable)] |
2 | ||
e74abb32 | 3 | use super::{Scope, ScopeBase}; |
532ac7d7 | 4 | use internal::task::{ScopeHandle, Task, ToScopeHandle}; |
2c00a5a8 XL |
5 | use std::any::Any; |
6 | use std::mem; | |
7 | use std::sync::Arc; | |
2c00a5a8 XL |
8 | |
9 | impl<'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)] | |
18 | pub struct LocalScopeHandle<'scope> { | |
e74abb32 | 19 | scope: *const ScopeBase<'scope>, |
2c00a5a8 XL |
20 | } |
21 | ||
22 | impl<'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 | ||
32 | impl<'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. | |
45 | unsafe 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 | } |