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
use std::ptr::NonNull;
use std::sync::atomic::Ordering;
use bytemuck::AnyBitPattern;
use crate::{
error::QueError, headless_spmc::MAGIC, page_size::PageSize,
shmem::Shmem,
};
use super::{burst_amount, Channel};
unsafe impl<T, const N: usize> Send for Consumer<T, N> {}
#[repr(C)]
pub struct Consumer<T, const N: usize> {
spsc: NonNull<Channel<T, N>>,
head: usize,
interval: usize,
consumer_index: usize,
last_producer_heartbeat: usize,
}
impl<T: AnyBitPattern, const N: usize> Consumer<T, N> {
const MODULO_MASK: usize = N - 1;
/// Joins an existing channel back by shared memory as a consumer.
pub unsafe fn join_shmem(
shmem_id: &str,
#[cfg(target_os = "linux")] page_size: PageSize,
) -> Result<Consumer<T, N>, QueError> {
Self::join_shmem_multi(
shmem_id,
#[cfg(target_os = "linux")]
page_size,
1,
)
}
/// Joins an existing channel back by shared memory as a consumer.
///
/// `interval` is the number of consumers. This channel is not FIFO!
/// To consume all produced values, you must consume all values
/// generated by all consumers generated via `next_multi`.
pub unsafe fn join_shmem_multi(
shmem_id: &str,
#[cfg(target_os = "linux")] page_size: PageSize,
interval: usize,
) -> Result<Consumer<T, N>, QueError> {
#[cfg(not(target_os = "linux"))]
let page_size = PageSize::Standard;
// Calculate buffer size.
// If using huge pages, we must uplign to page size.
let buffer_size: i64 = page_size
.mem_size(core::mem::size_of::<Channel<T, N>>())
.try_into()
.map_err(|_| QueError::InvalidSize)?;
// Open shmem
let shmem = Shmem::open_or_create(
shmem_id,
buffer_size,
#[cfg(target_os = "linux")]
page_size,
)?;
Consumer::join_multi(shmem.get_mut_ptr(), interval)
}
/// Joins an existing channel backed by `buffer`.
///
/// SAFETY:
/// This must point to a buffer of proper size and alignment.
pub unsafe fn join(
buffer: *mut u8,
) -> Result<Consumer<T, N>, QueError> {
Self::join_multi(buffer, 1)
}
/// Joins an existing channel backed by `buffer` as a consumer.
///
/// `interval` is the number of consumers. This channel is not FIFO!
/// To consume all produced values, you must consume all values
/// generated by all consumers generated via `next_multi`.
///
/// SAFETY:
/// This must point to a buffer of proper size and alignment.
pub unsafe fn join_multi(
buffer: *mut u8,
interval: usize,
) -> Result<Consumer<T, N>, QueError> {
assert!(
N > 0 && N.is_power_of_two(),
"Capacity must be a power of two"
);
assert!(buffer as usize % 128 == 0, "unaligned");
assert!(
interval <= 64,
"interval must be less than or equal to 64"
);
// Zerocopy deserialize the SPSC
let spsc: &Channel<T, N> = &*buffer.cast();
// Check magic
let magic = spsc.magic.load(Ordering::Acquire);
let capacity = spsc.capacity.load(Ordering::Acquire);
if magic == MAGIC {
// Check capacity
if capacity != N {
return Err(QueError::IncorrectCapacity(capacity));
}
// Initialize
let Channel {
tail,
head: _, // not used in headless mode
capacity: _,
producer_heartbeat: _,
consumer_heartbeat,
magic: _,
buffer: _unused,
padding: _,
} = spsc;
// Assume spsc is empty upon joining
let head = tail.load(Ordering::Acquire);
consumer_heartbeat.fetch_add(1, Ordering::Release);
// Successful join if magic and capacity is correct
Ok(Consumer {
spsc: NonNull::new_unchecked(buffer.cast()),
head: next_modulo(head, 0, interval),
interval,
consumer_index: 0,
last_producer_heartbeat: spsc
.producer_heartbeat
.load(Ordering::Acquire),
})
} else if magic == 0 {
// Technically could be corrupted but uninitialized
// is most likely explanation
Err(QueError::Uninitialized)
} else {
// Magic is not MAGIC and not zero
println!("magic = {}; expected {}", magic, MAGIC);
Err(QueError::CorruptionDetected)
}
}
/// Returns `None` if consumer_index would be equal to `interval`.
pub fn next_multi(&self) -> Option<Consumer<T, N>> {
if self.consumer_index + 1 == self.interval {
None
} else {
Some(Consumer {
consumer_index: self.consumer_index + 1,
head: self.head + 1,
..*self
})
}
}
/// Attempts to read the next element. Returns `None` if the
/// consuemr is caught up.
pub fn pop(&mut self) -> Option<T> {
loop {
let initial_tail = unsafe {
(*self.spsc.as_ptr())
.tail
.load(Ordering::Acquire)
};
// Check if there's anything to read
let previously_read_or_uninitialized =
initial_tail <= self.head;
if previously_read_or_uninitialized {
return None;
}
// Check for overrun
let not_overrun = initial_tail
<= (self
.head
.wrapping_add(N - burst_amount::<N>()));
if !not_overrun {
// Must reset to next integer that is consumer_index % interval
self.head = next_modulo(
initial_tail.wrapping_sub(N - burst_amount::<N>()),
self.consumer_index,
self.interval,
);
continue;
}
// Optimistically read value and then check if valid
let head_index = self.head & Self::MODULO_MASK;
let value = unsafe {
*(*self.spsc.as_ptr())
.buffer
.as_ptr()
.add(head_index)
};
// Check if still not overrun
let current_tail = unsafe {
(*self.spsc.as_ptr())
.tail
.load(Ordering::Acquire)
};
let still_not_overrun = current_tail
<= (self
.head
.wrapping_add(N - burst_amount::<N>()));
// If overrun, update head and try again
if !still_not_overrun {
// Must reset to next integer that is consumer_index %
// interval
self.head = next_modulo(
current_tail.wrapping_sub(N - burst_amount::<N>()),
self.consumer_index,
self.interval,
);
continue;
}
self.head += self.interval;
return Some(value);
}
}
/// Increments the consumer heartbeat.
///
/// Can be read by the producer to see that the consumer is still
/// online if done periodically. Can also be used to ack individual
/// messages or alert that we've joined.
pub fn beat(&self) {
unsafe {
(*self.spsc.as_ptr())
.consumer_heartbeat
.fetch_add(1, Ordering::Release);
}
}
/// Checks if the producer has incremented its heartbeat since last
/// called. Can be used by the consumer to see if the producer is
/// still online if done periodically. Can also be used to ack
/// individual messages or alert that we've joined.
pub fn producer_heartbeat(&mut self) -> bool {
let heartbeat = unsafe {
(*self.spsc.as_ptr())
.producer_heartbeat
.load(Ordering::Acquire)
};
if heartbeat != self.last_producer_heartbeat {
self.last_producer_heartbeat = heartbeat;
true
} else {
false
}
}
/// Returns pointer to inner padding.
///
/// User is responsible for safe usage.
///
/// Can be used to store metadata (e.g. hash seed).
///
/// Byte array is 128 byte aligned.
pub fn get_padding_ptr(&self) -> NonNull<[u8; 112]> {
unsafe {
NonNull::new_unchecked(
self.spsc.cast::<u8>().as_ptr().add(512),
)
.cast()
}
}
}
#[inline(always)]
fn next_modulo(
head: usize,
target_mod: usize,
mod_value: usize,
) -> usize {
let head_mod = head % mod_value;
let add_value = (target_mod + mod_value - head_mod) % mod_value;
head + add_value
}