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
/*!
In this module there're implementations & tests of `Cor`.
*/
use std::sync::{
    atomic::{AtomicBool, Ordering},
    mpsc::{channel, Receiver, Sender},
    Arc, Mutex,
};
use std::thread;

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

# Arguments

* `X` - The generic type of yielded 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<X: 'static> {
    pub cor: Arc<Mutex<Cor<X>>>,
    pub val: Option<X>,
}
impl<X> CorOp<X> {}

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

# Arguments

* `X` - The generic type of yielded/yielding 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<X: 'static> {
    async: bool,

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

    op_ch_sender: Arc<Mutex<Sender<CorOp<X>>>>,
    op_ch_receiver: Arc<Mutex<Receiver<CorOp<X>>>>,
    result_ch_sender: Arc<Mutex<Sender<Option<X>>>>,
    result_ch_receiver: Arc<Mutex<Receiver<Option<X>>>>,

    effect: Arc<Mutex<FnMut(Arc<Mutex<Cor<X>>>) + Send + Sync + 'static>>,
}
impl<X: Send + Sync + Clone + 'static> Cor<X> {

    /**
    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<X>>>) + Send + Sync + 'static) -> Cor<X> {
        let (op_ch_sender, op_ch_receiver) = channel();
        let (result_ch_sender, result_ch_receiver) = channel();
        Cor {
            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)),
            result_ch_sender: Arc::new(Mutex::new(result_ch_sender)),
            result_ch_receiver: Arc::new(Mutex::new(result_ch_receiver)),

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

    /**
    Generate a new `Arc<Mutex<Cor<X>>>` 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<X>>>) + Send + Sync + 'static,
    ) -> Arc<Mutex<Cor<X>>> {
        Arc::new(Mutex::new(<Cor<X>>::new(effect)))
    }

    /**
    Make `this` sends a given `Option<X>` to `target`,
    and this method returns the 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(
        this: Arc<Mutex<Cor<X>>>,
        target: Arc<Mutex<Cor<X>>>,
        sent_to_inside: Option<X>,
    ) -> Option<X> {
        let _result_ch_receiver;

        // me MutexGuard lifetime block
        {
            let _me = this.clone();
            let mut me = _me.lock().unwrap();
            if !me.is_alive() {
                return None;
            }
            _result_ch_receiver = me.result_ch_receiver.clone();
        }

        // target MutexGuard lifetime block
        {
            {
                target
                    .lock()
                    .unwrap()
                    .receive(this.clone(), sent_to_inside);
            }

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

            match result.ok() {
                Some(_x) => {
                    return _x;
                }
                None => {}
            }
        }

        return None;
    }

    /**
    Make `this` returns a given `None::<X>` to its callee `Cor`,
    and this method returns the 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<X>>>) -> Option<X> {
        return Cor::yield_ref(this, None);
    }

    /**
    Make `this` returns a given `Option<X>` `given_to_outside` to its callee `Cor`,
    and this method returns the 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<X>>>, given_to_outside: Option<X>) -> Option<X> {
        let _op_ch_receiver;
        // me MutexGuard lifetime block
        {
            let _me = this.clone();
            let mut 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();
        }

        match op.ok() {
            Some(_x) => {
                {
                    let mut cor_other = _x.cor.lock().unwrap();
                    cor_other.offer(given_to_outside);
                }

                return _x.val;
            }
            None => {}
        }

        return 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<X>>>) {
        let async;

        {
            let _started_alive;

            {
                let _me = this.clone();
                let me = _me.lock().unwrap();
                async = me.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 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, async: bool) {
        self.async = async;
    }

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

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

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

    */
    pub fn is_alive(&mut self) -> bool {
        let _started_alive = self.started_alive.clone();
        let started_alive = _started_alive.lock().unwrap();
        let &(_, ref alive) = &*started_alive;
        return 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::<X>`.
    (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());
                drop(self.result_ch_sender.lock().unwrap());
            }
        }
    }

    fn receive(&mut self, cor: Arc<Mutex<Cor<X>>>, given_as_request: Option<X>) {
        let _op_ch_sender = self.op_ch_sender.clone();
        let _given_as_request = Box::new(given_as_request);

        // 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 given_as_request = _given_as_request.clone();
            let _result = _op_ch_sender.lock().unwrap().send(CorOp {
                cor: cor,
                val: *given_as_request,
            });
        }
    }

    fn offer(&mut self, received_as_response: Option<X>) {
        let _result_ch_sender = self.result_ch_sender.clone();
        let _received_as_response = Box::new(received_as_response);
        self.do_close_safe(move || {
            let result_ch_sender = _result_ch_sender.clone();
            let received_as_response = _received_as_response.clone();

            let _result = result_ch_sender.lock().unwrap().send(*received_as_response);
        });
    }

    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_new() {
    use std::time;

    println!("test_cor_new");

    let _cor1 = <Cor<String>>::new_with_mutex(|this| {
        println!("cor1 started");

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

    let _cor2 = <Cor<String>>::new_with_mutex(move |this| {
        println!("cor2 started");

        println!("cor2 yield_from before");

        let s = Cor::yield_from(this.clone(), cor1.clone(), Some(String::from("3")));
        println!("cor2 {:?}", s);
    });

    {
        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.clone());
    Cor::start(_cor2.clone());

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