summaryrefslogtreecommitdiffstats
path: root/src/input/thread/state.rs
blob: 43fb478549dcbfe784ede63bf7113f088b5a6e83 (plain)
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
use std::{
	collections::VecDeque,
	mem,
	sync::{
		atomic::{AtomicBool, Ordering},
		Arc,
	},
	time::Duration,
};

use parking_lot::Mutex;

use crate::input::Event;

const MAXIMUM_EVENTS: usize = 100;
const EVENT_POLL_TIMEOUT: Duration = Duration::from_secs(1);

/// Input thread state.
#[derive(Clone, Debug)]
pub(crate) struct State {
	ended: Arc<AtomicBool>,
	event_queue: Arc<Mutex<VecDeque<Event>>>,
	paused: Arc<AtomicBool>,
	update_receiver: crossbeam_channel::Receiver<()>,
	update_sender: crossbeam_channel::Sender<()>,
}

impl State {
	pub(crate) fn new() -> Self {
		let (update_sender, update_receiver) = crossbeam_channel::unbounded();
		Self {
			ended: Arc::new(AtomicBool::from(false)),
			event_queue: Arc::new(Mutex::new(VecDeque::new())),
			paused: Arc::new(AtomicBool::from(false)),
			update_receiver,
			update_sender,
		}
	}

	fn send_update(&self) {
		let _result = self.update_sender.send(());
	}

	pub(crate) fn is_paused(&self) -> bool {
		self.paused.load(Ordering::Acquire)
	}

	pub(crate) fn is_ended(&self) -> bool {
		self.ended.load(Ordering::Acquire)
	}

	/// Pause the event read thread.
	pub(crate) fn pause(&self) {
		self.paused.store(true, Ordering::Release);
	}

	/// Resume the event read thread.
	pub(crate) fn resume(&self) {
		self.paused.store(false, Ordering::Release);
	}

	/// Permanently End the event read thread.
	pub(crate) fn end(&self) {
		self.ended.store(true, Ordering::Release);
	}

	/// Add an event after existing events.
	pub(crate) fn enqueue_event(&self, event: Event) {
		let mut events = self.event_queue.lock();
		let last_resize_event_maybe = matches!(event, Event::Resize(..))
			.then(|| events.back_mut().filter(|e| matches!(*e, &mut Event::Resize(..))))
			.flatten();

		if let Some(last_resize_event) = last_resize_event_maybe {
			let _old = mem::replace(last_resize_event, event);
		}
		else if events.len() < MAXIMUM_EVENTS {
			events.push_back(event);
		}
		self.send_update();
	}

	/// Add an event before existing events.
	pub(crate) fn push_event(&self, event: Event) {
		let mut events = self.event_queue.lock();
		if events.len() >= MAXIMUM_EVENTS {
			_ = events.pop_back();
		}
		events.push_front(event);
		self.send_update();
	}

	/// Read an event from the queue. This function will block for a while until an event is
	/// available. And if no event is available, it will return `Event::None`.
	#[must_use]
	pub(crate) fn read_event(&self) -> Event {
		// clear existing message since last read
		while self.update_receiver.try_recv().is_ok() {}
		loop {
			if let Some(event) = self.event_queue.lock().pop_front() {
				return event;
			}

			// if there is no event available on the queue, instead of returning early, we can wait
			// for the new event message and try again.
			if self.update_receiver.recv_timeout(EVENT_POLL_TIMEOUT).is_ok() {
				continue;
			}

			// We always return if the above recv call times out, to ensure this does not block
			// forever
			return Event::None;
		}
	}
}

#[cfg(test)]
mod tests {
	use std::{
		sync::atomic::AtomicUsize,
		thread::{sleep, spawn},
	};

	use super::*;
	use crate::input::Event;

	fn create_state() -> State {
		State::new()
	}

	#[test]
	fn paused() {
		let state = create_state();
		state.pause();
		assert!(state.is_paused());
	}

	#[test]
	fn resumed() {
		let state = create_state();
		state.resume();
		assert!(!state.is_paused());
	}

	#[test]
	fn ended() {
		let state = create_state();
		state.end();
		assert!(state.is_ended());
	}

	#[test]
	fn enqueue_event() {
		let state = create_state();
		state.enqueue_event(Event::from('a'));
		state.enqueue_event(Event::from('b'));

		assert_eq!(state.read_event(), Event::from('a'));
		assert_eq!(state.read_event(), Event::from('b'));
	}

	#[test]
	fn enqueue_event_resize_last_follow_by_non_resize() {
		let state = create_state();
		state.enqueue_event(Event::Resize(1, 1));
		state.enqueue_event(Event::from('a'));

		assert_eq!(state.read_event(), Event::Resize(1, 1));
		assert_eq!(state.read_event(), Event::from('a'));
	}

	#[test]
	fn enqueue_event_resize_last_follow_by_new_resize() {
		let state = create_state();
		state.enqueue_event(Event::Resize(1, 1));
		state.enqueue_event(Event::Resize(2, 2));

		assert_eq!(state.read_event(), Event::Resize(2, 2));
		assert_eq!(state.read_event(), Event::None);
	}

	#[test]
	fn enqueue_event_overflow() {
		let state = create_state();
		// fill queue
		for _ in 0..MAXIMUM_EVENTS {
			state.enqueue_event(Event::from('a'));
		}
		state.enqueue_event(Event::from('b'));

		let mut events_received = vec![];
		loop {
			let event = state.read_event();
			if event == Event::None {
				break;
			}
			events_received.push(event);
		}

		assert_eq!(state.read_event(), Event::None);
		assert_eq!(events_received.len(), MAXIMUM_EVENTS);
		assert_eq!(events_received.first().unwrap(), &Event::from('a'));
		assert_eq!(events_received.last().unwrap(), &Event::from('a'));
	}

	#[test]
	fn push_event() {
		let state = create_state();
		state.push_event(Event::from('a'));
		state.push_event(Event::from('b'));

		assert_eq!(state.read_event(), Event::from('b'));
		assert_eq!(state.read_event(), Event::from('a'));
	}

	#[test]
	fn push_event_overflow() {
		let state = create_state();
		// fill queue
		for _ in 0..MAXIMUM_EVENTS {
			state.push_event(Event::from('a'));
		}
		state.push_event(Event::from('b'));

		let mut events_received = vec![];
		loop {
			let event = state.read_event();
			if event == Event::None {
				break;
			}
			events_received.push(event);
		}

		assert_eq!(state.read_event(), Event::None);
		assert_eq!(events_received.len(), MAXIMUM_EVENTS);
		assert_eq!(events_received.first().unwrap(), &Event::from('b'));
		assert_eq!(events_received.last().unwrap(), &Event::from('a'));
	}

	#[test]
	fn read_event() {
		// STEPS:
		// 0 -> thread: initial event read with timeout, returns None
		//      test: waits for initial event read with timeout to occur (moves to step 1)
		// 1 -> thread: waits for step 1 to complete
		//      test: enqueues new event (moves to step 2)
		// 2 -> tread: reads enqueued event (moves to step 3)
		//      test: waits for step 2 to complete
		// 3 -> thead: ended, no action
		//      test: assert events read match

		let state = create_state();

		let step: Arc<Mutex<AtomicUsize>> = Arc::new(Mutex::new(AtomicUsize::new(0)));
		let events_read: Arc<Mutex<Vec<Event>>> = Arc::new(Mutex::new(vec![]));

		let thread_state = state.clone();
		let thread_step = Arc::clone(&step);
		let thread_events_read = Arc::clone(&events_read);
		_ = spawn(move || {
			loop {
				let mut thread_events_read_lock = thread_events_read.lock();
				let thread_step_lock = thread_step.lock();
				match thread_step_lock.load(Ordering::Acquire) {
					0 => {
						let event = thread_state.read_event();
						thread_events_read_lock.push(event);
						thread_step_lock.store(1, Ordering::Release);
					},
					1 => {
						sleep(Duration::from_millis(10));
					},
					2 => {
						let event = thread_state.