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
#[cfg(feature = "runtime_thread")]
pub type Receiver<T> = crossbeam::channel::Receiver<T>;
#[cfg(feature = "runtime_thread")]
pub type Sender<T> = crossbeam::channel::Sender<T>;
#[cfg(feature = "runtime_thread")]
pub type SendError<T> = crossbeam_channel::SendError<T>;
#[cfg(feature = "runtime_thread")]
pub type JoinHandle<T> = std::thread::JoinHandle<T>;
#[cfg(feature = "runtime_thread")]
pub type WaitGroup = crossbeam_utils::sync::WaitGroup;

#[cfg(feature = "runtime_thread")]
pub fn chan<T>(len: Option<usize>) -> (Sender<T>, Receiver<T>) {
    match len {
        None => crossbeam::channel::unbounded(),
        Some(len) => crossbeam::channel::bounded(len),
    }
}

#[cfg(feature = "runtime_thread")]
pub fn spawn<F>(f: F) -> JoinHandle<()>
    where
        F: FnOnce() + Send + 'static,
{
    std::thread::spawn(f)
}

#[cfg(feature = "runtime_thread")]
pub fn spawn_stack_size<F>(f: F, stack_size: usize) -> JoinHandle<()>
    where
        F: FnOnce() + Send + 'static,
{
    std::thread::spawn(f)
}


pub fn try_send_num<T>(sender: &Sender<T>, num: usize, mut target: T) {
    let mut trys = 0;
    loop {
        if trys > 3 {
            sender.send(target);
            break;
        }
        if let Err(e) = sender.try_send(target) {
            trys += 1;
            target = e.into_inner();
        } else {
            break;
        }
    }
}