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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
#[cfg(not(loom))]
#[cfg(test)]
mod test;
use crate::sync::{Ordering};
use crate::sync::{Mutex, Arc};
use crate::sync::{SpinMutex};
use std::ptr::{null_mut, null, NonNull};
use crate::event_reader::EventReader;
use std::ops::ControlFlow;
use std::ops::ControlFlow::{Continue, Break};
use std::marker::PhantomPinned;
use std::pin::Pin;
use crate::cursor::Cursor;
use crate::dynamic_chunk::{DynamicChunk};
#[cfg(feature = "double_buffering")]
use crate::dynamic_chunk::{DynamicChunkRecycled};
use crate::{StartPositionEpoch};
/// This way you can control when chunk's memory deallocation happens.
/// _In addition, some operations may cause deallocations as well._
#[derive(PartialEq)]
pub enum CleanupMode{
/// Cleanup will be called when chunk fully read.
///
/// In this mode memory will be freed ASAP - right in the end of reader consumption session.
///
/// !! Not allowed for spmc !!
OnChunkRead,
/// Cleanup will be called when new chunk created.
OnNewChunk,
/// Cleanup will never be called. You should call `EventQueue::cleanup` manually.
Never
}
pub trait Settings{
const MIN_CHUNK_SIZE : u32;
const MAX_CHUNK_SIZE : u32;
const CLEANUP : CleanupMode;
// for spmc/mpmc
/// Lock on new chunk cleanup event. Will dead-lock if already locked.
const LOCK_ON_NEW_CHUNK_CLEANUP: bool;
/// Call cleanup on unsubscribe?
const CLEANUP_IN_UNSUBSCRIBE: bool;
}
pub struct List<T, S: Settings>{
first: *mut DynamicChunk<T, S>,
last : *mut DynamicChunk<T, S>,
chunk_id_counter: usize,
total_capacity: usize,
readers_count: u32,
/// 0 - means no penult
penult_chunk_size: u32,
#[cfg(feature = "double_buffering")]
/// Biggest freed chunk
free_chunk: Option<DynamicChunkRecycled<T, S>>,
}
pub struct EventQueue<T, S: Settings>{
pub(crate) list : Mutex<List<T, S>>,
/// Separate lock from list::start_position_epoch, is safe, because start_point_epoch encoded in
/// chunk's atomic len+epoch.
// TODO: Make RWLock? Bench.
// TODO: Optioned
pub(crate) start_position: SpinMutex<Option<Cursor<T, S>>>,
_pinned: PhantomPinned,
}
//unsafe impl<T, S: Settings> Send for EventQueue<T, S>{}
//unsafe impl<T, S: Settings> Sync for EventQueue<T, S>{}
impl<T, S: Settings> EventQueue<T, S>
{
pub fn with_capacity(new_capacity: u32) -> Pin<Arc<Self>>{
assert!(S::MIN_CHUNK_SIZE <= new_capacity && new_capacity <= S::MAX_CHUNK_SIZE);
let this = Arc::new(Self{
list: Mutex::new(List{
first: null_mut(),
last: null_mut(),
chunk_id_counter: 0,
readers_count:0,
total_capacity:new_capacity as usize,
penult_chunk_size : 0,
#[cfg(feature = "double_buffering")]
free_chunk: None,
}),
start_position: SpinMutex::new(None),
_pinned: PhantomPinned,
});
let node = DynamicChunk::<T, S>::construct(
0, StartPositionEpoch::zero(), &*this, new_capacity as usize);
{
let mut list = this.list.lock();
list.first = node;
list.last = node;
}
unsafe{ Pin::new_unchecked(this) }
}
#[inline]
fn add_chunk_sized(&self, list: &mut List<T, S>, size: usize) -> &mut DynamicChunk<T, S>{
let node = unsafe{&mut *list.last};
let epoch = node.chunk_state(Ordering::Relaxed).epoch();
// make new node
list.chunk_id_counter += 1;
#[cfg(not(feature = "double_buffering"))]
let new_node = DynamicChunk::<T, S>::construct(list.chunk_id_counter, epoch, self, size);
#[cfg(feature = "double_buffering")]
let new_node = {
let mut new_node: *mut DynamicChunk<T, S> = null_mut();
if let Some(recycled_chunk) = &list.free_chunk {
// Check if recycled_chunk have exact capacity.
if recycled_chunk.capacity() == size {
// unwrap_unchecked()
new_node =
match list.free_chunk.take() {
Some(recycled_chunk) => {
unsafe { DynamicChunk::from_recycled(
recycled_chunk,
list.chunk_id_counter,
epoch) }
}, None => unsafe { std::hint::unreachable_unchecked() },
}
} else {
// TODO: try free in cleanup somehow
list.free_chunk = None;
}
}
if new_node.is_null(){
new_node = DynamicChunk::<T, S>::construct(list.chunk_id_counter, epoch, self, size);
}
new_node
};
// connect
node.set_next(new_node, Ordering::Release);
list.last = new_node;
list.penult_chunk_size = node.capacity() as u32;
list.total_capacity += size;
unsafe{&mut *new_node}
}
#[inline]
fn on_new_chunk_cleanup(&self, list: &mut List<T, S>){
if S::CLEANUP == CleanupMode::OnNewChunk{
// this should acts as compile-time-if.
if S::LOCK_ON_NEW_CHUNK_CLEANUP{
let _lock = self.list.lock();
self.cleanup_impl(list);
} else {
self.cleanup_impl(list);
}
}
}
#[inline]
fn add_chunk(&self, list: &mut List<T, S>) -> &mut DynamicChunk<T, S>{
let node = unsafe{&*list.last};
self.on_new_chunk_cleanup(list);
// Size pattern 4,4,8,8,16,16
let new_size: usize = {
if list.penult_chunk_size as usize == node.capacity(){
std::cmp::min(node.capacity() * 2, S::MAX_CHUNK_SIZE as usize)
} else {
node.capacity()
}
};
self.add_chunk_sized(list, new_size)
}
// Have 10% better performance. Observable in spmc.
#[inline]
pub fn push(&self, list: &mut List<T, S>, value: T){
let mut node = unsafe{&mut *list.last};
// Relaxed because we update only under lock
let chunk_state = node.chunk_state(Ordering::Relaxed);
let mut storage_len = chunk_state.len();
if /*unlikely*/ storage_len == node.capacity() as u32{
node = self.add_chunk(&mut *list);
storage_len = 0;
}
unsafe { node.push_at(value, storage_len, chunk_state, Ordering::Release); }
}
/*
#[inline]
pub fn push(&self, list: &mut List<T, S>, value: T){
let node = unsafe{&mut *list.last};
if let Err(err) = node.try_push(value, Ordering::Release){
unsafe {
self.add_chunk(&mut *list)
.push_unchecked(err.value, Ordering::Release);
}
}
}
*/
// Not an Extend trait, because Extend::extend(&mut self)
#[inline]
pub fn extend<I>(&self, list: &mut List<T, S>, iter: I)
where I: IntoIterator<Item = T>
{
let mut node = unsafe{&mut *list.last};
let mut iter = iter.into_iter();
while node.extend(&mut iter, Ordering::Release).is_err(){
match iter.next() {
None => {return;}
Some(value) => {
// add chunk and push value there
node = self.add_chunk(&mut *list);
unsafe{ node.push_unchecked(value, Ordering::Relaxed); }
}
};
}
}
/// EventReader will start receive events from NOW.
/// It will not see events that was pushed BEFORE subscription.
pub fn subscribe(&self, list: &mut List<T, S>) -> EventReader<T, S>{
if list.readers_count == 0{
// Keep alive. Decrements in unsubscribe
unsafe { Arc::increment_strong_count(self); }
}
list.readers_count += 1;
let last_chunk = unsafe{&*list.last};
let chunk_state = last_chunk.chunk_state(Ordering::Relaxed);
// Enter chunk
last_chunk.readers_entered().fetch_add(1, Ordering::AcqRel);
EventReader{
position: Cursor{chunk: last_chunk, index: chunk_state.len() as usize},
start_position_epoch: chunk_state.epoch()
}
}
// Called from EventReader Drop
//
// `this_ptr` instead of `&self`, because `&self` as reference should be valid during
// function call. And we drop it sometimes.... through `Arc::decrement_strong_count`.
pub(crate) fn unsubscribe(this_ptr: NonNull<Self>, event_reader: &EventReader<T, S>){
let this = unsafe { this_ptr.as_ref() };
let mut list = this.list.lock();
// Exit chunk
unsafe{&*event_reader.position.chunk}.read_completely_times().fetch_add(1, Ordering::AcqRel);
if S::CLEANUP_IN_UNSUBSCRIBE && S::CLEANUP != CleanupMode::Never{
if list.first as *const _ == event_reader.position.chunk {
this.cleanup_impl(&mut *list);
}
}
list.readers_count -= 1;
if list.readers_count == 0{
drop(list);
// Safe to self-destruct
unsafe { Arc::decrement_strong_count(this_ptr.as_ptr()); }
}
}
unsafe fn free_chunk<const LOCK_ON_WRITE_START_POSITION: bool>(
&self,
chunk: *mut DynamicChunk<T, S>,
list: &mut List<T, S>)
{
if let Some(start_position) = *self.start_position.as_mut_ptr(){
if start_position.chunk == chunk{
if LOCK_ON_WRITE_START_POSITION{
*self.start_position.lock() = None;
} else {
*self.start_position.as_mut_ptr() = None;
}
}
}
list.total_capacity -= (*chunk).capacity();
#[cfg(not(feature = "double_buffering"))]
{
DynamicChunk::destruct(chunk);
std::mem::drop(list); // just for use
}
#[cfg(feature = "double_buffering")]
{
if let Some(free_chunk) = &list.free_chunk {
if free_chunk.capacity() >= (*chunk).capacity() {
// Discard - recycled chunk bigger then our
DynamicChunk::destruct(chunk);
return;
}
}
// Replace free_chunk with our.
list.free_chunk = Some(DynamicChunk::recycle(chunk));
}
}
fn cleanup_impl(&self, list: &mut List<T, S>){
unsafe {
// using _ptr version, because with &chunk - reference should be valid during whole
// lambda function call. (according to miri and some rust borrowing rules).
// And we actually drop that chunk.
foreach_chunk_ptr_mut(
list.first,
list.last,
Ordering::Relaxed, // we're under mutex
|chunk_ptr| {
// Do not lock prev_chunk.chunk_switch_mutex because we traverse in order.
let chunk = &mut *chunk_ptr;
let chunk_readers = chunk.readers_entered().load(Ordering::Acquire);
let chunk_read_times = chunk.read_completely_times().load(Ordering::Acquire);
// Cleanup only in order
if chunk_readers != chunk_read_times {
return Break(());
}
let next_chunk_ptr = chunk.next(Ordering::Relaxed);
debug_assert!(!next_chunk_ptr.is_null());
debug_assert!(std::ptr::eq(chunk, list.first));
// Do not lock start_position permanently, because reader will
// never enter chunk before list.first
self.free_chunk::<true>(chunk, list);
list.first = next_chunk_ptr;
Continue(())
}
);
}
if list.first == list.last{
list.penult_chunk_size = 0;
}
}
/// This will traverse up to the start_point - and will free all unoccupied chunks. (out-of-order cleanup)
/// This one slower then cleanup_impl.
fn force_cleanup_impl(&self, list: &mut List<T, S>){
self.cleanup_impl(list);
// Lock start_position permanently, due to out of order chunk destruction.
// Reader can try enter in the chunk in the middle of force_cleanup execution.
let start_position = self.start_position.lock();
let terminal_chunk = match &*start_position{
None => { return; }
Some(cursor) => {cursor.chunk}
};
if list.first as *const _ == terminal_chunk{
return;
}
unsafe {
// cleanup_impl dealt with first chunk before. Omit.
let mut prev_chunk = list.first;
// using _ptr version, because with &chunk - reference should be valid during whole
// lambda function call. (according to miri and some rust borrowing rules).
// And we actually drop that chunk.
foreach_chunk_ptr_mut(
(*list.first).next(Ordering::Relaxed),
terminal_chunk,
Ordering::Relaxed, // we're under mutex
|chunk| {
// We need to lock only `prev_chunk`, because it is impossible
// to get in `chunk` omitting chunk.readers_entered+1
let lock = (*prev_chunk).chunk_switch_mutex().write();
let chunk_readers = (*chunk).readers_entered().load(Ordering::Acquire);
let chunk_read_times = (*chunk).read_completely_times().load(Ordering::Acquire);
if chunk_readers != chunk_read_times {
prev_chunk = chunk;
return Continue(());
}
let next_chunk_ptr = (*chunk).next(Ordering::Relaxed);
debug_assert!(!next_chunk_ptr.is_null());
(*prev_chunk).set_next(next_chunk_ptr, Ordering::Release);
drop(lock);
self.free_chunk::<false>(chunk, list);
Continue(())
}
);
}
}
pub fn cleanup(&self){
self.cleanup_impl(&mut *self.list.lock());
}
#[inline]
fn set_start_position(
&self,
list: &mut List<T, S>,
new_start_position: Cursor<T, S>)
{
*self.start_position.lock() = Some(new_start_position);
// update len_and_start_position_epoch in each chunk
let first_chunk = unsafe{&mut *list.first};
let new_epoch = first_chunk.chunk_state(Ordering::Relaxed).epoch().increment();
unsafe {
foreach_chunk_mut(
first_chunk,
null(),
Ordering::Relaxed, // we're under mutex
|chunk| {
chunk.set_epoch(new_epoch, Ordering::Relaxed, Ordering::Release);
Continue(())
}
);
}
}
pub fn clear(&self, list: &mut List<T, S>){
let last_chunk = unsafe{ &*list.last };
let last_chunk_len = last_chunk.chunk_state(Ordering::Relaxed).len() as usize;
self.set_start_position(list, Cursor {
chunk: last_chunk,
index: last_chunk_len
});
self.force_cleanup_impl(list);
}
pub fn truncate_front(&self, list: &mut List<T, S>, len: usize) {
// make chunks* array
// TODO: subtract from total_capacity
// TODO: use small_vec
// TODO: loop if > 128?
// there is no way we can have memory enough to hold > 2^64 bytes.
let mut chunks : [*const DynamicChunk<T, S>; 128] = [null(); 128];
let chunks_count=
unsafe {
let mut i = 0;
foreach_chunk(
list.first,
null(),
Ordering::Relaxed, // we're under mutex
|chunk| {
chunks[i] = chunk;
i+=1;
Continue(())
}
);
i
};
let mut total_len = 0;
for i in (0..chunks_count).rev(){
let chunk = unsafe{ &*chunks[i] };
let chunk_len = chunk.chunk_state(Ordering::Relaxed).len() as usize;
total_len += chunk_len;
if total_len >= len{
let new_start_position = Cursor {
chunk: chunks[i],
index: total_len - len
};
// Do we actually need to truncate?
if let Some(start_position) = unsafe{*self.start_position.as_mut_ptr()}{
if start_position >= new_start_position{
return;
}
}
self.set_start_position(list, new_start_position);
self.force_cleanup_impl(list);
return;
}
}
// len is bigger then total_len.
// do nothing.
}
pub fn change_chunk_capacity(&self, list: &mut List<T, S>, new_capacity: u32){
assert!(S::MIN_CHUNK_SIZE <= new_capacity && new_capacity <= S::MAX_CHUNK_SIZE);
self.on_new_chunk_cleanup(list);
self.add_chunk_sized(&mut *list, new_capacity as usize);
}
pub fn total_capacity(&self, list: &List<T, S>) -> usize {
list.total_capacity
}
pub fn chunk_capacity(&self, list: &List<T, S>) -> usize {
unsafe { (*list.last).capacity() }
}
/*
// chunks_count can be atomic. But does that needed?
pub fn chunks_count(&self) -> usize {
let list = self.list.lock();
unsafe{
list.chunk_id_counter/*(*list.last).id*/ - (*list.first).id() + 1
}
}*/
}
impl<T, S: Settings> Drop for EventQueue<T, S>{
fn drop(&mut self) {
let list = self.list.get_mut();
debug_assert!(list.readers_count == 0);
unsafe{
let mut node_ptr = list.first;
while node_ptr != null_mut() {
let node = &mut *node_ptr;
node_ptr = node.next(Ordering::Relaxed);
DynamicChunk::destruct(node);
}
}
}
}
#[inline(always)]
pub(super) unsafe fn foreach_chunk<T, F, S: Settings>
(
start_chunk_ptr : *const DynamicChunk<T, S>,
end_chunk_ptr : *const DynamicChunk<T, S>,
load_ordering : Ordering,
mut func : F
)
where F: FnMut(&DynamicChunk<T, S>) -> ControlFlow<()>
{
foreach_chunk_mut(
start_chunk_ptr as *mut _,
end_chunk_ptr,
load_ordering,
|mut_chunk| func(mut_chunk)
);
}
/// end_chunk_ptr may be null
#[inline(always)]
pub(super) unsafe fn foreach_chunk_mut<T, F, S: Settings>
(
start_chunk_ptr : *mut DynamicChunk<T, S>,
end_chunk_ptr : *const DynamicChunk<T, S>,
load_ordering : Ordering,
mut func : F
)
where F: FnMut(&mut DynamicChunk<T, S>) -> ControlFlow<()>
{
foreach_chunk_ptr_mut(
start_chunk_ptr,
end_chunk_ptr,
load_ordering,
|mut_chunk_ptr| func(&mut *mut_chunk_ptr)
);
}
/// end_chunk_ptr may be null
#[inline(always)]
pub(super) unsafe fn foreach_chunk_ptr_mut<T, F, S: Settings>
(
start_chunk_ptr : *mut DynamicChunk<T, S>,
end_chunk_ptr : *const DynamicChunk<T, S>,
load_ordering : Ordering,
mut func : F
)
where F: FnMut(*mut DynamicChunk<T, S>) -> ControlFlow<()>
{
debug_assert!(!start_chunk_ptr.is_null());
debug_assert!(
end_chunk_ptr.is_null()
||
std::ptr::eq((*start_chunk_ptr).event(), (*end_chunk_ptr).event())
);
debug_assert!(
end_chunk_ptr.is_null()
||
(*start_chunk_ptr).id() <= (*end_chunk_ptr).id()
);
let mut chunk_ptr = start_chunk_ptr;
while !chunk_ptr.is_null(){
if chunk_ptr as *const _ == end_chunk_ptr {
break;
}
// chunk can be dropped inside `func`, so fetch `next` beforehand
let next_chunk_ptr = (*chunk_ptr).next(load_ordering);
let proceed = func(chunk_ptr);
if proceed == Break(()) {
break;
}
chunk_ptr = next_chunk_ptr;
}
}