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

use std::time::Duration;

/// if use cogo runtime
#[cfg(feature = "cogo")]
pub type TcpListener = cogo::net::TcpListener;
#[cfg(feature = "cogo")]
pub type TcpStream = cogo::net::TcpStream;
#[cfg(feature = "cogo")]
pub type Receiver<T> = cogo::std::sync::channel::Receiver<T>;
#[cfg(feature = "cogo")]
pub type Sender<T> = cogo::std::sync::channel::Sender<T>;
#[cfg(feature = "cogo")]
pub type JoinHandle<T> = cogo::coroutine::JoinHandle<T>;
#[cfg(feature = "cogo")]
pub type Mutex<T> = cogo::std::sync::Mutex<T>;


#[cfg(feature = "cogo")]
pub fn chan<T>() -> (Sender<T>, Receiver<T>) {
    cogo::chan!()
}

#[cfg(feature = "cogo")]
pub fn sleep(d: Duration) {
    cogo::coroutine::sleep(d)
}

#[cfg(feature = "cogo")]
pub fn spawn<F>(f: F) -> JoinHandle<()> where F: FnOnce() + std::marker::Send + 'static {
    cogo::coroutine::Builder::new().stack_size(0x1000).spawn(f)
}

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


/// if not cogo
#[cfg(not(feature = "cogo"))]
pub type TcpListener = std::net::TcpListener;
#[cfg(not(feature = "cogo"))]
pub type TcpStream = std::net::TcpStream;
#[cfg(not(feature = "cogo"))]
pub type Receiver<T> = crossbeam::channel::Receiver<T>;
#[cfg(not(feature = "cogo"))]
pub type Sender<T> = crossbeam::channel::Sender<T>;
#[cfg(not(feature = "cogo"))]
pub type JoinHandle<T> = std::thread::JoinHandle<T>;
#[cfg(not(feature = "cogo"))]
pub type Mutex<T> = std::sync::Mutex<T>;

#[cfg(not(feature = "cogo"))]
pub fn chan<T>() -> (Sender<T>, Receiver<T>) {
    crossbeam::channel::unbounded()
}

#[cfg(not(feature = "cogo"))]
pub fn sleep(d: Duration) {
    std::thread::sleep(d)
}

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

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