cupchan 0.1.2

Simple async overwriting channel between two threads that is wait & block free by swapping cups around
Documentation
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
//! Cup Channel Library
//!
//! *Yes cup-chan please swap my cups around uwu*
//!
//! Simple usage example:
//! ```rust
//! use cupchan::cupchan;
//!
//! let (mut writer, reader) = cupchan(0);
//! *writer = 1;
//! writer.flush();
//! assert_eq!(*reader, 1);
//!
//! *writer = 2;
//! writer.flush();
//! assert_eq!(*reader, 2);
//!
//! drop(reader);
//!
//! let reader = writer.new_reader().unwrap(); // Create a new reader
//!
//! *writer = 3;
//! writer.flush();
//! assert_eq!(*reader, 3);
//! ```
#![feature(test)]

use std::{fmt, ptr};

#[cfg(loom)]
pub(crate) use loom::{
	cell::{ConstPtr, MutPtr, UnsafeCell},
	sync::atomic::{AtomicBool, AtomicUsize, Ordering},
};

#[cfg(not(loom))]
pub(crate) use std::{
	cell::UnsafeCell,
	ops::{Deref, DerefMut},
	sync::atomic::{AtomicBool, AtomicUsize, Ordering},
};

const OBJECT_PERMUTATIONS: &[usize; 6] = &[
	0b000, // <W><S><R>
	0b001, // <W><R><S>
	0b010, // <S><R><W>
	0b011, // <S><W><R>
	0b100, // <R><S><W>
	0b101, // <R><W><S>
];
/// Writer Permutations that map from Permutation to Permutation
/* const WRITER_SWAP_PERMUTATIONS: &[u8; 6] = &[
	0b011, // <S><W><R>
	0b010, // <S><R><W>
	0b001, // <W><R><S>
	0b000, // <W><S><R>
	0b101, // <R><W><S>
	0b100, // <R><S><W>
]; */
/// Writer State Map, XOR with state to represent write
const WRITER_STATE_MAP: &[usize; 16] = &[
	0b1011,     // 0b000 -> 0b011
	0b1011,     // 0b001 -> 0b010
	0b1011,     // 0b010 -> 0b001
	0b1011,     // 0b011 -> 0b000
	0b1001,     // 0b100 -> 0b101
	0b1001,     // 0b101 -> 0b100
	0b11111111, // (Invalid State)
	0b11111111, // (Invalid State)
	0b0011,     // 0b000 -> 0b011
	0b0011,     // 0b001 -> 0b010
	0b0011,     // 0b010 -> 0b001
	0b0011,     // 0b011 -> 0b000
	0b0001,     // 0b100 -> 0b101
	0b0001,     // 0b101 -> 0b100
	0b11111111, // (Invalid State)
	0b11111111, // (Invalid State)
];
const WRITER_CUP_MAP: &[usize; 16] = &[0, 0, 2, 1, 2, 1, 3, 3, 0, 0, 2, 1, 2, 1, 3, 3];

/* const READER_SWAP_PERMUTATIONS: &[u8; 6] = &[
	0b001, // <W><R><S>
	0b000, // <W><S><R>
	0b100, // <R><S><W>
	0b101, // <R><W><S>
	0b010, // <S><R><W>
	0b011, // <S><W><R>
]; */
/// Reader State Map, XOR with state to represent read
const READER_STATE_MAP: &[usize; 16] = &[
	0b0000,     // (Preserve State)
	0b0000,     // (Preserve State)
	0b0000,     // (Preserve State)
	0b0000,     // (Preserve State)
	0b0000,     // (Preserve State)
	0b0000,     // (Preserve State)
	0b11111111, // (Invalid State)
	0b11111111, // (Invalid State)
	0b1001,     // 0b000 -> 0b001
	0b1001,     // 0b001 -> 0b000
	0b1110,     // 0b010 -> 0b100
	0b1110,     // 0b011 -> 0b101
	0b1110,     // 0b100 -> 0b010
	0b1110,     // 0b101 -> 0b011
	0b11111111, // (Invalid State)
	0b11111111, // (Invalid State)
];
const READER_CUP_MAP: &[usize; 16] = &[2, 1, 1, 2, 0, 0, 3, 3, 2, 1, 1, 2, 0, 0, 3, 3];

/// A simple async channel used to quickly update data between threads
/// Useful in a situation where you need to model some read-only state on a receiving thread that can be periodically, but quickly, updated from a writer thread.
struct Cupchan<T> {
	// One of these cups is reading, one writing, one for intermediate storage, which one is which depends on the permutation state
	cups: [UnsafeCell<T>; 3], // Use boxes to avoid False Sharing between cpu cache lines https://en.wikipedia.org/wiki/False_sharing
	/// Represents the permutation of cups i.e. which one is the reader, writer, and storage as well as whether or not storage is ready to be read from.
	state: AtomicUsize,
	/// True if reader or writer is dropped
	unconnected: AtomicBool,
}
/// Create a new Cup Channel
pub fn cupchan<T: Clone>(initial: T) -> (CupchanWriter<T>, CupchanReader<T>) {
	let chan = Cupchan {
		cups: [
			UnsafeCell::new(initial.clone()),
			UnsafeCell::new(initial.clone()),
			UnsafeCell::new(initial),
		],
		state: AtomicUsize::new(OBJECT_PERMUTATIONS[0]), // Initial state: <W><S><R> permutation with UPDATE_FLAG unset
		unconnected: AtomicBool::new(false),
	};
	let chan = Box::leak(Box::new(chan)); // Use special dropping logic based on self.unconnected
	(
		CupchanWriter {
			chan,
			current_cup: &chan.cups[0],
		},
		CupchanReader { chan },
	)
}
impl<T: fmt::Debug> fmt::Debug for Cupchan<T> {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		f.debug_struct("Cupchan")
			.field("cups", &self.cups)
			.field("state", &self.state.load(Ordering::SeqCst))
			.field("unconnected", &self.unconnected.load(Ordering::SeqCst))
			.finish()
	}
}

/// Write to the Cup Channel, make sure to call flush() afterwards.
#[derive(Debug)]
pub struct CupchanWriter<T: 'static> {
	chan: &'static Cupchan<T>,
	current_cup: &'static UnsafeCell<T>,
}
impl<T> CupchanWriter<T> {
	fn new(chan: &'static Cupchan<T>) -> Self {
		let cup_index = WRITER_CUP_MAP[chan.state.load(Ordering::Acquire)];
		Self {
			chan,
			current_cup: &chan.cups[cup_index],
		}
	}
	pub fn flush(&mut self) {
		// Needs exclusive reference
		// Update storage flag & swap cups
		let res = self
			.chan
			.state
			.fetch_update(Ordering::AcqRel, Ordering::Acquire, |state| {
				Some(state ^ WRITER_STATE_MAP[state])
			})
			.unwrap();
		self.current_cup = &self.chan.cups[WRITER_CUP_MAP[res ^ WRITER_STATE_MAP[res]]];
	}
	pub fn new_reader(&self) -> Option<CupchanReader<T>> {
		// Set unconnected false, If was actually unconnected, return new reader
		if self.chan.unconnected.swap(false, Ordering::SeqCst) {
			Some(CupchanReader::new(self.chan))
		} else {
			None
		}
	}

	#[cfg(loom)]
	pub fn loom_ptr(&mut self) -> MutPtr<T> {
		self.current_cup.get_mut()
	}
}
#[cfg(not(loom))]
impl<T> Deref for CupchanWriter<T> {
	type Target = T;
	fn deref(&self) -> &Self::Target {
		unsafe { &*self.current_cup.get() }
	}
}
#[cfg(not(loom))]
impl<T> DerefMut for CupchanWriter<T> {
	fn deref_mut(&mut self) -> &mut Self::Target {
		unsafe { &mut *self.current_cup.get() }
	}
}
impl<T> Drop for CupchanWriter<T> {
	fn drop(&mut self) {
		// Set unconnected to true
		let was_unconnected = self.chan.unconnected.swap(true, Ordering::AcqRel);
		if was_unconnected {
			// If was unconnected, drop channel
			unsafe {
				let ptr = std::mem::transmute::<_, *mut Cupchan<T>>(self.chan);
				ptr::drop_in_place(ptr);
			}
		}
	}
}
// Allow sending between threads
unsafe impl<T: Sync + Send> Send for CupchanWriter<T> {}
unsafe impl<T: Sync + Send> Sync for CupchanWriter<T> {}

// when created, modify state to set reader lock flag
// when dropped, modify state permutation to swap reader & storage object, unset reader lock flag, unset storage new flag
/// Read from the Cup Channel by dereferencing this obejct

#[derive(Debug)]
pub struct CupchanReader<T: 'static> {
	chan: &'static Cupchan<T>,
}
impl<T> CupchanReader<T> {
	fn new(chan: &'static Cupchan<T>) -> Self {
		Self { chan }
	}
	#[inline]
	fn read(&self) -> &'static UnsafeCell<T> {
		let res = self
			.chan
			.state
			.fetch_update(Ordering::AcqRel, Ordering::Acquire, |state| {
				Some(state ^ READER_STATE_MAP[state])
			})
			.unwrap();
		&self.chan.cups[READER_CUP_MAP[res ^ READER_STATE_MAP[res]]]
	}
	pub fn new_writer(&self) -> Option<CupchanWriter<T>> {
		// Set unconnected false, If was actually unconnected, return new reader
		if self.chan.unconnected.swap(false, Ordering::SeqCst) {
			Some(CupchanWriter::new(self.chan))
		} else {
			None
		}
	}
	#[cfg(loom)]
	pub fn loom_ptr(&self) -> ConstPtr<T> {
		self.read().get()
	}
}
#[cfg(not(loom))]
impl<T> Deref for CupchanReader<T> {
	type Target = T;
	fn deref(&self) -> &Self::Target {
		unsafe { &(*self.read().get()) }
	}
}
impl<T> Drop for CupchanReader<T> {
	fn drop(&mut self) {
		// Set unconnected to true
		let was_unconnected = self.chan.unconnected.swap(true, Ordering::AcqRel);
		if was_unconnected {
			// If was unconnected, drop channel
			unsafe {
				let ptr = std::mem::transmute::<_, *mut Cupchan<T>>(self.chan);
				ptr::drop_in_place(ptr);
			}
		}
	}
}
// Allow sending between threads
unsafe impl<T: Sync + Send> Send for CupchanReader<T> {}
unsafe impl<T: Sync + Send> Sync for CupchanReader<T> {}

#[cfg(test)]
mod tests {
	extern crate test;
	use test::Bencher;

	use std::thread;

	use crate::cupchan;

	#[test]
	fn test_chan_sync() {
		let (mut writer, reader) = cupchan(0);
		*writer = 1;
		writer.flush();
		assert_eq!(*reader, 1);

		*writer = 2;
		writer.flush();
		assert_eq!(*reader, 2);

		drop(reader);

		let reader = writer.new_reader().unwrap();

		*writer = 3;
		writer.flush();
		assert_eq!(*reader, 3);
		drop(writer)
	}

	const MAX: usize = 5_000;
	#[test]
	fn cupchan_async_greedy_reader() {
		let (mut writer, reader) = cupchan(0usize);

		let join = thread::spawn(move || {
			for i in 0..MAX {
				*writer = i;
				writer.flush();
			}
		});

		let mut current = *reader;
		// let mut array = [0usize; MAX];
		while current < MAX - 1 {
			current = *reader;
		}
		// print!("{:?}", array);
		assert!(*reader == MAX - 1);

		join.join().unwrap();
	}
	#[test]
	fn cupchan_async_lazy_reader() {
		let (mut writer, reader) = cupchan(0usize);

		let join = thread::spawn(move || {
			for i in 0..MAX {
				*writer = i;
				writer.flush();
			}
		});

		let mut current = *reader;
		// let mut array = [0usize; MAX];
		while current < MAX - 1 {
			thread::yield_now();
			current = *reader;
		}
		// print!("{:?}", array);
		assert!(*reader == MAX - 1);

		join.join().unwrap();
	}

	#[test]
	fn crossbeam_chan_async() {
		let (tx, rx) = crossbeam_channel::bounded(3);

		let join = thread::spawn(move || {
			for i in 0..MAX {
				tx.send(i).unwrap();
			}
		});

		let mut current = 0;
		for _ in 0..MAX {
			current = rx.recv().unwrap();
		}
		assert!(current == MAX - 1);

		join.join().unwrap();
	}

	#[test]
	fn crossbeam_chan_async_cap_10() {
		let (tx, rx) = crossbeam_channel::bounded(10);

		let join = thread::spawn(move || {
			for i in 0..MAX {
				tx.send(i).unwrap();
			}
		});

		let mut current = 0;
		for _ in 0..MAX {
			current = rx.recv().unwrap();
		}
		assert!(current == MAX - 1);

		join.join().unwrap();
	}

	#[test]
	fn flume_chan_async() {
		let (tx, rx) = flume::unbounded();

		let join = thread::spawn(move || {
			for i in 0..MAX {
				tx.send(i).unwrap();
			}
		});

		let mut current = 0;
		for _ in 0..MAX {
			current = rx.recv().unwrap();
		}
		assert!(current == MAX - 1);

		join.join().unwrap();
	}

	#[bench]
	fn bench_cupchan_greedy(b: &mut Bencher) {
		b.iter(|| {
			cupchan_async_greedy_reader();
		})
	}
	#[bench]
	fn bench_cupchan_lazy(b: &mut Bencher) {
		b.iter(|| {
			cupchan_async_lazy_reader();
		})
	}

	#[bench]
	fn bench_crossbeam_chan_cap_3(b: &mut Bencher) {
		b.iter(|| {
			crossbeam_chan_async();
		})
	}

	#[bench]
	fn bench_crossbeam_chan_cap_10(b: &mut Bencher) {
		b.iter(|| {
			crossbeam_chan_async_cap_10();
		})
	}

	#[bench]
	fn bench_flume_chan(b: &mut Bencher) {
		b.iter(|| {
			flume_chan_async();
		})
	}
}