read-write-store 0.2.0

A concurrent, unordered collection for Rust, where each element has an internally generated ID and a read-write lock.
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
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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
#[cfg(debug_assertions)]
use std::thread;
use std::time::Instant;

use crate::timeout::{BlockResult, TimedOut};
use crate::util::sync::atomic::{AtomicU64, Ordering};
use crate::util::sync::park::{Park, ParkChoice, ParkResult};
use crate::Timeout;

pub const RESERVED_ID: u32 = u32::MAX;

/// The maximum number of read locks which can be held concurrently.
///
/// Acquiring more than this number of read locks simultaneously will panic.
pub const MAX_CONCURRENT_READS: u32 = (1 << 31) - 2;

/// An unsafe read-write lock with ID matching. As well as the lock state, a header stores an ID for
/// the data it is guarding. Many operations on the header take an ID which is compared to the
/// stored ID to determine whether to proceed with the operation.
pub struct Header {
    // The layout of the header's state is as follows:
    // * The most significant bit is the thread notification bit. This is set when no threads are
    //   blocking on the header and is unset when there are threads blocking. The behavior of this
    //   bit is the opposite what you might expect because the most significant 32 bits being set
    //   represents a special state.
    // * The next 31 most significant bits store the number of readers or is all ones if the header
    //   is write locked. If the header is unlocked, these bits will be zero.
    // * There are never any threads blocking on an unlocked header, so it is invalid for the thread
    //   notification bit to be unset (indicating one or more waiting threads) and for the reader
    //   bits to be all unset. This bit pattern instead represents the header being in an unoccupied
    //   state.
    // * The lower 32 bits store the ID if the header is occupied, or store the next ID if the
    //   header is unoccupied.
    state: Park<AtomicU64>,
}

impl Header {
    pub fn new() -> Self {
        let state = Self::unoccupied_bits(0);

        debug_assert!(state == 0, "initial state was not zeroed");

        Self {
            state: Park::new(AtomicU64::new(state)),
        }
    }

    /// Locks the header for reading if it's ID matches and it is not write locked. If the IDs match
    /// the header must be occupied.
    pub unsafe fn lock_read(&self, id: u32, timeout: Timeout) -> BlockResult<bool> {
        debug_assert!(id != RESERVED_ID, "attempted to read lock the reserved ID");

        let current = self.state.load(Ordering::Relaxed);
        self.lock_read_with_current(id, current, timeout)
    }

    unsafe fn lock_read_with_current(
        &self,
        id: u32,
        current: u64,
        timeout: Timeout,
    ) -> BlockResult<bool> {
        if Self::id_from_bits(current) != id {
            return Ok(false);
        }

        if Self::is_write_locked(current) {
            return self.lock_read_slow(id, timeout);
        }

        match self.compare_exchange_weak(current, Self::increment_readers(current)) {
            Ok(_) => Ok(true),
            Err(actual) => self.lock_read_with_current(id, actual, timeout),
        }
    }

    #[inline(never)]
    unsafe fn lock_read_slow(&self, id: u32, timeout: Timeout) -> BlockResult<bool> {
        enum Response {
            Matched(u64),
            Mismatch,
        }

        let timeout_optional = match timeout {
            Timeout::DontBlock => return BlockResult::Err(TimedOut),
            Timeout::BlockIndefinitely => None,
            Timeout::BlockUntil(deadline) => Some(deadline),
        };

        let result = self.block(timeout_optional, || {
            let current = self.state.load(Ordering::Relaxed);

            if Self::id_from_bits(current) != id {
                return BlockChoice::DontBlock(Response::Mismatch);
            }

            if Self::is_write_locked(current) {
                return BlockChoice::Block(current);
            }

            BlockChoice::DontBlock(Response::Matched(current))
        });

        match result {
            Ok(Response::Matched(current)) => {
                match self.compare_exchange_weak(current, Self::increment_readers(current)) {
                    Ok(_) => Ok(true),
                    Err(actual) => self.lock_read_with_current(id, actual, timeout),
                }
            }
            Ok(Response::Mismatch) => Ok(false),
            Err(err) => Err(err),
        }
    }

    /// Read unlocks the header. The header must be currently read locked and the ID must match the
    /// header's current ID.
    pub unsafe fn unlock_read(&self, id: u32) {
        let current = self.state.load(Ordering::Relaxed);
        self.unlock_read_with_current(id, current)
    }

    unsafe fn unlock_read_with_current(&self, id: u32, current: u64) {
        debug_assert!(
            Self::readers_from_bits(current) != 0,
            "attempted to read unlock already unlocked header"
        );

        debug_assert!(
            !Self::is_write_locked(current),
            "attempted to read unlock write locked header"
        );

        debug_assert!(
            Self::id_from_bits(current) == id,
            "attempted to read unlock with ID 0x{:x} but it was actually 0x{:x}",
            id,
            Self::id_from_bits(current)
        );

        let must_unpark =
            Self::has_thread_blocking(current) && Self::readers_from_bits(current) == 1;

        let new = if must_unpark {
            Self::unmark_thread_blocking(Self::decrement_readers(current))
        } else {
            Self::decrement_readers(current)
        };

        match self.compare_exchange_weak(current, new) {
            Ok(_) => {
                if must_unpark {
                    Park::unpark(&self.state)
                }
            }
            Err(actual) => self.unlock_read_with_current(id, actual),
        }
    }

    /// Write locks the header if its ID matches and it is not locked. If the IDs match the header
    /// must be occupied.
    pub unsafe fn lock_write(&self, id: u32, timeout: Timeout) -> BlockResult<bool> {
        debug_assert!(id != RESERVED_ID, "attempted to write lock the reserved ID");

        self.transition(
            Self::occupied_unlocked_bits(id),
            Self::write_locked_bits(id),
            timeout,
        )
    }

    /// Write unlocks the header. The header must be currently write locked and the ID must match
    /// this header's current ID.
    pub unsafe fn unlock_write(&self, id: u32) {
        let new = Self::occupied_unlocked_bits(id);
        let old = self.state.swap(new, Ordering::AcqRel);

        debug_assert!(
            Self::id_from_bits(old) == id,
            "attempted to write unlock with ID 0x{:x} but it was actually 0x{:x}",
            id,
            Self::id_from_bits(old)
        );

        debug_assert!(
            Self::is_write_locked(old),
            "attempted to write unlock header that was not write locked"
        );

        if Self::has_thread_blocking(old) {
            Park::unpark(&self.state)
        }
    }

    /// Moves the header from an unoccupied state into an occupied one, returning the ID of the
    /// newly occupied header. The header must be in an unoccupied state.
    pub unsafe fn occupy(&self) -> u32 {
        let old = self
            .state
            .fetch_or(Self::thread_notification_mask(), Ordering::AcqRel);

        debug_assert!(
            !Self::is_occupied(old),
            "attempted to occupy occupied header"
        );

        debug_assert!(
            Self::id_from_bits(old) != RESERVED_ID,
            "attempted to occupy header with the reserved ID"
        );

        Self::id_from_bits(old)
    }

    /// Increments the ID of the header and moves it into the unoccupied state if the ID matches. If
    /// the IDs match, the header must be occupied.
    pub unsafe fn remove(&self, id: u32, timeout: Timeout) -> BlockResult<RemoveResult> {
        debug_assert!(id != RESERVED_ID, "attempted to remove the reserved ID");

        let next_id = id + 1;

        let matched = self.transition(
            Self::occupied_unlocked_bits(id),
            Self::unoccupied_bits(next_id),
            timeout,
        )?;

        if matched {
            Ok(RemoveResult::Matched {
                may_reuse: next_id != RESERVED_ID,
            })
        } else {
            Ok(RemoveResult::DidntMatch)
        }
    }

    /// Write unlocks the header, increments the ID and moves it into the unoccupied state. The
    /// header's ID must match and it must be in the write locked state. Returns whether the header
    /// can be reused.
    pub unsafe fn remove_locked(&self, id: u32) -> bool {
        let next_id = id + 1;

        let new = Self::unoccupied_bits(next_id);
        let old = self.state.swap(new, Ordering::AcqRel);

        debug_assert!(
            Self::id_from_bits(old) == id,
            "attempted to write unlock with ID 0x{:x} but it was actually 0x{:x}",
            id,
            Self::id_from_bits(old)
        );

        debug_assert!(
            Self::is_write_locked(old),
            "attempted to write unlock header that was not write locked"
        );

        if Self::has_thread_blocking(old) {
            Park::unpark(&self.state)
        }

        next_id != RESERVED_ID
    }

    /// Sets the state of the header to the new state if it is currently in the expected state. If
    /// the expected value did not match, the thread will block until it does. If the ID of the
    /// actual state is different to the ID of the expected state, this will fail.
    unsafe fn transition(&self, expected: u64, new: u64, timeout: Timeout) -> BlockResult<bool> {
        match self.compare_exchange_weak(expected, new) {
            Ok(_) => Ok(true),
            Err(actual) => {
                if Self::id_from_bits(actual) == Self::id_from_bits(expected) {
                    if Self::readers_from_bits(actual) > 0 {
                        self.transition_slow(expected, new, timeout)
                    } else {
                        self.transition(expected, new, timeout)
                    }
                } else {
                    Ok(false)
                }
            }
        }
    }

    #[inline(never)]
    unsafe fn transition_slow(
        &self,
        expected: u64,
        new: u64,
        timeout: Timeout,
    ) -> BlockResult<bool> {
        let timeout = match timeout {
            Timeout::DontBlock => return BlockResult::Err(TimedOut),
            Timeout::BlockIndefinitely => None,
            Timeout::BlockUntil(deadline) => Some(deadline),
        };

        self.block(timeout, move || {
            match self.compare_exchange(expected, new) {
                Ok(_) => BlockChoice::DontBlock(true),
                Err(actual) => {
                    if Self::id_from_bits(actual) == Self::id_from_bits(expected) {
                        BlockChoice::Block(actual)
                    } else {
                        BlockChoice::DontBlock(false)
                    }
                }
            }
        })
    }

    /// Performs an operation atomically with respect to unparking which may either return a final
    /// result or decide to block. If the operation decides to block, it must return an expected
    /// value for the current state of the header. This will be used to set the thread notification
    /// bit of the header with a CAS operation. If the CAS fails, the operation will be run again
    /// until it succeeds. If blocking is successful, upon wakeup the entire process will be run
    /// again until the operation decides not to block.
    unsafe fn block<T, F>(&self, timeout: Option<Instant>, f: F) -> BlockResult<T>
    where
        F: Fn() -> BlockChoice<T>,
    {
        match Park::park(&self.state, timeout, || {
            self.block_result_to_park_result(&f)
        }) {
            ParkResult::Waited => self.block(timeout, f),
            ParkResult::TimedOut => Err(TimedOut),
            ParkResult::DidntPark(result) => Ok(result),
        }
    }

    fn block_result_to_park_result<T, F>(&self, f: &F) -> ParkChoice<T>
    where
        F: Fn() -> BlockChoice<T>,
    {
        match f() {
            BlockChoice::Block(expected_state) => {
                let new_state = Self::mark_thread_blocking(expected_state);

                if self.compare_exchange(expected_state, new_state).is_ok() {
                    ParkChoice::Park
                } else {
                    self.block_result_to_park_result(f)
                }
            }
            BlockChoice::DontBlock(result) => ParkChoice::DontPark(result),
        }
    }

    /// Determines whether or not the header is tracking an element.
    pub fn needs_drop(&mut self) -> bool {
        Self::is_occupied(self.state.load_directly())
    }

    /// Determines the ID the header is currently tracking.
    pub fn id(&mut self) -> u32 {
        let state = self.state.load_directly();
        Self::id_from_bits(state)
    }

    pub fn id_if_occupied(&mut self) -> Option<u32> {
        let state = self.state.load_directly();

        if Self::is_occupied(state) {
            Some(Self::id_from_bits(state))
        } else {
            None
        }
    }

    /// Puts the header into the unoccupied state, returning the header's ID if it was occupied.
    pub fn reset(&mut self) -> Option<u32> {
        let state = self.state.load_directly();

        debug_assert!(
            Self::readers_from_bits(state) == 0,
            "header had readers (0x{:x}) when being reset",
            Self::readers_from_bits(state),
        );

        if Self::is_occupied(state) {
            let id = Self::id_from_bits(state);

            debug_assert!(
                !Self::has_thread_blocking(state),
                "header had thread blocking when being reset"
            );

            self.state.store_directly(Self::unoccupied_bits(id));

            Some(id)
        } else {
            None
        }
    }

    fn compare_exchange(&self, expected: u64, new: u64) -> Result<u64, u64> {
        self.state
            .compare_exchange(expected, new, Ordering::Release, Ordering::Relaxed)
    }

    fn compare_exchange_weak(&self, expected: u64, new: u64) -> Result<u64, u64> {
        self.state
            .compare_exchange_weak(expected, new, Ordering::Release, Ordering::Relaxed)
    }

    fn unoccupied_bits(id: u32) -> u64 {
        id as u64
    }

    fn occupied_unlocked_bits(id: u32) -> u64 {
        Self::thread_notification_mask() | Self::unoccupied_bits(id)
    }

    fn thread_notification_mask() -> u64 {
        1u64 << 63
    }

    fn is_occupied(state: u64) -> bool {
        state >> 32 != 0
    }

    fn write_locked_bits(id: u32) -> u64 {
        (id as u64) | ((u32::MAX as u64) << 32)
    }

    fn id_from_bits(bits: u64) -> u32 {
        bits as u32
    }

    fn readers_from_bits(bits: u64) -> u32 {
        (bits >> 32) as u32 & !(1u32 << 31)
    }

    fn is_write_locked(bits: u64) -> bool {
        Self::readers_from_bits(bits) == !(1u32 << 31)
    }

    fn has_thread_blocking(bits: u64) -> bool {
        debug_assert!(
            Self::is_occupied(bits),
            "cannot check thread blocking status when unoccupied"
        );

        bits & Self::thread_notification_mask() == 0
    }

    fn mark_thread_blocking(bits: u64) -> u64 {
        debug_assert!(
            Self::readers_from_bits(bits) > 0,
            "cannot block when unlocked"
        );

        debug_assert!(
            Self::id_from_bits(bits) != RESERVED_ID,
            "cannot block on the reserved ID"
        );

        bits & !Self::thread_notification_mask()
    }

    fn unmark_thread_blocking(bits: u64) -> u64 {
        bits | Self::thread_notification_mask()
    }

    fn increment_readers(bits: u64) -> u64 {
        if Self::readers_from_bits(bits) == MAX_CONCURRENT_READS {
            Self::too_many_readers();
        }

        debug_assert!(
            !Self::is_write_locked(bits),
            "cannot add reader when write locked"
        );

        debug_assert!(
            Self::id_from_bits(bits) != RESERVED_ID,
            "cannot lock when empty"
        );

        bits + (1 << 32)
    }

    #[inline(never)]
    fn too_many_readers() -> ! {
        panic!("too many concurrent readers on RwStore element")
    }

    fn decrement_readers(bits: u64) -> u64 {
        debug_assert!(Self::readers_from_bits(bits) != 0, "no readers to remove");
        bits - (1 << 32)
    }
}

#[cfg(debug_assertions)]
impl Drop for Header {
    fn drop(&mut self) {
        if !thread::panicking() {
            let state = self.state.load_directly();

            debug_assert!(
                Self::readers_from_bits(state) == 0,
                "header had readers (0x{:x}) when being dropped",
                Self::readers_from_bits(state),
            );

            debug_assert!(
                !Self::is_occupied(state) || !Self::has_thread_blocking(state),
                "header had thread blocking when being dropped"
            );
        }
    }
}

#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum RemoveResult {
    Matched { may_reuse: bool },
    DidntMatch,
}

enum BlockChoice<T> {
    Block(u64),
    DontBlock(T),
}

#[cfg(test)]
mod test {
    use crate::header::{Header, RemoveResult};
    use crate::timeout::TimedOut;
    use crate::timeout::Timeout::DontBlock;

    #[test]
    fn reset_initially_returns_none() {
        let mut header = Header::new();
        assert_eq!(header.reset(), None);
    }

    #[test]
    fn reset_returns_the_tracked_id() {
        unsafe {
            let mut header = Header::new();
            let id = header.occupy();

            assert_eq!(header.reset(), Some(id));
        }

        unsafe {
            let mut header = Header::new();
            let id = header.occupy();
            header.remove(id, DontBlock).unwrap();
            let id = header.occupy();

            assert_eq!(header.reset(), Some(id));
        }
    }

    #[test]
    fn reset_returns_none_after_double_invocation() {
        unsafe {
            let mut header = Header::new();
            header.occupy();

            header.reset();
            assert_eq!(header.reset(), None);
        }
    }

    #[test]
    fn needs_drop_is_false_initially() {
        let mut header = Header::new();
        assert!(!header.needs_drop());
    }

    #[test]
    fn needs_drop_is_true_after_occupation() {
        unsafe {
            let mut header = Header::new();
            header.occupy();

            assert!(header.needs_drop());
        }
    }

    #[test]
    fn needs_drop_is_false_after_removal() {
        unsafe {
            let mut header = Header::new();
            let id = header.occupy();

            header.remove(id, DontBlock).unwrap();
            assert!(!header.needs_drop());
        }
    }

    #[test]
    fn needs_drop_is_false_after_locked_removal() {
        unsafe {
            let mut header = Header::new();
            let id = header.occupy();

            header.lock_write(id, DontBlock).unwrap();
            header.remove_locked(id);

            assert!(!header.needs_drop());
        }
    }

    #[test]
    fn lock_read_succeeds_when_id_matches() {
        unsafe {
            let header = Header::new();
            let id = header.occupy();

            assert_eq!(header.lock_read(id, DontBlock), Ok(true));
            header.unlock_read(id);
        }
    }

    #[test]
    fn lock_write_succeeds_when_id_matches() {
        unsafe {
            let header = Header::new();
            let id = header.occupy();

            assert_eq!(header.lock_write(id, DontBlock), Ok(true));
            header.unlock_write(id);
        }
    }

    #[test]
    fn lock_read_fails_when_id_doesnt_match() {
        unsafe {
            let header = Header::new();
            let id = header.occupy();

            assert_eq!(header.lock_read(id + 1, DontBlock), Ok(false));
        }
    }

    #[test]
    fn lock_write_fails_when_id_doesnt_match() {
        unsafe {
            let header = Header::new();
            let id = header.occupy();

            assert_eq!(header.lock_write(id + 1, DontBlock), Ok(false));
        }
    }

    #[test]
    fn double_read_lock_succeeds() {
        unsafe {
            let header = Header::new();
            let id = header.occupy();

            header.lock_read(id, DontBlock).unwrap();
            assert_eq!(header.lock_read(id, DontBlock), Ok(true));
            header.unlock_read(id);
            header.unlock_read(id);
        }
    }

    #[test]
    fn remove_succeeds_when_id_matches() {
        unsafe {
            let header = Header::new();
            let id = header.occupy();

            assert_eq!(
                header.remove(id, DontBlock),
                Ok(RemoveResult::Matched { may_reuse: true })
            );
        }
    }

    #[test]
    fn remove_fails_when_id_doesnt_match() {
        unsafe {
            let header = Header::new();
            let id = header.occupy();

            assert_eq!(
                header.remove(id + 1, DontBlock),
                Ok(RemoveResult::DidntMatch)
            );
        }
    }

    #[test]
    fn remove_fails_before_occupation() {
        unsafe {
            let header = Header::new();
            assert_eq!(header.remove(42, DontBlock), Ok(RemoveResult::DidntMatch));
        }
    }

    #[test]
    fn remove_fails_after_double_invocation() {
        unsafe {
            let header = Header::new();
            let id = header.occupy();

            header.remove(id, DontBlock).unwrap();
            assert_eq!(header.remove(id, DontBlock), Ok(RemoveResult::DidntMatch));
        }
    }

    #[test]
    fn cannot_lock_read_when_locking_write() {
        unsafe {
            let header = Header::new();
            let id = header.occupy();

            header.lock_write(id, DontBlock).unwrap();
            assert_eq!(header.lock_read(id, DontBlock), Err(TimedOut));
            header.unlock_write(id);
        }
    }

    #[test]
    fn cannot_lock_write_when_locking_read() {
        unsafe {
            let header = Header::new();
            let id = header.occupy();

            header.lock_read(id, DontBlock).unwrap();
            assert_eq!(header.lock_write(id, DontBlock), Err(TimedOut));
            header.unlock_read(id);
        }
    }

    #[test]
    fn cannot_remove_when_locking_read() {
        unsafe {
            let header = Header::new();
            let id = header.occupy();

            header.lock_read(id, DontBlock).unwrap();
            assert_eq!(header.remove(id, DontBlock), Err(TimedOut));
            header.unlock_read(id);
        }
    }

    #[test]
    fn cannot_remove_when_locking_write() {
        unsafe {
            let header = Header::new();
            let id = header.occupy();

            header.lock_write(id, DontBlock).unwrap();
            assert_eq!(header.remove(id, DontBlock), Err(TimedOut));
            header.unlock_write(id);
        }
    }
}