added completion status

serial
Avril 3 years ago
parent 18944e4838
commit a65590d86a
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -10,6 +10,81 @@ use std::io::{
};
use std::{fmt,error};
/// A job's completion status
///
/// If the job has not completed, it will be the default value `Pending`.
/// Once the job completes, the result of the job can be inserted to set to `Complete`.
/// Afterwards, you can extract the value and it will have the state `Taken`; which means "job has completed, but the value has been consumed".
///
/// # Notes
/// This is not coupled to anything, it's the user's responsibility to update this status when a job completes.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Status<T>
{
/// For running operations
Pending,
/// An operation that has completed with its value
Complete(T),
/// An operation who's completed value has been extracted and processed
Taken,
}
impl<T> Status<T>
{
/// Assign a completed value to this instance.
///
/// If there was one already set, it is ignored.
#[inline] pub fn complete(&mut self, value: T)
{
*self = Self::Complete(value);
}
/// Has the value not yet been assigned or taken?
pub fn is_pending(&self) -> bool
{
match self {
Self::Pending => true,
_ => false,
}
}
/// Take the completed value from this instance
pub fn take(&mut self) -> Option<T>
{
match std::mem::replace(self, Self::Taken) {
Self::Complete(t) => Some(t),
_ => None
}
}
/// Get a mutable reference to the completed value if there is one.
pub fn peek_mut(&mut self) -> Option<&mut T>
{
match self {
Self::Complete(ref mut t) => Some(t),
_ => None,
}
}
/// Get a reference to the completed value if there is one.
pub fn peek(&self) -> Option<&T>
{
match self {
Self::Complete(ref t) => Some(t),
_ => None,
}
}
}
impl<T> Default for Status<T>
{
#[inline]
fn default() -> Self
{
Self::Pending
}
}
#[derive(Debug)]
pub struct Prelude
{
@ -52,8 +127,6 @@ pub struct Job
offset: usize,
}
//TODO: job's work :^)
impl Job
{
pub fn state(&self) -> &state::State

@ -21,6 +21,19 @@ use std::convert::TryFrom;
};
}
macro_rules! vec_default {
($num:expr) => {
{
let num = $num;
let mut v= Vec::with_capacity(num);
v.resize_with(num, Default::default);
v
}
}
}
mod state;
mod pool;
mod map;
@ -54,6 +67,7 @@ fn work(arg::Operation{output, inputs}: arg::Operation) -> Result<(), Box<dyn st
let output = output.complete(size)?;
// - create state
let mut complete: Vec<job::Status<usize>> = vec_default!(jobs.len()); // when we receive completion from the stream (which yeilds `(usize, usize)`), the first item is the *index* in this vector to set, and the second is the value
let state: state::State = {todo!()};
// - spawn the task thread pool

@ -41,7 +41,7 @@ impl MemoryMapMut
/// Unmap this file and return the fd
pub fn unmap(self) -> fs::File
{
use std::{mem, ptr};
use std::mem;
let mut this = mem::ManuallyDrop::new(self);
unsafe {
let map = (&mut this.map as *mut MmapMut).read();

Loading…
Cancel
Save