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
use std::fmt;
use std::marker::PhantomData;
use std::time::Duration;

use serde::{Deserialize, Serialize};
use thiserror::Error;

use crate::function::process::{IntoProcess, NoLink};
use crate::host::api::message;
use crate::serializer::{Bincode, CanSerialize, DecodeError};
use crate::{host, LunaticError, Process, ProcessConfig, Tag};

pub const DATA_MESSAGE: u32 = 0;
pub const LINK_DIED: u32 = 1;
pub const PROCESS_DIED: u32 = 2;
pub const TIMEOUT: u32 = 9027;

pub type MailboxResult<T, U = ()> = Result<MessageSignal<T, U>, MailboxError>;

/// The mailbox of a [`Process`].
///
/// Each process in lunatic gets one dedicated mailbox. Messages sent to the
/// process will end up in this mailbox. Each [`Process`] and [`Mailbox`] pair
/// have matching message and serializer types, because of this de/serialization
/// should never fail.
///
/// One case where deserialization might fail is when the `Mailbox` type is used
/// on a function inside an external WebAssembly module that is loaded by
/// [`WasmModule`](crate::WasmModule). In this case we don't have any
/// compile-time information about what messages are going to be received by
/// this mailbox. For such cases the function
/// [`try_receive`](./struct.Mailbox.html#method.try_receive) can be used. It
/// will not panic in case it can't deserialize the message buffer.
///
/// ## Message ordering
///
/// Lunatic guarantees that messages sent between two processes will arrive in
/// the same order they were sent. Ordering is not guaranteed if more than two
/// processes are involved.
///
/// ## Link deaths
///
/// By default, if a linked process fails all the links will die too. This
/// behavior can be changed by using the [`catch_link_failure`]() function. The
/// returned [`Mailbox<_, _, Catching>`] will receive a special
/// [`MailboxError::LinkDied`] in its mailbox containing the [`Tag`] used when
/// the process was spawned ([`spawn_link_tag`](Process::spawn_link_tag)).
pub struct Mailbox<M, S = Bincode, L = ()>
where
    S: CanSerialize<M>,
{
    phantom: PhantomData<(M, S, L)>,
}

impl<M, S> Mailbox<M, S, ()>
where
    S: CanSerialize<M>,
{
    /// Gets next message from process' mailbox.
    ///
    /// If the mailbox is empty, this function will block until a new message
    /// arrives.
    ///
    /// # Panics
    ///
    /// This function will panic if the received message can't be deserialized
    /// into `M` with serializer `S`.
    #[track_caller]
    pub fn receive(&self) -> M {
        self.receive_(&[], None).unwrap().unwrap_message()
    }

    /// Gets next message from process' mailbox that is tagged with one of the
    /// `tags`.
    ///
    /// If no such message exists, this function will block until a new message
    /// arrives. If `tags` is an empty array, it will behave the same as
    /// `receive`.
    ///
    /// # Panics
    ///
    /// This function will panic if the received message can't be deserialized
    /// into `M` with serializer `S`.
    #[track_caller]
    pub fn tag_receive(&self, tags: &[Tag]) -> M {
        self.receive_(tags, None).unwrap().unwrap_message()
    }

    /// Same as `receive`, but doesn't panic in case the deserialization fails.
    /// Instead, it will return [`MailboxError::DeserializationFailed`].
    pub fn try_receive(&self) -> Result<M, MailboxError> {
        self.receive_(&[], None).map(MessageSignal::unwrap_message)
    }

    /// Same as `receive`, but only waits for the duration of timeout for the
    /// message. If the timeout expires it will return
    /// [`MailboxError::TimedOut`].
    pub fn receive_timeout(&self, timeout: Duration) -> Result<M, MailboxError> {
        self.receive_(&[], Some(timeout))
            .map(MessageSignal::unwrap_message)
    }

    /// Same as `tag_receive`, but only waits for the duration of timeout for
    /// the message. If the timeout expires it will return
    /// [`MailboxError::TimedOut`].
    pub fn tag_receive_timeout(&self, tags: &[Tag], timeout: Duration) -> Result<M, MailboxError> {
        self.receive_(tags, Some(timeout))
            .map(MessageSignal::unwrap_message)
    }

    /// Allow this mailbox to catch link failures.
    ///
    /// This function returns a [`Mailbox`] that will get a
    /// [`LinkDiedSignal`] message every time a linked process dies.
    pub fn catch_link_failure(self) -> Mailbox<M, S, LinkDiedSignal> {
        unsafe {
            host::api::process::die_when_link_dies(0);
            Mailbox::new()
        }
    }

    /// Allows this mailbox to monitor other processes.
    ///
    /// This function returns a [`Mailbox`] that will get a [`ProcessDiedSignal`] message
    /// whenever a monitored process dies.
    pub fn monitorable(self) -> Mailbox<M, S, ProcessDiedSignal> {
        unsafe { Mailbox::new() }
    }
}

macro_rules! impl_mailbox_receive {
    ($signal:ty) => {
        impl<M, S> Mailbox<M, S, $signal>
        where
            S: CanSerialize<M>,
        {
            /// Gets next message or signal from process' mailbox.
            ///
            /// If the mailbox is empty, this function will block until a new message
            /// arrives.
            ///
            /// # Panics
            ///
            /// This function will panic if the received message can't be deserialized
            /// into `M` with serializer `S`.
            pub fn receive(&self) -> MessageSignal<M, $signal> {
                self.try_receive().unwrap()
            }

            /// Same as `receive`, but doesn't panic in case the deserialization fails.
            /// Instead, it will return [`MailboxError::DeserializationFailed`].
            pub fn try_receive(&self) -> MailboxResult<M, $signal> {
                self.receive_(&[], None)
                    .map(|message| message.try_into().unwrap())
            }

            /// Gets next message from process' mailbox that is tagged with one of the
            /// `tags`.
            ///
            /// If no such message exists, this function will block until a new message
            /// arrives. If `tags` is an empty array, it will behave the same as
            /// `receive`.
            ///
            /// This function can also be used to await the death of specific linked
            /// processes. In this case the `tags` array should contain tags
            /// corresponding to the processes we are awaiting to die.
            pub fn tag_receive(&self, tags: &[Tag]) -> MessageSignal<M, $signal> {
                self.try_tag_receive(tags).unwrap()
            }

            /// Same as `tag_receive`, but doesn't panic in case the deserialization fails.
            /// Instead, it will return [`MailboxError::DeserializationFailed`].
            pub fn try_tag_receive(&self, tags: &[Tag]) -> MailboxResult<M, $signal> {
                self.receive_(tags, None)
                    .map(|message| message.try_into().unwrap())
            }

            /// Same as `receive`, but only waits for the duration of timeout for the
            /// message. If the timeout expires it will return
            /// [`MailboxError::TimedOut`].
            pub fn receive_timeout(&self, timeout: Duration) -> MailboxResult<M, $signal> {
                self.receive_(&[], Some(timeout))
                    .map(|message| message.try_into().unwrap())
            }

            /// Same as `tag_receive`, but only waits for the duration of timeout for
            /// the message. If the timeout expires it will return
            /// [`MailboxError::TimedOut`].
            pub fn tag_receive_timeout(
                &self,
                tags: &[Tag],
                timeout: Duration,
            ) -> MailboxResult<M, $signal> {
                self.receive_(tags, Some(timeout))
                    .map(|message| message.try_into().unwrap())
            }
        }
    };
}

impl_mailbox_receive!(LinkDiedSignal);
impl_mailbox_receive!(ProcessDiedSignal);
impl_mailbox_receive!(Signal);

/// A mailbox that is catching dead links.
impl<M, S> Mailbox<M, S, LinkDiedSignal>
where
    S: CanSerialize<M>,
{
    /// Allows this mailbox to monitor other processes.
    ///
    /// This function returns a [`Mailbox`] that will get a [`Signal::ProcessDied`] message
    /// whenever a monitored process dies.
    pub fn monitorable(self) -> Mailbox<M, S, Signal> {
        unsafe { Mailbox::new() }
    }
}

/// A mailbox that is monitoring other processes.
impl<M, S> Mailbox<M, S, ProcessDiedSignal>
where
    S: CanSerialize<M>,
{
    /// Allow this mailbox to catch link failures.
    ///
    /// This function returns a [`Mailbox`] that will get a
    /// [`Signal::LinkDied`] message every time a linked process dies.
    pub fn catch_link_failure(self) -> Mailbox<M, S, Signal> {
        unsafe {
            host::api::process::die_when_link_dies(0);
            Mailbox::new()
        }
    }

    /// Starts monitoring a process, .
    pub fn monitor<T, U>(&self, process: Process<T, U>) {
        unsafe { host::api::process::monitor(process.id()) };
    }

    /// Stop monitoring a process.
    pub fn stop_monitoring<T, U>(&self, process: Process<T, U>) {
        unsafe { host::api::process::stop_monitoring(process.id()) };
    }
}

impl<M, S, L> Mailbox<M, S, L>
where
    S: CanSerialize<M>,
{
    /// Create a mailbox with a specific type.
    ///
    /// ### Safety
    ///
    /// It's not safe to mix different types of mailboxes inside one process.
    /// This function should never be used directly. The only reason it's public
    /// is that it's used inside the `main` macro and needs to be available
    /// outside this crate.
    pub unsafe fn new() -> Self {
        Self {
            phantom: PhantomData {},
        }
    }

    /// Returns a reference to the currently running process
    pub fn this(&self) -> Process<M, S> {
        unsafe { Process::new(host::node_id(), host::process_id()) }
    }

    fn receive_(&self, tags: &[Tag], timeout: Option<Duration>) -> MailboxResult<M, Signal> {
        let tags: Vec<i64> = tags.iter().map(|tag| tag.id()).collect();
        let timeout_ms = match timeout {
            Some(timeout) => timeout.as_millis() as u64,
            None => u64::MAX,
        };
        let message_type = unsafe { message::receive(tags.as_ptr(), tags.len(), timeout_ms) };
        match message_type {
            DATA_MESSAGE => match S::decode() {
                Ok(msg) => Ok(MessageSignal::Message(msg)),
                Err(err) => Err(MailboxError::DeserializationFailed(err)),
            },
            LINK_DIED => Ok(MessageSignal::Signal(Signal::LinkDied(unsafe {
                Tag::from(message::get_tag())
            }))),
            PROCESS_DIED => Ok(MessageSignal::Signal(Signal::ProcessDied(unsafe {
                message::get_process_id()
            }))),
            TIMEOUT => Err(MailboxError::TimedOut),
            _ => panic!("unknown message type: {message_type}"),
        }
    }
}

impl<M, S, L> Clone for Mailbox<M, S, L>
where
    S: CanSerialize<M>,
{
    fn clone(&self) -> Self {
        Self {
            phantom: self.phantom,
        }
    }
}

impl<M, S, L> Copy for Mailbox<M, S, L> where S: CanSerialize<M> {}

impl<M, S, L> fmt::Debug for Mailbox<M, S, L>
where
    S: CanSerialize<M>,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Mailbox")
            .field("message", &std::any::type_name::<M>())
            .field("serializer", &std::any::type_name::<S>())
            .field("link", &std::any::type_name::<L>())
            .finish()
    }
}

/// Result of a `recieve*` call on a [`Mailbox`].
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub enum MessageSignal<T, U> {
    Message(T),
    Signal(U),
}

/// An error returned when receiving from a mailbox.
#[derive(Error, Debug)]
pub enum MailboxError {
    /// Message failed to be deserialized.
    #[error("deserialization failed: {0}")]
    DeserializationFailed(#[from] DecodeError),
    /// Receive message timed out.
    #[error("timed out")]
    TimedOut,
}

/// A signal received when a link dies or monitored process dies.
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub enum Signal {
    /// A linked process died.
    LinkDied(Tag),
    /// A monitored process died.
    ProcessDied(u64),
}

/// A linked process died.
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub struct LinkDiedSignal(pub Tag);

/// A monitored process died.
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub struct ProcessDiedSignal(pub u64);

impl<T, U> MessageSignal<T, U> {
    /// Unwraps the inner message, otherwise panics.
    pub fn unwrap_message(self) -> T {
        match self {
            MessageSignal::Message(m) => m,
            MessageSignal::Signal(_) => panic!("MessageSignal was a signal"),
        }
    }

    /// Unwraps the inner signal, otherwise panics.
    pub fn unwrap_signal(self) -> U {
        match self {
            MessageSignal::Message(_) => panic!("MessageSignal was a message"),
            MessageSignal::Signal(s) => s,
        }
    }

    // Returns true if it's a regular message.
    pub fn is_message(&self) -> bool {
        matches!(self, MessageSignal::Message(_))
    }

    // Returns true if it's a signal.
    pub fn is_signal(&self) -> bool {
        matches!(self, MessageSignal::Signal(_))
    }
}

impl MailboxError {
    /// Returns true if the error is a [`MailboxError::TimedOut`].
    pub fn is_timed_out(&self) -> bool {
        matches!(self, MailboxError::TimedOut)
    }

    /// Returns true if the error is a [`MailboxError::DeserializationFailed`].
    pub fn is_deserialization_failed(&self) -> bool {
        matches!(self, MailboxError::DeserializationFailed(_))
    }
}

/// Error returned when converting a [`MessageSignal`].
#[derive(Clone, Copy, Debug)]
pub struct MessageSignalConvertError;

impl<T> TryFrom<MessageSignal<T, Signal>> for MessageSignal<T, ()> {
    type Error = MessageSignalConvertError;

    fn try_from(value: MessageSignal<T, Signal>) -> Result<Self, Self::Error> {
        match value {
            MessageSignal::Message(m) => Ok(MessageSignal::Message(m)),
            MessageSignal::Signal(_) => Err(MessageSignalConvertError),
        }
    }
}

impl<T> TryFrom<MessageSignal<T, Signal>> for MessageSignal<T, LinkDiedSignal> {
    type Error = MessageSignalConvertError;

    fn try_from(value: MessageSignal<T, Signal>) -> Result<Self, Self::Error> {
        match value {
            MessageSignal::Message(m) => Ok(MessageSignal::Message(m)),
            MessageSignal::Signal(Signal::LinkDied(tag)) => {
                Ok(MessageSignal::Signal(LinkDiedSignal(tag)))
            }
            MessageSignal::Signal(Signal::ProcessDied(_)) => Err(MessageSignalConvertError),
        }
    }
}

impl<T> TryFrom<MessageSignal<T, Signal>> for MessageSignal<T, ProcessDiedSignal> {
    type Error = MessageSignalConvertError;

    fn try_from(value: MessageSignal<T, Signal>) -> Result<Self, Self::Error> {
        match value {
            MessageSignal::Message(m) => Ok(MessageSignal::Message(m)),
            MessageSignal::Signal(Signal::LinkDied(_)) => Err(MessageSignalConvertError),
            MessageSignal::Signal(Signal::ProcessDied(id)) => {
                Ok(MessageSignal::Signal(ProcessDiedSignal(id)))
            }
        }
    }
}

impl<M, S> NoLink for Mailbox<M, S> where S: CanSerialize<M> {}

impl<M, S> IntoProcess<M, S> for Mailbox<M, S>
where
    S: CanSerialize<M>,
{
    type Process = Process<M, S>;

    fn spawn<C>(
        capture: C,
        entry: fn(C, Self),
        name: Option<&str>,
        link: Option<Tag>,
        config: Option<&ProcessConfig>,
        node: Option<u64>,
    ) -> Result<Self::Process, LunaticError>
    where
        S: CanSerialize<C> + CanSerialize<M>,
    {
        let entry = entry as usize as i32;
        let node_id = node.unwrap_or_else(host::node_id);

        // The `type_helper_wrapper` function is used here to create a pointer to a
        // function with generic types C, M & S. We can only send pointer data across
        // processes and this is the only way the Rust compiler will let us transfer
        // this information into the new process.
        match host::spawn(
            name,
            node,
            config,
            link,
            type_helper_wrapper::<C, M, S>,
            entry,
        ) {
            Ok(id) => {
                // If the captured variable is of size 0, we don't need to send it to another
                // process.
                if std::mem::size_of::<C>() == 0 {
                    Ok(unsafe { Process::new(node_id, id) })
                } else {
                    let child = unsafe { Process::<C, S>::new(node_id, id) };
                    child.send(capture);
                    // Processes can only receive one type of message, but to pass in the captured
                    // variable we pretend for the first message that our process is receiving
                    // messages of type `C`.
                    Ok(unsafe { std::mem::transmute(child) })
                }
            }
            Err(err) => Err(err),
        }
    }
}

/// Wrapper function to help transfer the generic types C, M & S into the new
/// process.
fn type_helper_wrapper<C, M, S>(function: i32)
where
    S: CanSerialize<C> + CanSerialize<M>,
{
    // If the captured variable is of size 0, don't wait on it.
    let captured = if std::mem::size_of::<C>() == 0 {
        unsafe { std::mem::MaybeUninit::<C>::zeroed().assume_init() }
    } else {
        unsafe { Mailbox::<C, S>::new() }.receive()
    };
    let mailbox = unsafe { Mailbox::new() };
    let function: fn(C, Mailbox<M, S>) = unsafe { std::mem::transmute(function as usize) };
    function(captured, mailbox);
}

#[cfg(test)]
mod tests {
    use std::time::Duration;

    use lunatic_test::test;

    use super::*;
    use crate::{sleep, Mailbox};

    #[test]
    fn mailbox() {
        let child = Process::spawn(1, |capture, mailbox: Mailbox<i32>| {
            assert_eq!(capture, 1);
            assert_eq!(mailbox.receive(), 2);
        });

        child.send(2);
        sleep(Duration::from_millis(100));
    }

    #[test]
    #[should_panic]
    fn mailbox_link() {
        Process::spawn_link((), |_, _: Mailbox<()>| {
            panic!("fails");
        });

        // This process should fail before 100ms, because the link panics.
        sleep(Duration::from_millis(100));
    }
}