#![cfg(rayon_unstable)]
use internal::task::{ScopeHandle, ToScopeHandle, Task};
use registry::Registry;
use std::any::Any;
use std::fmt;
use std::sync::Arc;
use super::ThreadPool;
impl ToScopeHandle<'static> for ThreadPool {
type ScopeHandle = ThreadPoolScopeHandle;
fn to_scope_handle(&self) -> Self::ScopeHandle {
unsafe { ThreadPoolScopeHandle::new(self.registry.clone()) }
}
}
pub struct ThreadPoolScopeHandle {
registry: Arc<Registry>
}
impl fmt::Debug for ThreadPoolScopeHandle {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("ThreadPoolScopeHandle")
.field("pool", &self.registry.id())
.finish()
}
}
impl ThreadPoolScopeHandle {
unsafe fn new(registry: Arc<Registry>) -> Self {
registry.increment_terminate_count();
ThreadPoolScopeHandle { registry: registry }
}
}
impl Drop for ThreadPoolScopeHandle {
fn drop(&mut self) {
self.registry.terminate();
}
}
unsafe impl ScopeHandle<'static> for ThreadPoolScopeHandle {
unsafe fn spawn_task<T: Task + 'static>(&self, task: Arc<T>) {
self.registry.submit_task(task);
}
fn ok(self) {
}
fn panicked(self, err: Box<Any + Send>) {
self.registry.handle_panic(err);
}
}