SyncPeriodicTasks

Struct SyncPeriodicTasks 

Source
pub struct SyncPeriodicTasks { /* private fields */ }
Expand description

A main instance which spawns the task executor.

let spt = SyncPeriodicTasks::new(1.try_into().unwrap()).unwrap();
let task1 = TaskStruct1::new(2, send);
let task1_ptt = PeriodicTaskTime::Relative(TimerExpMode::<RelativeTime>::new_interval(RelativeTime::new_time(1, 0)));
let task1_guard = s.add("task1", task1, task1_ptt).unwrap();

This instance should be kept somewhere and dropped only if the task executor WITH all spawned tasks are no longer needed.

Implementations§

Source§

impl SyncPeriodicTasks

Source

pub fn new(threads_cnt: NonZeroUsize) -> TimerResult<Self>

Creates new instance. An amount of threads allocated for the task executor should be specified. All threads will be started immidiatly. For small tasks one thread will be enough. For a large amount of tasks, especially it tasks are waken up oftenly then at least two threads should be allocated.

§Arguments
§Returns

The Result is returned as alias TimerResult.

Source

pub fn add<T>( &self, task_name: impl Into<String>, task: T, task_time: PeriodicTaskTime, ) -> TimerResult<PeriodicTaskGuard>
where T: PeriodicTask,

Adds and spawns the task. The task is defined with generic T which is any datatype which implements PeriodicTask.

§Arguments
  • task_name - a task name. used only for identification purposes in debug messages.

  • task - a task which should be executed. It should implenet PeriodicTask.

  • task_time - PeriodicTaskTime a time when the task must be spawned.

§Returns

The Result is returned as alias TimerResult.

§Example
#[derive(Debug)]
struct TaskStruct1
{
    a1: u64,
    s: Sender<u64>,
}
 
impl TaskStruct1
{
    fn new(a1: u64, s: Sender<u64>) -> Self
    {
        return Self{ a1: a1, s };
    }
}
 
impl PeriodicTask for TaskStruct1
{
    fn exec(&mut self) -> PeriodicTaskResult
    {
        println!("taskstruct1 val: {}", self.a1);
        let _ = self.s.send(self.a1);
        return PeriodicTaskResult::Ok;
    }
}
 
fn main()
{
    let s = SyncPeriodicTasks::new(1.try_into().unwrap()).unwrap();
     
    let (send, recv) = mpsc::channel::<u64>();
 
    let task1 = TaskStruct1::new(2, send);
 
    let task1_ptt = PeriodicTaskTime::exact_time(AbsoluteTime::now() + RelativeTime::new_time(3, 0));
 
    let task1_guard = s.add("task1", task1, task1_ptt).unwrap();
 
    println!("added");
 
    let val = recv.recv();
 
    println!("{:?}", val);
 
    drop(task1_guard);
}
Source

pub fn add_closure<F>( &self, task_name: impl Into<String>, task_time: PeriodicTaskTime, clo: F, ) -> TimerResult<PeriodicTaskGuard>
where F: 'static + FnMut() -> PeriodicTaskResult + Send,

Adds and spawns the task in form of a closure.

§Arguments
  • task_name - a task name. used only for identification purposes in debug messages.

  • task_time - PeriodicTaskTime a time when the task must be spawned.

  • clo - a function to exec on timeout. The function must return PeriodicTaskResult.

§Returns

The Result is returned as alias TimerResult.

§Example
 
let task1_ptt = PeriodicTaskTime::exact_time(AbsoluteTime::now() + RelativeTime::new_time(3, 0));
 
let (send, recv) = mpsc::channel::<u64>();
 
let task1_guard = 
    s.add_closure("task2", task1_ptt, 
        move || 
        { 
            println!("test output"); 
            send.send(1).unwrap();
 
            return PeriodicTaskResult::Ok;
        }
    ).unwrap();
 
let val = recv.recv();
 
println!("{:?}", val);
Source

pub fn check_thread_status(&self) -> Option<String>

Checks if any thread have crashed and no longer works.

§Returns

A Option is retuerned with the inner data:

Trait Implementations§

Source§

impl Clone for SyncPeriodicTasks

Source§

fn clone(&self) -> SyncPeriodicTasks

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SyncPeriodicTasks

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for SyncPeriodicTasks

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V