1
1
//! A contiguous push-only array type with lock-free reads.
2
2
3
- use crate :: {
4
- collect:: { self , Pin } ,
5
- scopeguard:: guard,
6
- } ;
7
3
use core:: ptr:: NonNull ;
4
+ use std:: alloc:: { Allocator , Global , Layout , LayoutError , handle_alloc_error} ;
5
+ use std:: cell:: UnsafeCell ;
6
+ use std:: intrinsics:: unlikely;
7
+ use std:: iter:: FromIterator ;
8
+ use std:: marker:: PhantomData ;
9
+ use std:: ops:: { Deref , DerefMut } ;
10
+ use std:: ptr:: slice_from_raw_parts;
11
+ use std:: sync:: Arc ;
12
+ use std:: sync:: atomic:: { AtomicPtr , AtomicUsize , Ordering } ;
13
+ use std:: { cmp, mem} ;
14
+
8
15
use parking_lot:: { Mutex , MutexGuard } ;
9
- use std:: {
10
- alloc:: { Allocator , Global , Layout , LayoutError , handle_alloc_error} ,
11
- cell:: UnsafeCell ,
12
- intrinsics:: unlikely,
13
- iter:: FromIterator ,
14
- marker:: PhantomData ,
15
- mem,
16
- ops:: { Deref , DerefMut } ,
17
- sync:: atomic:: { AtomicPtr , Ordering } ,
18
- } ;
19
- use std:: {
20
- cmp,
21
- ptr:: slice_from_raw_parts,
22
- sync:: { Arc , atomic:: AtomicUsize } ,
23
- } ;
16
+
17
+ use super :: collect:: { self , Pin } ;
18
+ use super :: scopeguard:: guard;
24
19
25
20
mod code;
26
21
mod tests;
@@ -94,10 +89,7 @@ impl<T> Copy for TableRef<T> {}
94
89
impl < T > Clone for TableRef < T > {
95
90
#[ inline]
96
91
fn clone ( & self ) -> Self {
97
- Self {
98
- data : self . data ,
99
- marker : self . marker ,
100
- }
92
+ Self { data : self . data , marker : self . marker }
101
93
}
102
94
}
103
95
@@ -115,12 +107,8 @@ impl<T> TableRef<T> {
115
107
info : TableInfo ,
116
108
}
117
109
118
- static EMPTY : EmptyTable = EmptyTable {
119
- info : TableInfo {
120
- capacity : 0 ,
121
- items : AtomicUsize :: new ( 0 ) ,
122
- } ,
123
- } ;
110
+ static EMPTY : EmptyTable =
111
+ EmptyTable { info : TableInfo { capacity : 0 , items : AtomicUsize :: new ( 0 ) } } ;
124
112
125
113
Self {
126
114
data : unsafe {
@@ -149,16 +137,10 @@ impl<T> TableRef<T> {
149
137
let info =
150
138
unsafe { NonNull :: new_unchecked ( ptr. as_ptr ( ) . add ( info_offset) as * mut TableInfo ) } ;
151
139
152
- let mut result = Self {
153
- data : info,
154
- marker : PhantomData ,
155
- } ;
140
+ let mut result = Self { data : info, marker : PhantomData } ;
156
141
157
142
unsafe {
158
- * result. info_mut ( ) = TableInfo {
159
- capacity,
160
- items : AtomicUsize :: new ( 0 ) ,
161
- } ;
143
+ * result. info_mut ( ) = TableInfo { capacity, items : AtomicUsize :: new ( 0 ) } ;
162
144
}
163
145
164
146
result
@@ -342,13 +324,9 @@ impl<T> SyncPushVec<T> {
342
324
pub fn with_capacity ( capacity : usize ) -> Self {
343
325
Self {
344
326
current : AtomicPtr :: new (
345
- if capacity > 0 {
346
- TableRef :: < T > :: allocate ( capacity)
347
- } else {
348
- TableRef :: empty ( )
349
- }
350
- . data
351
- . as_ptr ( ) ,
327
+ if capacity > 0 { TableRef :: < T > :: allocate ( capacity) } else { TableRef :: empty ( ) }
328
+ . data
329
+ . as_ptr ( ) ,
352
330
) ,
353
331
old : UnsafeCell :: new ( Vec :: new ( ) ) ,
354
332
marker : PhantomData ,
@@ -389,25 +367,16 @@ impl<T> SyncPushVec<T> {
389
367
/// Creates a [LockedWrite] handle by taking the underlying mutex that protects writes.
390
368
#[ inline]
391
369
pub fn lock ( & self ) -> LockedWrite < ' _ , T > {
392
- LockedWrite {
393
- table : Write { table : self } ,
394
- _guard : self . lock . lock ( ) ,
395
- }
370
+ LockedWrite { table : Write { table : self } , _guard : self . lock . lock ( ) }
396
371
}
397
372
398
373
/// Creates a [LockedWrite] handle from a guard protecting the underlying mutex that protects writes.
399
374
#[ inline]
400
375
pub fn lock_from_guard < ' a > ( & ' a self , guard : MutexGuard < ' a , ( ) > ) -> LockedWrite < ' a , T > {
401
376
// Verify that we are target of the guard
402
- assert_eq ! (
403
- & self . lock as * const _,
404
- MutexGuard :: mutex( & guard) as * const _
405
- ) ;
406
-
407
- LockedWrite {
408
- table : Write { table : self } ,
409
- _guard : guard,
410
- }
377
+ assert_eq ! ( & self . lock as * const _, MutexGuard :: mutex( & guard) as * const _) ;
378
+
379
+ LockedWrite { table : Write { table : self } , _guard : guard }
411
380
}
412
381
413
382
/// Extracts a mutable slice of the entire vector.
@@ -545,14 +514,9 @@ impl<T: Send> Write<'_, T> {
545
514
fn replace_table ( & mut self , new_table : TableRef < T > ) {
546
515
let table = self . table . current ( ) ;
547
516
548
- self . table
549
- . current
550
- . store ( new_table. data . as_ptr ( ) , Ordering :: Release ) ;
517
+ self . table . current . store ( new_table. data . as_ptr ( ) , Ordering :: Release ) ;
551
518
552
- let destroy = Arc :: new ( DestroyTable {
553
- table,
554
- lock : Mutex :: new ( false ) ,
555
- } ) ;
519
+ let destroy = Arc :: new ( DestroyTable { table, lock : Mutex :: new ( false ) } ) ;
556
520
557
521
unsafe {
558
522
( * self . table . old . get ( ) ) . push ( destroy. clone ( ) ) ;
0 commit comments