Task/
Thread.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use super::*;
use std::{
    any::Any,
    mem::transmute,
    thread::{self},
};

pub struct Join_handle_type<T>(thread::JoinHandle<T>);

impl<T> Join_handle_type<T> {
    pub fn Join(self) -> std::result::Result<T, Box<dyn Any + Send>> {
        self.0.join()
    }

    pub fn Get_thread_identifier(&self) -> Thread_identifier_type {
        self.Get_thread_wrapper().Get_identifier()
    }

    pub(crate) fn Get_thread_wrapper(&self) -> Thread_wrapper_type {
        Thread_wrapper_type(self.0.thread().clone())
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct Thread_identifier_type(usize);

impl From<usize> for Thread_identifier_type {
    fn from(Identifier: usize) -> Self {
        Thread_identifier_type(Identifier)
    }
}

impl From<Thread_identifier_type> for usize {
    fn from(Identifier: Thread_identifier_type) -> Self {
        Identifier.0
    }
}

impl From<thread::ThreadId> for Thread_identifier_type {
    fn from(Identifier: thread::ThreadId) -> Self {
        let Identifier: u64 = unsafe { transmute(Identifier) };

        Thread_identifier_type(Identifier as usize)
    }
}

/// A wrapper around [std::thread::Thread].
pub struct Thread_wrapper_type(thread::Thread);

impl Thread_wrapper_type {
    /// Creates a new thread with a given name, stack size and function.
    pub fn Spawn<F, T>(
        Name: &str,
        Stack_size: Option<usize>,
        Function: F,
    ) -> Result_type<Join_handle_type<T>>
    where
        T: Send + 'static,
        F: FnOnce() -> T + Send + 'static,
    {
        let Thread_builder = thread::Builder::new().name(Name.to_string());

        let Thread_builder = match Stack_size {
            Some(Stack_size) => Thread_builder.stack_size(Stack_size),
            None => Thread_builder,
        };

        let Join_handle = Thread_builder
            .spawn(Function)
            .map_err(|_| Error_type::Failed_to_spawn_thread)?;

        Ok(Join_handle_type(Join_handle))
    }

    /// Gets the name of the thread.
    pub fn Get_name(&self) -> Option<&str> {
        self.0.name()
    }

    pub fn Sleep(Duration: std::time::Duration) {
        std::thread::sleep(Duration);
    }

    pub fn Get_identifier(&self) -> Thread_identifier_type {
        Thread_identifier_type::from(self.0.id())
    }

    pub fn Get_current() -> Thread_wrapper_type {
        Thread_wrapper_type(thread::current())
    }
}