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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
/*!
In this module there're implementations & tests of `Cor`.
*/
use std::sync::{
    atomic::{AtomicBool, Ordering},
    mpsc::{channel, Receiver, Sender},
    Arc, Mutex,
};
use std::thread;

/**
Run codes inside a `doM` block(`Haskell` `do notation`)
(this is a `sync` macro)

# Arguments

* `func` - The given `FnMut`, the execution code of `Cor`.

*/
#[macro_export]
macro_rules! do_m {
    ($this:expr) => {{
        let mut _do_m = cor_newmutex!($this, (), ());

        {
            let _do_m2 = _do_m.clone();
            let mut do_m = _do_m2.lock().unwrap();
            do_m.set_async(false);
        }

        cor_start!(_do_m);
    }};
}

/**
Run codes inside a `doM` block(`Haskell` `do notation`)
(this is a `sync` macro)
*/
#[macro_export]
macro_rules! do_m_pattern {
    (
        let $p:pat = $e:expr ; $( $t:tt )*
    ) => (
        { let $p = $e ; do_m_pattern! { $( $t )* } }
    );
    (
        let $p:ident : $ty:ty = $e:expr ; $( $t:tt )*
    ) => (
        { let $p:$ty = $e ; do_m_pattern! { $( $t )* } }
    );
    (
        let mut $p:ident : $ty:ty = $e:expr ; $( $t:tt )*
    ) => (
        { let mut $p:$ty = $e ; do_m_pattern! { $( $t )* } }
    );
    (
        $p:ident = $e:expr ; $( $t:tt )*
    ) => (
        { $p = $e ; do_m_pattern! { $( $t )* } }
    );
    (
        exec $b:expr ; $( $t:tt )*
    ) => (
        { $b ; do_m_pattern! { $( $t )* } }
    );
    (
        ret $e:expr
    ) => (
        // vec!()
        $e
    );

    ($p:ident $ty:ty, $val:expr, yield_from $cor:expr; $($t:tt)*) => ({
        let $p: Arc<Mutex<Option<$ty>>> = Arc::new(Mutex::new(None::<$ty>));
        let _p = $p.clone();

        let _cor = $cor.clone();
        let mut _do_m = cor_newmutex!(
            move |this| {
                let mut p = _p.lock().unwrap();
                *p = cor_yield_from!(this, _cor, $val);
            },
            (),
            ()
        );

        {
            let _do_m2 = _do_m.clone();
            let mut do_m = _do_m2.lock().unwrap();
            do_m.set_async(false);
        }

        cor_start!(_do_m);

        do_m_pattern! { $( $t )* }

        // let mut v = vec!(p);
        // let next = &mut do_m_pattern! { $( $t )* };
        // v.append(next);
        // v
    });
}

/**
Define a new `Cor` with type.
It will return a `Arc<Mutex<Cor>>`.

# Arguments

* `func` - The given `FnMut`, the execution code of `Cor`.
* `RETURN` - The type of returned data
* `RECEIVE` - The type of received data

*/
#[macro_export]
macro_rules! cor_newmutex {
    ($func:expr, $RETURN:ty, $RECEIVE:ty) => {
        <Cor<$RETURN, $RECEIVE>>::new_with_mutex($func)
    };
}

/**
Define a new `Cor` with type and start it immediately.
It will return a `Arc<Mutex<Cor>>`.

# Arguments

* `func` - The given `FnMut`, the execution code of `Cor`.
* `RETURN` - The type of returned data
* `RECEIVE` - The type of received data

*/
#[macro_export]
macro_rules! cor_newmutex_and_start {
    ($func:expr, $RETURN:ty, $RECEIVE:ty) => {{
        let new_one = <Cor<$RETURN, $RECEIVE>>::new_with_mutex($func);
        cor_start!(new_one);
        new_one
    }};
}

/**
Make `this` returns a given `Option<RETURN>` `given_to_outside` to its callee `Cor`,
and this method returns the `Option<RECEIVE>` value given from outside.

# Arguments

* `this` - The sender when sending `given_to_outside` to callee `Cor`.
* `given_to_outside` - The value sent by `this` and received by `target`.

# Remarks

This method is implemented according to some coroutine/generator implementations,
such as `Python`, `Lua`, `ECMASript`...etc.

*/
#[macro_export]
macro_rules! cor_yield {
    ($this:expr, $given_to_outside:expr) => {
        Cor::yield_ref($this.clone(), $given_to_outside)
    };
}

/**
Make `this` sends a given `Option<RECEIVETARGET>` to `target`,
and this method returns the `Option<RETURNTARGET>` response from `target`.

# Arguments

* `this` - The sender when sending `sent_to_inside` to `target`.
* `target` - The receiver of value `sent_to_inside` sent by `this`.
* `sent_to_inside` - The value sent by `this` and received by `target`.

# Remarks

This method is implemented according to some coroutine/generator implementations,
such as `Python`, `Lua`, `ECMASript`...etc.

*/
#[macro_export]
macro_rules! cor_yield_from {
    ($this:expr, $target:expr, $sent_to_inside:expr) => {
        Cor::yield_from($this.clone(), $target.clone(), $sent_to_inside)
    };
}

/**

Start `this` `Cor`.

# Arguments

* `this` - The target `Cor` to start.

*NOTE*: Beware the deadlock if it's sync(waiting for each other), except the entry point.

*/
#[macro_export]
macro_rules! cor_start {
    ($this:expr) => {
        Cor::start($this.clone())
    };
}

/**
`CorOp` defines a yield action between `Cor` objects.

# Arguments

* `RETURN` - The generic type of returned data
* `RECEIVE` - The generic type of received data

# Remarks

It's the base of implementations of `Cor`.
It contains the `Cor` calling `yield_from`() and the val sent together,
and it's necessary to the target `Cor` making the response by `yield_ref`()/`yield_none`().

*/
pub struct CorOp<RETURN: 'static, RECEIVE: 'static> {
    pub result_ch_sender: Arc<Mutex<Sender<Option<RETURN>>>>,
    pub val: Option<RECEIVE>,
}
impl<RETURN, RECEIVE> CorOp<RETURN, RECEIVE> {}

type CorEffect<RETURN, RECEIVE> = FnMut(Arc<Mutex<Cor<RETURN, RECEIVE>>>) + Send + Sync + 'static;

/**
The `Cor` implements a *PythonicGenerator-like Coroutine*.

# Arguments

* `RETURN` - The generic type of returned data
* `RECEIVE` - The generic type of received data

# Remarks

It could be sync or async up to your usages,
and it could use `yield_from` to send a value to another `Cor` object and get the response,
and use `yield_ref`()/`yield_none`() to return my response to the callee of mine.

*NOTE*: Beware the deadlock if it's sync(waiting for each other), except the entry point.

*/
#[derive(Clone)]
pub struct Cor<RETURN: 'static, RECEIVE: 'static> {

    is_async: bool,

    started_alive: Arc<Mutex<(AtomicBool, AtomicBool)>>,

    op_ch_sender: Arc<Mutex<Sender<CorOp<RETURN, RECEIVE>>>>,
    op_ch_receiver: Arc<Mutex<Receiver<CorOp<RETURN, RECEIVE>>>>,
    effect: Arc<Mutex<CorEffect<RETURN, RECEIVE>>>,
}
impl<RETURN: Send + Sync + 'static, RECEIVE: Send + Sync + 'static> Cor<RETURN, RECEIVE> {
    /**
    Generate a new `Cor` with the given `FnMut` function for the execution of this `Cor`.

    # Arguments

    * `effect` - The given `FnMut`, the execution code of `Cor`.

    */
    pub fn new(
        effect: impl FnMut(Arc<Mutex<Cor<RETURN, RECEIVE>>>) + Send + Sync + 'static,
    ) -> Cor<RETURN, RECEIVE> {
        let (op_ch_sender, op_ch_receiver) = channel();
        Cor {
            is_async: true,
            started_alive: Arc::new(Mutex::new((AtomicBool::new(false), AtomicBool::new(false)))),

            op_ch_sender: Arc::new(Mutex::new(op_ch_sender)),
            op_ch_receiver: Arc::new(Mutex::new(op_ch_receiver)),
            effect: Arc::new(Mutex::new(effect)),
        }
    }

    /**
    Generate a new `Arc<Mutex<Cor<RETURN, RECEIVE>>>` with the given `FnMut` function for the execution of this `Cor`.

    # Arguments

    * `effect` - The given `FnMut`, the execution code of `Cor`.

    */
    pub fn new_with_mutex(
        effect: impl FnMut(Arc<Mutex<Cor<RETURN, RECEIVE>>>) + Send + Sync + 'static,
    ) -> Arc<Mutex<Cor<RETURN, RECEIVE>>> {
        Arc::new(Mutex::new(<Cor<RETURN, RECEIVE>>::new(effect)))
    }

    /**
    Make `this` sends a given `Option<RECEIVETARGET>` to `target`,
    and this method returns the `Option<RETURNTARGET>` response from `target`.

    # Arguments

    * `this` - The sender when sending `sent_to_inside` to `target`.
    * `target` - The receiver of value `sent_to_inside` sent by `this`.
    * `sent_to_inside` - The value sent by `this` and received by `target`.

    # Remarks

    This method is implemented according to some coroutine/generator implementations,
    such as `Python`, `Lua`, `ECMASript`...etc.

    */
    pub fn yield_from<RETURNTARGET: Send + Sync + 'static, RECEIVETARGET: Send + Sync + 'static>(
        this: Arc<Mutex<Cor<RETURN, RECEIVE>>>,
        target: Arc<Mutex<Cor<RETURNTARGET, RECEIVETARGET>>>,
        sent_to_inside: Option<RECEIVETARGET>,
    ) -> Option<RETURNTARGET> {
        // me MutexGuard lifetime block
        {
            let _me = this.clone();
            let me = _me.lock().unwrap();
            if !me.is_alive() {
                return None;
            }
        }

        // target MutexGuard lifetime block
        {
            let (result_ch_sender, result_ch_receiver) = channel();
            let _result_ch_sender = Arc::new(Mutex::new(result_ch_sender));
            let _result_ch_receiver = Arc::new(Mutex::new(result_ch_receiver));

            {
                target
                    .lock()
                    .unwrap()
                    .receive(_result_ch_sender.clone(), sent_to_inside);
            }

            let result;
            {
                let result_ch_receiver = _result_ch_receiver.lock().unwrap();
                result = result_ch_receiver.recv();
            }
            {
                drop(_result_ch_sender.lock().unwrap());
            }

            if let Ok(_x) = result {
                return _x;
            }
        }

        None
    }

    /**
    Make `this` returns a given `None::<RETURN>` to its callee `Cor`,
    and this method returns the `Option<RECEIVE>` value given from outside.

    # Arguments

    * `this` - The sender when sending `given_to_outside` to callee `Cor`.

    # Remarks

    This method is implemented according to some coroutine/generator implementations,
    such as `Python`, `Lua`, `ECMASript`...etc.

    */
    pub fn yield_none(this: Arc<Mutex<Cor<RETURN, RECEIVE>>>) -> Option<RECEIVE> {
        Cor::yield_ref(this, None)
    }

    /**
    Make `this` returns a given `Option<RETURN>` `given_to_outside` to its callee `Cor`,
    and this method returns the `Option<RECEIVE>` value given from outside.

    # Arguments

    * `this` - The sender when sending `given_to_outside` to callee `Cor`.
    * `given_to_outside` - The value sent by `this` and received by `target`.

    # Remarks

    This method is implemented according to some coroutine/generator implementations,
    such as `Python`, `Lua`, `ECMASript`...etc.

    */
    pub fn yield_ref(
        this: Arc<Mutex<Cor<RETURN, RECEIVE>>>,
        given_to_outside: Option<RETURN>,
    ) -> Option<RECEIVE> {
        let _op_ch_receiver;
        // me MutexGuard lifetime block
        {
            let _me = this.clone();
            let me = _me.lock().unwrap();
            if !me.is_alive() {
                return None;
            }
            _op_ch_receiver = me.op_ch_receiver.clone();
        }

        let op;
        {
            let op_ch = &*_op_ch_receiver.lock().unwrap();
            op = op_ch.recv();
        }

        if let Ok(_x) = op {
            {
                let _result = _x.result_ch_sender.lock().unwrap().send(given_to_outside);
            }

            return _x.val;
        }

        None
    }

    /**

    Start `this` `Cor`.

    # Arguments

    * `this` - The target `Cor` to start.

    *NOTE*: Beware the deadlock if it's sync(waiting for each other), except the entry point.

    */
    pub fn start(this: Arc<Mutex<Cor<RETURN, RECEIVE>>>) {
        let is_async;

        {
            let _started_alive;

            {
                let _me = this.clone();
                let me = _me.lock().unwrap();
                is_async = me.is_async;
                _started_alive = me.started_alive.clone();
            }

            let started_alive = _started_alive.lock().unwrap();
            let &(ref started, ref alive) = &*started_alive;
            if started.load(Ordering::SeqCst) {
                return;
            }
            started.store(true, Ordering::SeqCst);
            alive.store(true, Ordering::SeqCst);
        }

        {
            let do_things = move || {
                {
                    let mut _effect;
                    {
                        let _me = this.clone();
                        let me = _me.lock().unwrap();
                        _effect = me.effect.clone();
                    }

                    let effect = &mut *_effect.lock().unwrap();
                    (effect)(this.clone());
                }

                {
                    let _me = this.clone();
                    let mut me = _me.lock().unwrap();
                    me.stop();
                }
            };
            if is_async {
                thread::spawn(do_things);
            } else {
                do_things();
            }
        }
    }

    /**
    Setup async or not.
    Default `async`: `true`

    # Arguments

    * `async` - async when `true`, otherwise `sync`.

    *NOTE*: Beware the deadlock if it's sync(waiting for each other), except the entry point.

    */
    pub fn set_async(&mut self, is_async: bool) {
        self.is_async = is_async;
    }

    /**
    Did this `Cor` start?
    Return `true` when it did started (no matter it has stopped or not)

    */
    pub fn is_started(&self) -> bool {
        let _started_alive = self.started_alive.clone();
        let started_alive = _started_alive.lock().unwrap();
        let &(ref started, _) = &*started_alive;
        started.load(Ordering::SeqCst)
    }

    /**
    Is this `Cor` alive?
    Return `true` when it has started and not stopped yet.

    */
    pub fn is_alive(&self) -> bool {
        let _started_alive = self.started_alive.clone();
        let started_alive = _started_alive.lock().unwrap();
        let &(_, ref alive) = &*started_alive;
        alive.load(Ordering::SeqCst)
    }

    /**

    Stop `Cor`.
    This will make self.`is_alive`() returns `false`,
    and all `yield_from`() from this `Cor` as `target` will return `None::<RETURN>`.
    (Because it has stopped :P, that's reasonable)

    */
    pub fn stop(&mut self) {
        {
            let _started_alive = self.started_alive.clone();
            let started_alive = _started_alive.lock().unwrap();
            let &(ref started, ref alive) = &*started_alive;

            if !started.load(Ordering::SeqCst) {
                return;
            }
            if !alive.load(Ordering::SeqCst) {
                return;
            }
            alive.store(false, Ordering::SeqCst);

            {
                drop(self.op_ch_sender.lock().unwrap());
            }
        }
    }

    fn receive(
        &mut self,
        result_ch_sender: Arc<Mutex<Sender<Option<RETURN>>>>,
        given_as_request: Option<RECEIVE>,
    ) {
        let _op_ch_sender = self.op_ch_sender.clone();

        // do_close_safe
        if !self.is_alive() {
            return;
        }

        {
            let __started_alive = self.started_alive.clone();
            let _started_alive = __started_alive.lock().unwrap();

            // do: (effect)();
            let _result = _op_ch_sender.lock().unwrap().send(CorOp {
                // cor: cor,
                result_ch_sender,
                val: given_as_request,
            });
        }
    }

    /*
    fn do_close_safe(&mut self, mut effect: impl FnMut()) {
        if !self.is_alive() {
            return;
        }

        {
            let __started_alive = self.started_alive.clone();
            let _started_alive = __started_alive.lock().unwrap();

            (effect)();
        }
    }
    */
}

#[test]
fn test_cor_do_m() {
    let v = Arc::new(Mutex::new(String::from("")));

    let _v = v.clone();
    do_m!(move |this| {
        println!("test_cor_do_m started");

        let cor_inner1 = cor_newmutex_and_start!(
            |this| {
                let s = cor_yield!(this, Some(String::from("1")));
                println!("cor_inner1 {:?}", s);
            },
            String,
            i16
        );
        let cor_inner2 = cor_newmutex_and_start!(
            |this| {
                let s = cor_yield!(this, Some(String::from("2")));
                println!("cor_inner2 {:?}", s);
            },
            String,
            i16
        );
        let cor_inner3 = cor_newmutex_and_start!(
            |this| {
                let s = cor_yield!(this, Some(String::from("3")));
                println!("cor_inner3 {:?}", s);
            },
            String,
            i16
        );

        {
            (*_v.lock().unwrap()) = [
                cor_yield_from!(this, cor_inner1, Some(1)).unwrap(),
                cor_yield_from!(this, cor_inner2, Some(2)).unwrap(),
                cor_yield_from!(this, cor_inner3, Some(3)).unwrap(),
            ].join("");
        }
    });

    let _v = v.clone();

    {
        assert_eq!("123", *_v.lock().unwrap());
    }
}

#[test]
fn test_cor_do_m_pattern() {
    let _r = do_m_pattern! {
        let mut _v4 = String::from("");
        _v4 = String::from("4");

        exec {
            println!("do_m_pattern _v4:{:?}", _v4)
        };

        _v1 String, Some(1), yield_from cor_newmutex_and_start!(
            |this| {
                let s = cor_yield!(this, Some(String::from("1")));
                println!("cor_inner1 {:?}", s);
            },
            String,
            i16
        );
        _v2 String, Some(2), yield_from cor_newmutex_and_start!(
            |this| {
                let s = cor_yield!(this, Some(String::from("2")));
                println!("cor_inner2 {:?}", s);
            },
            String,
            i16
        );
        _v3 String, Some(3), yield_from cor_newmutex_and_start!(
            |this| {
                let s = cor_yield!(this, Some(String::from("3")));
                println!("cor_inner3 {:?}", s);
            },
            String,
            i16
        );
        let _v1 = _v1.lock().unwrap();
        let _v2 = _v2.lock().unwrap();
        let _v3 = _v3.lock().unwrap();
        ret [
            _v1.clone().unwrap(),
            _v2.clone().unwrap(),
            _v3.clone().unwrap(),
            _v4,
        ].join("")
    };

    assert_eq!("1234", _r);
}

#[test]
fn test_cor_new() {
    use std::time;

    println!("test_cor_new");

    let _cor1 = cor_newmutex!(
        |this| {
            println!("cor1 started");

            let s = cor_yield!(this, Some(String::from("given_to_outside")));
            println!("cor1 {:?}", s);
        },
        String,
        i16
    );
    let cor1 = _cor1.clone();

    let _cor2 = cor_newmutex!(
        move |this| {
            println!("cor2 started");

            println!("cor2 yield_from before");

            let s = cor_yield_from!(this, cor1, Some(3));
            println!("cor2 {:?}", s);
        },
        i16,
        i16
    );

    {
        let cor1 = _cor1.clone();
        cor1.lock().unwrap().set_async(true); // NOTE Cor default async
                                              // NOTE cor1 should keep async to avoid deadlock waiting.(waiting for each other)
    }
    {
        let cor2 = _cor2.clone();
        cor2.lock().unwrap().set_async(false);
        // NOTE cor2 is the entry point, so it could be sync without any deadlock.
    }
    cor_start!(_cor1);
    cor_start!(_cor2);

    thread::sleep(time::Duration::from_millis(100));
}