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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
use std::os::raw::c_void;

pub use base_coroutine::*;

pub use open_coroutine_macros::*;

#[allow(dead_code)]
extern "C" {
    fn init_hook();

    fn coroutine_crate(
        f: UserFunc<Option<&'static mut c_void>, (), Option<&'static mut c_void>>,
        param: Option<&'static mut c_void>,
        stack_size: usize,
    ) -> JoinHandle;

    fn coroutine_join(handle: JoinHandle) -> libc::c_long;

    fn coroutine_timeout_join(handle: &JoinHandle, ns_time: u64) -> libc::c_long;

    fn try_timed_schedule(ns_time: u64) -> libc::c_int;

    fn timed_schedule(ns_time: u64) -> libc::c_int;
}

pub fn init() {
    unsafe { init_hook() };
    println!("open-coroutine inited !");
}

pub fn co<F, P, R: 'static>(f: F, param: Option<&'static mut P>, stack_size: usize) -> JoinHandle
where
    F: FnOnce(
            &'static Yielder<Option<&'static mut P>, (), Option<&'static mut R>>,
            Option<&'static mut P>,
        ) -> Option<&'static mut R>
        + Copy,
{
    extern "C" fn co_main<F, P: 'static, R: 'static>(
        yielder: &Yielder<Option<&'static mut c_void>, (), Option<&'static mut c_void>>,
        input: Option<&'static mut c_void>,
    ) -> Option<&'static mut c_void>
    where
        F: FnOnce(
                &'static Yielder<Option<&'static mut P>, (), Option<&'static mut R>>,
                Option<&'static mut P>,
            ) -> Option<&'static mut R>
            + Copy,
    {
        unsafe {
            let ptr: &mut (F, Option<&'static mut P>) = std::mem::transmute(input.unwrap());
            let data = std::ptr::read_unaligned(ptr);
            let result = (data.0)(std::mem::transmute(yielder), data.1);
            result.map(|p| std::mem::transmute(p))
        }
    }
    let inner = Box::leak(Box::new((f, param)));
    unsafe {
        coroutine_crate(
            co_main::<F, P, R>,
            Some(std::mem::transmute(inner)),
            stack_size,
        )
    }
}

pub fn join(handle: JoinHandle) -> libc::c_long {
    unsafe { coroutine_join(handle) }
}

pub fn timeout_join(handle: &JoinHandle, ns_time: u64) -> libc::c_long {
    unsafe { coroutine_timeout_join(handle, ns_time) }
}

pub fn schedule() -> bool {
    unsafe { try_timed_schedule(u64::MAX) == 0 }
}

#[cfg(test)]
mod tests {
    use crate::{co, coroutine_crate, init, schedule, JoinHandle, UserFunc, Yielder};
    use std::io::{BufRead, BufReader, Read, Write};
    use std::net::{IpAddr, Ipv4Addr, SocketAddr, TcpListener, TcpStream};
    use std::os::raw::c_void;
    use std::sync::atomic::{AtomicBool, Ordering};
    use std::sync::{Arc, Condvar, Mutex};
    use std::time::{Duration, SystemTime, UNIX_EPOCH};

    #[test]
    fn test_link() {
        init();
    }

    #[test]
    fn simplest() {
        let _ = co(
            |_yielder, input: Option<&'static mut c_void>| {
                println!("[coroutine1] launched");
                input
            },
            None,
            4096,
        );
        let _ = co(
            |_yielder, input: Option<&'static mut c_void>| {
                println!("[coroutine2] launched");
                input
            },
            None,
            4096,
        );
        assert!(schedule());
    }

    fn now() -> u64 {
        SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("1970-01-01 00:00:00 UTC was {} seconds ago!")
            .as_nanos() as u64
    }

    fn hook_test(millis: u64) {
        let _ = co(
            |_yielder, input: Option<&'static mut c_void>| {
                println!("[coroutine1] launched");
                input
            },
            None,
            4096,
        );
        let _ = co(
            |_yielder, input: Option<&'static mut c_void>| {
                println!("[coroutine2] launched");
                input
            },
            None,
            4096,
        );
        let start = now();
        std::thread::sleep(Duration::from_millis(millis));
        let end = now();
        assert!(end - start >= millis);
    }

    #[test]
    fn hook_test_schedule_timeout() {
        hook_test(1)
    }

    #[test]
    fn hook_test_schedule_normal() {
        hook_test(1_000)
    }

    fn co_crate(
        f: UserFunc<Option<&'static mut c_void>, (), Option<&'static mut c_void>>,
        param: Option<&'static mut c_void>,
        stack_size: usize,
    ) -> JoinHandle {
        unsafe { coroutine_crate(f, param, stack_size) }
    }

    extern "C" fn fx(
        _yielder: &Yielder<Option<&'static mut c_void>, (), Option<&'static mut c_void>>,
        input: Option<&'static mut c_void>,
    ) -> Option<&'static mut c_void> {
        match input {
            Some(param) => println!(
                "[coroutine] launched param:{}",
                param as *mut c_void as usize
            ),
            None => println!("[coroutine] launched"),
        }
        None
    }

    static SERVER_STARTED: AtomicBool = AtomicBool::new(false);

    unsafe fn crate_server(port: u16, server_finished: Arc<(Mutex<bool>, Condvar)>) {
        //invoke by libc::listen
        let _ = co_crate(fx, Some(&mut *(1usize as *mut c_void)), 4096);
        let mut data: [u8; 512] = std::mem::zeroed();
        data[511] = b'\n';
        let listener = TcpListener::bind("127.0.0.1:".to_owned() + &port.to_string())
            .expect(&*("bind to 127.0.0.1:".to_owned() + &port.to_string() + " failed !"));
        SERVER_STARTED.store(true, Ordering::Release);
        //invoke by libc::accept
        let _ = co_crate(fx, Some(&mut *(2usize as *mut c_void)), 4096);
        for stream in listener.incoming() {
            let mut stream = stream.expect("accept new connection failed !");
            let mut buffer: [u8; 512] = [0; 512];
            loop {
                //invoke by libc::recv
                let _ = co_crate(fx, Some(&mut *(6usize as *mut c_void)), 4096);
                //从流里面读内容,读到buffer中
                let bytes_read = stream.read(&mut buffer).expect("server read failed !");
                if bytes_read == 1 && buffer[0] == b'e' {
                    //如果读到的为空,说明已经结束了
                    let (lock, cvar) = &*server_finished;
                    let mut pending = lock.lock().unwrap();
                    *pending = false;
                    cvar.notify_one();
                    println!("server closed");
                    return;
                }
                assert_eq!(512, bytes_read);
                assert_eq!(data, buffer);
                //invoke by libc::send
                let _ = co_crate(fx, Some(&mut *(7usize as *mut c_void)), 4096);
                //回写
                assert_eq!(
                    bytes_read,
                    stream
                        .write(&buffer[..bytes_read])
                        .expect("server write failed !")
                );
            }
        }
    }

    unsafe fn client_main(mut stream: TcpStream) {
        let mut data: [u8; 512] = std::mem::zeroed();
        data[511] = b'\n';
        let mut buffer: Vec<u8> = Vec::with_capacity(512);
        for _ in 0..3 {
            //invoke by libc::send
            let _ = co_crate(fx, Some(&mut *(4usize as *mut c_void)), 4096);
            //写入stream流,如果写入失败,提示“写入失败”
            assert_eq!(512, stream.write(&data).expect("Failed to write!"));

            //invoke by libc::recv
            let _ = co_crate(fx, Some(&mut *(5usize as *mut c_void)), 4096);
            let mut reader = BufReader::new(&stream);
            //一直读到换行为止(b'\n'中的b表示字节),读到buffer里面
            assert_eq!(
                512,
                reader
                    .read_until(b'\n', &mut buffer)
                    .expect("Failed to read into buffer")
            );
            assert_eq!(&data, &buffer as &[u8]);
            buffer.clear();
        }
        //发送终止符
        assert_eq!(1, stream.write(&[b'e']).expect("Failed to write!"));
        println!("client closed");
    }

    #[test]
    fn hook_test_connect_and_poll_and_accept() -> std::io::Result<()> {
        let port = 8888;
        let clone = port.clone();
        let server_finished_pair = Arc::new((Mutex::new(true), Condvar::new()));
        let server_finished = Arc::clone(&server_finished_pair);
        unsafe {
            std::thread::spawn(move || crate_server(clone, server_finished_pair));
            std::thread::spawn(move || {
                //等服务端起来
                while !SERVER_STARTED.load(Ordering::Acquire) {}
                //invoke by libc::connect
                let _ = co_crate(fx, Some(&mut *(3usize as *mut c_void)), 4096);
                let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), port);
                let stream = TcpStream::connect_timeout(&socket, Duration::from_secs(3))
                    .expect(&*("failed to 127.0.0.1:".to_owned() + &port.to_string() + " !"));
                client_main(stream)
            });

            let (lock, cvar) = &*server_finished;
            let result = cvar
                .wait_timeout_while(
                    lock.lock().unwrap(),
                    Duration::from_secs(30),
                    |&mut pending| pending,
                )
                .unwrap();
            if result.1.timed_out() {
                Err(std::io::Error::new(
                    std::io::ErrorKind::Other,
                    "The service was not completed within the specified time",
                ))
            } else {
                Ok(())
            }
        }
    }
}