use latch::Latch;
use std::any::Any;
use std::cell::UnsafeCell;
use std::mem;
use unwind;
enum JobResult<T> {
None,
Ok(T),
Panic(Box<Any + Send>),
}
pub trait Job {
unsafe fn execute(this: *const Self, mode: JobMode);
}
pub enum JobMode {
Execute,
Abort,
}
#[derive(Copy, Clone)]
pub struct JobRef {
pointer: *const (),
execute_fn: unsafe fn(*const (), mode: JobMode),
}
unsafe impl Send for JobRef {}
unsafe impl Sync for JobRef {}
impl JobRef {
pub unsafe fn new<T>(data: *const T) -> JobRef
where T: Job
{
let fn_ptr: unsafe fn(*const T, JobMode) = <T as Job>::execute;
let fn_ptr: unsafe fn(*const (), JobMode) = mem::transmute(fn_ptr);
let pointer = data as *const ();
JobRef { pointer: pointer, execute_fn: fn_ptr }
}
#[inline]
pub unsafe fn execute(&self, mode: JobMode) {
(self.execute_fn)(self.pointer, mode)
}
}
pub struct StackJob<L: Latch, F, R> {
pub latch: L,
func: UnsafeCell<Option<F>>,
result: UnsafeCell<JobResult<R>>,
}
impl<L: Latch, F, R> StackJob<L, F, R>
where F: FnOnce() -> R
{
pub fn new(func: F, latch: L) -> StackJob<L, F, R> {
StackJob {
latch: latch,
func: UnsafeCell::new(Some(func)),
result: UnsafeCell::new(JobResult::None),
}
}
pub unsafe fn as_job_ref(&self) -> JobRef {
JobRef::new(self)
}
pub unsafe fn run_inline(self) -> R {
self.func.into_inner().unwrap()()
}
pub unsafe fn into_result(self) -> R {
match self.result.into_inner() {
JobResult::None => unreachable!(),
JobResult::Ok(x) => x,
JobResult::Panic(x) => unwind::resume_unwinding(x),
}
}
}
impl<L: Latch, F, R> Job for StackJob<L, F, R>
where F: FnOnce() -> R
{
unsafe fn execute(this: *const Self, mode: JobMode) {
let this = &*this;
match mode {
JobMode::Execute => {
let abort = unwind::AbortIfPanic;
let func = (*this.func.get()).take().unwrap();
(*this.result.get()) = match unwind::halt_unwinding(|| func()) {
Ok(x) => JobResult::Ok(x),
Err(x) => JobResult::Panic(x),
};
this.latch.set();
mem::forget(abort);
}
JobMode::Abort => {
this.latch.set();
}
}
}
}