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
//! # may_actor
//!
//! rust native actor library based on [may](https://github.com/Xudong-Huang/may)
//!
//! ## Features
//!
//! - run closure asynchronously by send message ([`Actor.call`])
//! - run closure synchronously with the actor's internal state ([`Actor.with`])
//! - convert from raw instance reference to Actor ([`Actor.from`])
//! - allow panic inside a closure message
//!
//! ## Notice
//!
//! This simple library doesn't support spawn actors across processes
//!
//! [`Actor.call`]: ./struct.Actor.html#method.call
//! [`Actor.with`]: ./struct.Actor.html#method.with
//! [`Actor.from`]: ./struct.Actor.html#method.from
//!
#![deny(missing_docs)]

#[doc(hidden)]
#[macro_use]
extern crate may;

use std::cell::UnsafeCell;
use std::panic::{self, RefUnwindSafe};
use std::sync::atomic::Ordering;
use std::sync::{Arc, Weak};

use may::sync::{mpsc, AtomicOption, Blocker};

#[doc(hidden)]
trait FnBox: Send {
    fn call_box(self: Box<Self>);
}

impl<F: FnOnce() + Send> FnBox for F {
    fn call_box(self: Box<Self>) {
        (*self)()
    }
}

// we must use repr(C) to fix the mem layout
#[repr(C)]
#[derive(Debug)]
struct ActorImpl<T> {
    data: UnsafeCell<T>,
    tx: mpsc::Sender<Box<FnBox>>,
}

unsafe impl<T: Send> Sync for ActorImpl<T> {}
impl<T> RefUnwindSafe for ActorImpl<T> {}

impl<T> ActorImpl<T> {
    fn new(data: T) -> Self {
        let (tx, rx) = mpsc::channel::<Box<FnBox>>();
        // when all tx are dropped, the coroutine would exit
        go!(move || for f in rx.into_iter() {
            panic::catch_unwind(panic::AssertUnwindSafe(move || {
                f.call_box();
            })).ok();
        });

        ActorImpl {
            data: UnsafeCell::new(data),
            tx: tx,
        }
    }

    fn get_mut(&self) -> &mut T {
        unsafe { &mut *self.data.get() }
    }
}

/// coroutine based Actor.
///
/// The type `Actor<T>` wraps `T` into an Actor.
/// You can send message to the actor by calling it's [`call`] method.
/// You can run a closure synchronously with the actor internal state by calling it's [`with`] method.
///
/// # Examples
///
/// ```
/// use may_actor::Actor;
///
/// let a = Actor::new(40);
/// a.call(|me| *me += 2);
/// a.with(|me| assert_eq!(*me, 42));
/// ```
///
/// [`call`]: ./struct.Actor.html#method.call
/// [`with`]: ./struct.Actor.html#method.with
#[derive(Debug)]
pub struct Actor<T> {
    inner: Arc<ActorImpl<T>>,
}

unsafe impl<T> Send for Actor<T> {}

impl<T> Clone for Actor<T> {
    fn clone(&self) -> Self {
        Actor {
            inner: self.inner.clone(),
        }
    }
}

impl<T> Actor<T> {
    /// create an actor by consuming the actual actor implementation
    pub fn new(actor: T) -> Self {
        Actor {
            inner: Arc::new(ActorImpl::new(actor)),
        }
    }

    /// create an actor with a driver coroutine running in backgroud
    /// when all actor instances got dropped, the driver coroutine
    /// would be cancelled
    pub fn drive_new<F>(data: T, f: F) -> Self
    where
        F: FnOnce(DriverActor<T>) + Send + 'static,
        T: Send + 'static,
    {
        let (tx, rx) = mpsc::channel::<Box<FnBox>>();

        let actor = Actor {
            inner: Arc::new(ActorImpl {
                data: UnsafeCell::new(data),
                tx: tx,
            }),
        };

        // create the back groud driver
        let driver_para = Arc::downgrade(&actor.inner);
        let driver = go!(|| f(DriverActor { inner: driver_para }));

        // when all tx are dropped, the coroutine would exit
        go!(move || {
            for f in rx.into_iter() {
                panic::catch_unwind(panic::AssertUnwindSafe(move || {
                    f.call_box();
                })).ok();
            }
            // when all the actor instances dropped, cancel the driver
            unsafe { driver.coroutine().cancel() };
            driver.join().ok();
            // info!("actor coroutine done");
        });

        actor
    }

    /// convert from inner ref to actor
    ///
    /// ## Safety
    /// only valid if `&T`is coming from an actor.
    /// normally this is used to convert `&self` to `Actor<T>`
    pub unsafe fn from(inner: &T) -> Self {
        // how to find the outer wrapper?
        let m: *const ActorImpl<T> = (inner as *const _ as usize) as *const _;
        let arc = Arc::from_raw(m);
        let ret = Actor { inner: arc.clone() };
        std::mem::forget(arc);
        ret
    }

    /// send the actor a 'message' by a closure.
    ///
    /// the closure would get the `&mut T` as parameter,
    /// so that you can manipulate its internal state.
    ///
    /// the raw actor type must be `Send` and `'static`
    /// so that it can be used by multi threads.
    ///
    /// the closure would be executed asynchronously
    pub fn call<F>(&self, f: F)
    where
        F: FnOnce(&mut T) + Send + 'static,
        T: Send + 'static,
    {
        let actor = self.inner.clone();
        let f = move || {
            let data = actor.get_mut();
            f(data);
        };

        self.inner.tx.send(Box::new(f)).unwrap();
    }

    /// execute a closure in the actor's coroutine context
    /// and wait for the result.
    ///
    /// This is a sync version of `call` method, it will
    /// block until finished, panic will be propogate to the
    /// caller's context.
    /// You can use this method to monitor the internal state
    pub fn with<R, F>(&self, f: F) -> R
    where
        F: FnOnce(&mut T) -> R + Send,
        T: Send,
        R: Send,
    {
        let blocker = Blocker::current();
        let ret = Arc::new(AtomicOption::none());
        let err = Arc::new(AtomicOption::none());

        {
            let ret = ret.clone();
            let err = err.clone();
            let blocker = blocker.clone();
            let actor = self.inner.clone();

            let f = move || {
                let exit = panic::catch_unwind(panic::AssertUnwindSafe(|| f(actor.get_mut())));
                match exit {
                    Ok(r) => {
                        ret.swap(r, Ordering::Relaxed);
                    }
                    Err(e) => {
                        err.swap(e, Ordering::Relaxed);
                    }
                }
                blocker.unpark();
            };

            let closure: Box<FnBox> = Box::new(f);
            // erase the lifetime of boxed closure
            let closure: Box<FnBox + 'static> = unsafe { std::mem::transmute(closure) };

            self.inner.tx.send(closure).unwrap();
        }

        // wait until the viewer pause the message processing
        match blocker.park(None) {
            Ok(_) => match ret.take(Ordering::Relaxed) {
                Some(v) => v,
                None => match err.take(Ordering::Relaxed) {
                    Some(panic) => panic::resume_unwind(panic),
                    None => unreachable!("failed to get result"),
                },
            },
            // impossible be a timeout err
            // cancel happened, we do nothing here
            Err(_) => may::coroutine::trigger_cancel_panic(),
        }
    }

    /// get the heap address as key, unique for each actor
    /// can be used to compare if two actors are the same
    pub fn key(&self) -> usize {
        let inner = self.inner.clone();
        let addr = Arc::into_raw(inner);
        let key = addr as usize;
        unsafe { Arc::from_raw(addr) };
        key
    }
}

/// parameter used in driver coroutine function
#[derive(Debug)]
pub struct DriverActor<T> {
    // hold a weak pointer
    // so that have chance to clean up
    inner: Weak<ActorImpl<T>>,
}

impl<T> DriverActor<T> {
    /// same as Actor.call
    pub fn call<F>(&self, f: F)
    where
        F: FnOnce(&mut T) + Send + 'static,
        T: Send + 'static,
    {
        // if the actor is gone, cancel the current coroutine
        let actor = match self.inner.upgrade() {
            None => may::coroutine::trigger_cancel_panic(),
            Some(inner) => Actor { inner },
        };
        actor.call(f);
    }

    /// same as Actor.with
    pub fn with<R, F>(&self, f: F) -> R
    where
        F: FnOnce(&mut T) -> R + Send,
        T: Send,
        R: Send,
    {
        // if the actor is gone, cancel the current coroutine
        let actor = match self.inner.upgrade() {
            None => may::coroutine::trigger_cancel_panic(),
            Some(inner) => Actor { inner },
        };
        actor.with(f)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn it_works() {
        let i = 0u32;
        let a = Actor::new(i);
        a.call(|me| *me += 2);
        a.call(|_me| panic!("support panic inside"));
        a.call(|me| *me += 4);
        // the with mothod would wait previous messages process done
        a.with(|me| assert_eq!(*me, 6));
    }

    #[test]
    fn ping_pong() {
        struct Ping {
            count: u32,
            tx: mpsc::Sender<()>,
        };

        struct Pong {
            count: u32,
        };

        impl Ping {
            fn ping(&mut self, to: Actor<Pong>) {
                if self.count > 10 {
                    self.tx.send(()).unwrap();
                    return;
                }

                println!("ping called, count={}", self.count);
                self.count += 1;
                let ping = unsafe { Actor::from(self) };
                to.call(move |pong| pong.pong(ping));
            }
        }

        impl Pong {
            fn pong(&mut self, to: Actor<Ping>) {
                println!("pong called, count={}", self.count);
                self.count += 1;
                let pong = unsafe { Actor::from(self) };
                to.call(|ping| ping.ping(pong))
            }
        }

        let (tx, rx) = mpsc::channel();

        let ping = Actor::new(Ping { count: 0, tx: tx });
        let pong = Actor::new(Pong { count: 0 });

        {
            let pong = pong.clone();
            ping.call(|me| me.ping(pong));
        }

        // wait the message process finish
        rx.recv().unwrap();

        ping.with(|me| assert_eq!(me.count, 11));
        pong.with(|me| assert_eq!(me.count, 11));
    }

    #[test]
    fn driver_actor() {
        use std::sync::atomic::{AtomicBool, Ordering};
        use std::time::Duration;

        #[derive(Debug)]
        struct DropFlag {
            flag: Arc<AtomicBool>,
        }

        impl Drop for DropFlag {
            fn drop(&mut self) {
                self.flag.store(true, Ordering::Relaxed);
            }
        }

        let flag = Arc::new(AtomicBool::new(false));

        let drop_flag = DropFlag { flag: flag.clone() };

        let actor = Actor::drive_new(0, move |me| loop {
            me.call(|v| {
                *v += 1;
                println!("new_value = {}", *v)
            });
            assert_eq!(drop_flag.flag.load(Ordering::Relaxed), false);
            may::coroutine::sleep(Duration::from_secs(1));
        });

        may::coroutine::sleep(Duration::from_secs(3));
        drop(actor);
        // wait some time for the driver coroutine exit
        may::coroutine::sleep(Duration::from_millis(100));
        assert_eq!(flag.load(Ordering::Relaxed), true);
    }

    #[test]
    fn with_test() {
        let mut i = 100;
        let a = Actor::new(0);
        let v = a.with(|me| {
            *me += 2;
            i += *me;
            *me
        });
        assert_eq!(100 + v, i);
    }

    #[test]
    #[should_panic]
    fn with_panic() {
        let a = Actor::new(0);
        a.with(|_| panic!("painic inside"));
    }

    #[test]
    fn test_key() {
        let a = Actor::new(0);
        let b = a.clone();
        // b.with(|_| println!("key = {}", a.key()));
        assert_eq!(a.key(), b.key());
    }
}