Skip to content

Commit

Permalink
Rename task naming to proc
Browse files Browse the repository at this point in the history
  • Loading branch information
vertexclique committed Oct 24, 2019
1 parent 36d1ace commit 7049d70
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 180 deletions.
2 changes: 1 addition & 1 deletion lightproc/examples/proc_panic.rs
Expand Up @@ -11,7 +11,7 @@ where
R: Send + 'static,
{
lazy_static! {
// A channel that holds scheduled tasks.
// A channel that holds scheduled procs.
static ref QUEUE: Sender<LightProc> = {
let (sender, receiver) = unbounded::<LightProc>();

Expand Down
16 changes: 8 additions & 8 deletions lightproc/src/lightproc.rs
Expand Up @@ -13,7 +13,7 @@ use crate::recoverable_handle::RecoverableHandle;
use std::panic::AssertUnwindSafe;

pub struct LightProc {
/// A pointer to the heap-allocated task.
/// A pointer to the heap-allocated proc.
pub(crate) raw_proc: NonNull<()>,
}

Expand Down Expand Up @@ -42,13 +42,13 @@ impl LightProc {
R: Send + 'static,
S: Fn(LightProc) + Send + Sync + 'static,
{
let raw_task = RawProc::allocate(stack, future, schedule);
let task = LightProc { raw_proc: raw_task };
let raw_proc = RawProc::allocate(stack, future, schedule);
let proc = LightProc { raw_proc: raw_proc };
let handle = ProcHandle {
raw_proc: raw_task,
raw_proc: raw_proc,
_marker: PhantomData,
};
(task, handle)
(proc, handle)
}

pub fn schedule(self) {
Expand Down Expand Up @@ -97,13 +97,13 @@ impl Drop for LightProc {
let pdata = ptr as *const ProcData;

unsafe {
// Cancel the task.
// Cancel the proc.
(*pdata).cancel();

// Drop the future.
((*pdata).vtable.drop_future)(ptr);

// Drop the task reference.
// Drop the proc reference.
((*pdata).vtable.decrement)(ptr);
}
}
Expand All @@ -114,7 +114,7 @@ impl fmt::Debug for LightProc {
let ptr = self.raw_proc.as_ptr();
let pdata = ptr as *const ProcData;

f.debug_struct("Task")
f.debug_struct("LightProc")
.field("pdata", unsafe { &(*pdata) })
.field("stack", self.stack())
.finish()
Expand Down
28 changes: 14 additions & 14 deletions lightproc/src/proc_data.rs
Expand Up @@ -11,50 +11,50 @@ use crate::proc_stack::*;
use crate::proc_vtable::ProcVTable;
use crate::state::*;

/// The pdata of a task.
/// The pdata of a proc.
///
/// This pdata is stored right at the beginning of every heap-allocated task.
/// This pdata is stored right at the beginning of every heap-allocated proc.
pub(crate) struct ProcData {
/// Current state of the task.
/// Current state of the proc.
///
/// Contains flags representing the current state and the reference count.
pub(crate) state: AtomicUsize,

/// The task that is blocked on the `JoinHandle`.
/// The proc that is blocked on the `ProcHandle`.
///
/// This waker needs to be woken once the task completes or is closed.
/// This waker needs to be woken once the proc completes or is closed.
pub(crate) awaiter: Cell<Option<Waker>>,

/// The virtual table.
///
/// In addition to the actual waker virtual table, it also contains pointers to several other
/// methods necessary for bookkeeping the heap-allocated task.
/// methods necessary for bookkeeping the heap-allocated proc.
pub(crate) vtable: &'static ProcVTable,
}

impl ProcData {
/// Cancels the task.
/// Cancels the proc.
///
/// This method will only mark the task as closed and will notify the awaiter, but it won't
/// reschedule the task if it's not completed.
/// This method will only mark the proc as closed and will notify the awaiter, but it won't
/// reschedule the proc if it's not completed.
pub(crate) fn cancel(&self) {
let mut state = self.state.load(Ordering::Acquire);

loop {
// If the task has been completed or closed, it can't be cancelled.
// If the proc has been completed or closed, it can't be cancelled.
if state & (COMPLETED | CLOSED) != 0 {
break;
}

// Mark the task as closed.
// Mark the proc as closed.
match self.state.compare_exchange_weak(
state,
state | CLOSED,
Ordering::AcqRel,
Ordering::Acquire,
) {
Ok(_) => {
// Notify the awaiter that the task has been closed.
// Notify the awaiter that the proc has been closed.
if state & AWAITER != 0 {
self.notify();
}
Expand All @@ -66,7 +66,7 @@ impl ProcData {
}
}

/// Notifies the task blocked on the task.
/// Notifies the proc blocked on the proc.
///
/// If there is a registered waker, it will be removed from the pdata and woken.
#[inline]
Expand All @@ -77,7 +77,7 @@ impl ProcData {
}
}

/// Notifies the task blocked on the task unless its waker matches `current`.
/// Notifies the proc blocked on the proc unless its waker matches `current`.
///
/// If there is a registered waker, it will be removed from the pdata.
#[inline]
Expand Down
68 changes: 34 additions & 34 deletions lightproc/src/proc_handle.rs
Expand Up @@ -10,14 +10,14 @@ use crate::proc_data::ProcData;
use crate::proc_stack::*;
use crate::state::*;

/// A handle that awaits the result of a task.
/// A handle that awaits the result of a proc.
///
/// This type is a future that resolves to an `Option<R>` where:
///
/// * `None` indicates the task has panicked or was cancelled
/// * `Some(res)` indicates the task has completed with `res`
/// * `None` indicates the proc has panicked or was cancelled
/// * `Some(res)` indicates the proc has completed with `res`
pub struct ProcHandle<R> {
/// A raw task pointer.
/// A raw proc pointer.
pub(crate) raw_proc: NonNull<()>,

/// A marker capturing the generic type `R`.
Expand All @@ -30,11 +30,11 @@ unsafe impl<R> Sync for ProcHandle<R> {}
impl<R> Unpin for ProcHandle<R> {}

impl<R> ProcHandle<R> {
/// Cancels the task.
/// Cancels the proc.
///
/// If the task has already completed, calling this method will have no effect.
/// If the proc has already completed, calling this method will have no effect.
///
/// When a task is cancelled, its future cannot be polled again and will be dropped instead.
/// When a proc is cancelled, its future cannot be polled again and will be dropped instead.
pub fn cancel(&self) {
let ptr = self.raw_proc.as_ptr();
let pdata = ptr as *const ProcData;
Expand All @@ -43,33 +43,33 @@ impl<R> ProcHandle<R> {
let mut state = (*pdata).state.load(Ordering::Acquire);

loop {
// If the task has been completed or closed, it can't be cancelled.
// If the proc has been completed or closed, it can't be cancelled.
if state & (COMPLETED | CLOSED) != 0 {
break;
}

// If the task is not scheduled nor running, we'll need to schedule it.
// If the proc is not scheduled nor running, we'll need to schedule it.
let new = if state & (SCHEDULED | RUNNING) == 0 {
(state | SCHEDULED | CLOSED) + REFERENCE
} else {
state | CLOSED
};

// Mark the task as closed.
// Mark the proc as closed.
match (*pdata).state.compare_exchange_weak(
state,
new,
Ordering::AcqRel,
Ordering::Acquire,
) {
Ok(_) => {
// If the task is not scheduled nor running, schedule it so that its future
// If the proc is not scheduled nor running, schedule it so that its future
// gets dropped by the executor.
if state & (SCHEDULED | RUNNING) == 0 {
((*pdata).vtable.schedule)(ptr);
}

// Notify the awaiter that the task has been closed.
// Notify the awaiter that the proc has been closed.
if state & AWAITER != 0 {
(*pdata).notify();
}
Expand All @@ -82,7 +82,7 @@ impl<R> ProcHandle<R> {
}
}

/// Returns a reference to the stack stored inside the task.
/// Returns a reference to the stack stored inside the proc.
pub fn stack(&self) -> &ProcStack {
let offset = ProcData::offset_stack();
let ptr = self.raw_proc.as_ptr();
Expand All @@ -103,8 +103,8 @@ impl<R> Drop for ProcHandle<R> {
let mut output = None;

unsafe {
// Optimistically assume the `JoinHandle` is being dropped just after creating the
// task. This is a common case so if the handle is not used, the overhead of it is only
// Optimistically assume the `ProcHandle` is being dropped just after creating the
// proc. This is a common case so if the handle is not used, the overhead of it is only
// one compare-exchange operation.
if let Err(mut state) = (*pdata).state.compare_exchange_weak(
SCHEDULED | HANDLE | REFERENCE,
Expand All @@ -113,10 +113,10 @@ impl<R> Drop for ProcHandle<R> {
Ordering::Acquire,
) {
loop {
// If the task has been completed but not yet closed, that means its output
// If the proc has been completed but not yet closed, that means its output
// must be dropped.
if state & COMPLETED != 0 && state & CLOSED == 0 {
// Mark the task as closed in order to grab its output.
// Mark the proc as closed in order to grab its output.
match (*pdata).state.compare_exchange_weak(
state,
state | CLOSED,
Expand All @@ -133,7 +133,7 @@ impl<R> Drop for ProcHandle<R> {
Err(s) => state = s,
}
} else {
// If this is the last reference to the task and it's not closed, then
// If this is the last reference to the proc and it's not closed, then
// close it and schedule one more time so that its future gets dropped by
// the executor.
let new = if state & (!(REFERENCE - 1) | CLOSED) == 0 {
Expand All @@ -150,7 +150,7 @@ impl<R> Drop for ProcHandle<R> {
Ordering::Acquire,
) {
Ok(_) => {
// If this is the last reference to the task, we need to either
// If this is the last reference to the proc, we need to either
// schedule dropping its future or destroy it.
if state & !(REFERENCE - 1) == 0 {
if state & CLOSED == 0 {
Expand All @@ -169,7 +169,7 @@ impl<R> Drop for ProcHandle<R> {
}
}

// Drop the output if it was taken out of the task.
// Drop the output if it was taken out of the proc.
drop(output);
}
}
Expand All @@ -185,39 +185,39 @@ impl<R> Future for ProcHandle<R> {
let mut state = (*pdata).state.load(Ordering::Acquire);

loop {
// If the task has been closed, notify the awaiter and return `None`.
// If the proc has been closed, notify the awaiter and return `None`.
if state & CLOSED != 0 {
// Even though the awaiter is most likely the current task, it could also be
// another task.
// Even though the awaiter is most likely the current proc, it could also be
// another proc.
(*pdata).notify_unless(cx.waker());
return Poll::Ready(None);
}

// If the task is not completed, register the current task.
// If the proc is not completed, register the current proc.
if state & COMPLETED == 0 {
// Replace the waker with one associated with the current task. We need a
// Replace the waker with one associated with the current proc. We need a
// safeguard against panics because dropping the previous waker can panic.
(*pdata).swap_awaiter(Some(cx.waker().clone()));

// Reload the state after registering. It is possible that the task became
// Reload the state after registering. It is possible that the proc became
// completed or closed just before registration so we need to check for that.
state = (*pdata).state.load(Ordering::Acquire);

// If the task has been closed, notify the awaiter and return `None`.
// If the proc has been closed, notify the awaiter and return `None`.
if state & CLOSED != 0 {
// Even though the awaiter is most likely the current task, it could also
// be another task.
// Even though the awaiter is most likely the current proc, it could also
// be another proc.
(*pdata).notify_unless(cx.waker());
return Poll::Ready(None);
}

// If the task is still not completed, we're blocked on it.
// If the proc is still not completed, we're blocked on it.
if state & COMPLETED == 0 {
return Poll::Pending;
}
}

// Since the task is now completed, mark it as closed in order to grab its output.
// Since the proc is now completed, mark it as closed in order to grab its output.
match (*pdata).state.compare_exchange(
state,
state | CLOSED,
Expand All @@ -226,12 +226,12 @@ impl<R> Future for ProcHandle<R> {
) {
Ok(_) => {
// Notify the awaiter. Even though the awaiter is most likely the current
// task, it could also be another task.
// proc, it could also be another proc.
if state & AWAITER != 0 {
(*pdata).notify_unless(cx.waker());
}

// Take the output from the task.
// Take the output from the proc.
let output = ((*pdata).vtable.get_output)(ptr) as *mut R;
return Poll::Ready(Some(output.read()));
}
Expand All @@ -247,7 +247,7 @@ impl<R> fmt::Debug for ProcHandle<R> {
let ptr = self.raw_proc.as_ptr();
let pdata = ptr as *const ProcData;

f.debug_struct("JoinHandle")
f.debug_struct("ProcHandle")
.field("pdata", unsafe { &(*pdata) })
.finish()
}
Expand Down
10 changes: 5 additions & 5 deletions lightproc/src/proc_layout.rs
Expand Up @@ -2,18 +2,18 @@ use std::alloc::Layout;

#[derive(Clone, Copy)]
pub(crate) struct TaskLayout {
/// Memory layout of the whole task.
/// Memory layout of the whole proc.
pub(crate) layout: Layout,

/// Offset into the task at which the stack is stored.
/// Offset into the proc at which the stack is stored.
pub(crate) offset_t: usize,

/// Offset into the task at which the schedule function is stored.
/// Offset into the proc at which the schedule function is stored.
pub(crate) offset_s: usize,

/// Offset into the task at which the future is stored.
/// Offset into the proc at which the future is stored.
pub(crate) offset_f: usize,

/// Offset into the task at which the output is stored.
/// Offset into the proc at which the output is stored.
pub(crate) offset_r: usize,
}

0 comments on commit 7049d70

Please sign in to comment.