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
use crate::alertable::Alertable;
use crossbeam::epoch::{pin, Atomic, Guard, Owned};
use lockfree::queue::Queue;
use std::any::{Any, TypeId};
use std::ops::Deref;
use std::sync::atomic::{AtomicU64, Ordering};

#[derive(Debug)]
pub(crate) struct Event {
    pub type_id: TypeId,
    pub data: Box<dyn Any + Send + Sync>,
}

/// A wrapper that can be de-referenced to access and read the event.
///
/// Implements the [`Deref`] and ['AsRef`] traits to access the wrapped event.
///
/// # Example
///
/// Basic `Deref` usage:
///
/// ```ignore
/// let read_msg: usize = *readable_event;
/// ```
///
/// Basic `AsRef` usage:
///
/// ```ignore
/// let read_msg: &String = readable_event.as_ref();
/// ```
///
pub struct EventRead<'a, T: 'a> {
    _guard: Guard,
    raw: *const T,
    _marker: std::marker::PhantomData<&'a T>,
}

impl<'a, T> Deref for EventRead<'a, T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        unsafe { &*self.raw }
    }
}

impl<'a, T> AsRef<T> for EventRead<'a, T> {
    fn as_ref(&self) -> &T {
        &*self
    }
}

pub(crate) struct EventEnvelope {
    sequence: AtomicU64,
    event: Atomic<Event>,
    num_waiting: AtomicU64,
    subscribers: Queue<Option<Box<dyn Alertable + Send + Sync>>>,
}

impl EventEnvelope {
    pub fn new() -> Self {
        Self {
            sequence: AtomicU64::new(0),
            event: Atomic::null(),
            num_waiting: AtomicU64::new(0),
            subscribers: Queue::new(),
        }
    }

    pub fn sequence(&self) -> u64 {
        self.sequence.load(Ordering::Acquire)
    }

    pub fn start_waiting(&self) {
        self.num_waiting.fetch_add(1, Ordering::Acquire);
    }

    pub fn stop_waiting(&self) {
        self.num_waiting.fetch_sub(1, Ordering::Release);
    }

    pub fn add_subscriber(&self, alerter: Box<dyn Alertable + Send + Sync>) {
        self.subscribers.push(Some(alerter));
    }

    pub unsafe fn read<'a, T: 'static>(&self) -> Option<EventRead<'a, T>> {
        let guard = pin();

        let event = self.event.load(Ordering::Acquire, &guard).as_raw();

        if !event.is_null() && TypeId::of::<T>() == (*event).type_id {
            if let Some(event_data) = (*event).data.downcast_ref() {
                return Some(EventRead {
                    _guard: guard,
                    raw: &*event_data,
                    _marker: std::marker::PhantomData,
                });
            }
        }

        return None;
    }

    pub(crate) fn overwrite<T: 'static + Send + Sync>(&self, sequence: u64, data: T) {
        let mut event = Owned::new(Event {
            type_id: TypeId::of::<T>(),
            data: Box::new(data),
        });

        let guard = pin();

        loop {
            std::hint::spin_loop();

            let current_event = self.event.load(Ordering::Acquire, &guard);

            match self
                .event
                .compare_and_set(current_event, event, Ordering::AcqRel, &guard)
            {
                Ok(_) => {
                    self.sequence.store(sequence, Ordering::Release);

                    if !current_event.is_null() {
                        unsafe {
                            guard.defer_destroy(current_event);
                        }
                    }

                    loop {
                        match self.num_waiting.compare_exchange(
                            0,
                            0,
                            Ordering::Acquire,
                            Ordering::Relaxed,
                        ) {
                            Ok(_) => break,

                            Err(_) => {
                                let num_waiting = self.num_waiting.load(Ordering::Acquire);

                                for alerter_opt in
                                    self.subscribers.pop_iter().take(num_waiting as usize)
                                {
                                    if let Some(alerter) = alerter_opt {
                                        alerter.alert();
                                    }
                                }
                            }
                        }
                    }

                    break;
                }

                Err(cas_err) => {
                    event = cas_err.new;
                }
            }
        }
    }
}

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

    #[test]
    fn event_read() {
        let e = EventEnvelope::new();
        let r = unsafe { e.read::<String>() };
        assert!(r.is_none());
    }

    #[test]
    fn event_read_deref() {
        let e = EventEnvelope::new();
        let i: usize = 5555;
        e.overwrite(1, i);

        let r = unsafe { e.read::<usize>() }.unwrap();
        assert_eq!(5555, *r);
    }

    #[test]
    fn event_read_as_ref() {
        let e = EventEnvelope::new();
        e.overwrite(1, String::from("test"));

        let r = unsafe { e.read::<String>() }.unwrap();
        assert!(r.eq("test"));
    }

    #[test]
    fn event_overwrite() {
        let e = EventEnvelope::new();
        e.overwrite(1, String::from("Hello world!"));

        let readable_event = unsafe { e.read::<String>() };
        assert!(readable_event.is_some());

        let read_msg = &*readable_event.unwrap();
        let expected_msg = String::from("Hello world!");

        assert!(expected_msg.eq(read_msg));

        e.overwrite(1, String::from("Bye Felicia!"));

        let another_readable_event = unsafe { e.read::<String>() };
        assert!(another_readable_event.is_some());

        let another_read_msg = &*another_readable_event.unwrap();
        let another_expected_msg = String::from("Bye Felicia!");

        assert!(another_expected_msg.eq(another_read_msg));

        assert!(expected_msg.eq(read_msg));
    }
}