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
//! Architecture-neutral ownership for the Operating System event queue.
use std::cell::UnsafeCell;
use std::collections::VecDeque;
use std::ops::{Deref, DerefMut};
use std::rc::Rc;
/// A queued Mac event (mouseDown, mouseUp, keyDown, etc.).
///
/// The Operating System Event Manager owns one queue, and `EventRecord.where`
/// uses global coordinates. Inside Macintosh Volume I, I-244 and I-259.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct QueuedEvent {
/// Event type (1=mouseDown, 2=mouseUp, 3=keyDown, etc.).
pub what: u16,
/// Event message (key code for key events, window ptr for activate, etc.).
pub message: u32,
/// Mouse location at event time, in global Macintosh coordinates.
pub where_v: i16,
pub where_h: i16,
/// Modifier flags.
pub modifiers: u16,
}
/// One serialized event queue that can be attached to both CPU adapters.
///
/// Ordinary construction owns a private queue. The runner may explicitly
/// attach a second adapter to the same allocation once both adapters are its
/// private children and all access is serialized through a mutable runner
/// borrow.
#[derive(Debug, Default)]
pub(crate) struct SharedEventQueue(Rc<UnsafeCell<VecDeque<QueuedEvent>>>);
impl Clone for SharedEventQueue {
fn clone(&self) -> Self {
// A cloned runtime is a snapshot, not another live CPU adapter.
Self(Rc::new(UnsafeCell::new(self.deref().clone())))
}
}
impl SharedEventQueue {
/// Attach another CPU adapter to this queue without copying it.
///
/// # Safety
///
/// Every handle sharing this allocation must remain under one owner that
/// serializes access. No queue reference may remain live while another
/// handle reads or mutates the allocation.
pub(crate) unsafe fn shared_handle(&self) -> Self {
Self(Rc::clone(&self.0))
}
}
impl Deref for SharedEventQueue {
type Target = VecDeque<QueuedEvent>;
fn deref(&self) -> &Self::Target {
// SAFETY: shared handles can only be created under the serialized
// ownership contract documented by `shared_handle`.
unsafe { &*self.0.get() }
}
}
impl DerefMut for SharedEventQueue {
fn deref_mut(&mut self) -> &mut Self::Target {
// SAFETY: shared handles can only be created under the serialized
// ownership contract documented by `shared_handle`.
unsafe { &mut *self.0.get() }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn attached_handles_have_immediate_bidirectional_visibility() {
let mut first = SharedEventQueue::default();
// SAFETY: this test accesses the handles strictly in sequence.
let mut second = unsafe { first.shared_handle() };
first.push_back(QueuedEvent {
what: 3,
message: 0x1122_3344,
where_v: 10,
where_h: 20,
modifiers: 0x0100,
});
assert_eq!(second.front().map(|event| event.message), Some(0x1122_3344));
second.pop_front();
assert!(first.is_empty());
}
#[test]
fn clone_is_a_detached_snapshot() {
let mut live = SharedEventQueue::default();
live.push_back(QueuedEvent {
what: 3,
message: 0x1122_3344,
where_v: 10,
where_h: 20,
modifiers: 0x0100,
});
let mut snapshot = live.clone();
snapshot.front_mut().unwrap().message = 0x5566_7788;
assert_eq!(live.front().unwrap().message, 0x1122_3344);
assert_eq!(snapshot.front().unwrap().message, 0x5566_7788);
}
}