bplustree 0.1.0

Concurrent in-memory B+ Tree featuring optimistic lock coupling
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
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
//! Implementation of a hybrid latch based on the LeanStore paper.
//!
//! The key difference from a standard `RwLock` is the ability of acquiring optimistic read access
//! without perfoming any writes to memory. This mode of access is called optimistic because it allows reads
//! to the underlying data even though writers may be able to acquire exclusive access without
//! being blocked and perform writes while optimistic access is still in place.
//!
//! Those reads would normaly result in undefined behavior, but can be made safe by correctly validating
//! each optimistic access before allowing any side effects to happen. The validation is performed through
//! the [`OptimisticGuard::recheck`] method that returns an [`error::Error::Unwind`] if any writes could have taken place since
//! the acquisition of the optimistic access.
//!
//! We refer to unwinding as the premature return from a function that performed invalid accesses with the
//! error variant [`error::Error::Unwind`].
//!
//! The `?` operator is a very ergonomic way to perform this kind of validation
//! ```
//! use bplustree::latch::HybridLatch;
//! use bplustree::error;
//!
//! let latch = HybridLatch::new(10usize);
//! let mut guard = latch.optimistic_or_spin();
//!
//! loop {
//!     let access = || {
//!         let n = *guard;
//!         if n == 10 {
//!             guard.recheck()?; // validation
//!             println!("n is 10"); // side effect
//!         } else {
//!             guard.recheck()?; // validation
//!             println!("n is not 10"); // side effect
//!         }
//!
//!         error::Result::Ok(())
//!     };
//!
//!     match access() {
//!         Ok(_) => {
//!             break
//!         },
//!         Err(_) => {
//!             // Access was invalidated by some write from another thread,
//!             // acquire a new guard and retry
//!             guard = latch.optimistic_or_spin();
//!             continue
//!         }
//!     }
//! }
//! ```

use parking_lot::{RwLock, RwLockReadGuard, RwLockWriteGuard};
use parking_lot_core::SpinWait;
use std::{
    sync::atomic::{AtomicUsize, Ordering}
};
use std::cell::UnsafeCell;

use crate::error;

/// A hybrid latch that uses versioning to enable optimistic, shared or exclusive access to the
/// underlying data
pub struct HybridLatch<T: ?Sized> {
    version: AtomicUsize,
    lock: RwLock<()>,
    data: UnsafeCell<T>
}

unsafe impl<T: ?Sized + Send> Send for HybridLatch<T> {}
unsafe impl<T: ?Sized + Send + Sync> Sync for HybridLatch<T> {}

impl<T> HybridLatch<T> {
    /// Creates a new instance of a `HybridLatch<T>` which is unlocked.
    #[inline]
    pub fn new(data: T) -> HybridLatch<T> {
        HybridLatch {
            version: AtomicUsize::new(0),
            data: UnsafeCell::new(data),
            lock: RwLock::new(()),
        }
    }

    /// Locks this `HybridLatch` with exclusive write access, blocking the thread until it can be
    /// acquired.
    ///
    /// Returns an RAII guard which will release the exclusive access when dropped
    #[inline]
    pub fn exclusive(&self) -> ExclusiveGuard<'_, T> {
        let guard = self.lock.write();
        let version = self.version.load(Ordering::Relaxed) + 1;
        self.version.store(version, Ordering::Release);
        ExclusiveGuard {
            latch: self,
            guard,
            data: self.data.get(),
            version
        }
    }

    /// Locks this `HybridLatch` with shared read access, blocking the thread until it can be
    /// acquired.
    ///
    /// Returns an RAII guard which will release the shared access when dropped
    #[inline]
    pub fn shared(&self) -> SharedGuard<'_, T> {
        let guard = self.lock.read();
        let version = self.version.load(Ordering::Relaxed);
        SharedGuard {
            latch: self,
            guard,
            data: self.data.get(),
            version
        }
    }

    /// Acquires optimistic read access from this `HybridLatch`, spinning until it can be acquired.
    ///
    /// Optimistic access must be validated before performing any action based on a read of the
    /// underlying data. See [`OptimisticGuard::recheck`] for the details.
    ///
    /// Returns an RAII guard which will NOT validate any accesses when dropped.
    #[inline(never)]
    pub fn optimistic_or_spin(&self) -> OptimisticGuard<'_, T> {
        let mut version = self.version.load(Ordering::Acquire);
        if (version & 1) == 1 {
            let mut spinwait = SpinWait::new();
            loop {
                version = self.version.load(Ordering::Acquire);
                if (version & 1) == 1 {
                    let result = spinwait.spin();
                    if !result {
                        spinwait.reset();
                    }
                    continue
                } else {
                    break
                }
            }
        }

        OptimisticGuard {
            latch: self,
            data: self.data.get(),
            version
        }
    }

    /// Tries to acquire optimistic read access from this `HybridLatch`, unwinding on contention.
    ///
    /// Optimistic access must be validated before performing any action based on a read of the
    /// underlying data. See [`OptimisticGuard::recheck`] for the details.
    ///
    /// Returns an RAII guard which will NOT validate any accesses when dropped.
    #[inline]
    pub fn optimistic_or_unwind(&self) -> error::Result<OptimisticGuard<'_, T>> {
        let version = self.version.load(Ordering::Acquire);
        if (version & 1) == 1 {
            return Err(error::Error::Unwind)
        }

        Ok(OptimisticGuard {
            latch: self,
            data: self.data.get(),
            version
        })
    }

    /// Tries to acquire optimistic read access from this `HybridLatch`, falling back to shared
    /// access on contention.
    ///
    /// Optimistic access must be validated before performing any action based on a read of the
    /// underlying data. See [`OptimisticGuard::recheck`] for the details.
    ///
    /// Acquiring shared access will may block the current thread. Reads from shared access do not
    /// need to be validated.
    ///
    /// Returns either an [`OptimisticGuard`] or a [`SharedGuard`] through the [`OptimisticOrShared`] enum.
    #[inline]
    pub fn optimistic_or_shared(&self) -> OptimisticOrShared<'_, T> {
        let version = self.version.load(Ordering::Acquire);
        if (version & 1) == 1 {
            let guard = self.lock.read();
            let version = self.version.load(Ordering::Relaxed);
            OptimisticOrShared::Shared(SharedGuard {
                latch: self,
                guard,
                data: self.data.get(),
                version
            })
        } else {
            OptimisticOrShared::Optimistic(OptimisticGuard {
                latch: self,
                data: self.data.get(),
                version
            })
        }
    }

    /// Tries to acquire optimistic read access from this `HybridLatch`, falling back to exclusive
    /// access on contention.
    ///
    /// Optimistic access must be validated before performing any action based on a read of the
    /// underlying data. See [`OptimisticGuard::recheck`] for the details.
    ///
    /// Acquiring exclusive access will may block the current thread. Reads or writes from exclusive access do not
    /// need to be validated.
    ///
    /// Returns either an [`OptimisticGuard`] or an [`ExclusiveGuard`] through the [`OptimisticOrExclusive`] enum.
    #[inline]
    pub fn optimistic_or_exclusive(&self) -> OptimisticOrExclusive<'_, T> {
        let version = self.version.load(Ordering::Acquire);
        if (version & 1) == 1 {
            let guard = self.lock.write();
            let version = self.version.load(Ordering::Relaxed) + 1;
            self.version.store(version, Ordering::Release);
            OptimisticOrExclusive::Exclusive(ExclusiveGuard {
                latch: self,
                guard,
                data: self.data.get(),
                version
            })
        } else {
            OptimisticOrExclusive::Optimistic(OptimisticGuard {
                latch: self,
                data: self.data.get(),
                version
            })
        }
    }
}

impl<T> std::convert::AsMut<T> for HybridLatch<T> {
    #[inline]
    fn as_mut(&mut self) -> &mut T {
        unsafe { &mut *self.data.get() }
    }
}

/// Trait to allow using any guard when only read access is needed.
pub trait HybridGuard<T: ?Sized> {
    /// Allows read access to the undelying data, which must be validated before any side effects
    fn inner(&self) -> &T;

    /// Validates any accesses performed.
    ///
    /// The user of a `HybridGuard` must validate all accesses because there is no guarantee of which
    /// mode the accesses are being performed.
    ///
    /// If validation fails it returns [`error::Error::Unwind`].
    fn recheck(&self) -> error::Result<()>;

    /// Returns a reference to the original `HybridLatch` struct
    fn latch(&self) -> &HybridLatch<T>;
}

/// Structure used to perform optimistic accesses and validation.
pub struct OptimisticGuard<'a, T: ?Sized> {
    latch: &'a HybridLatch<T>,
    data: *const T,
    version: usize
}

unsafe impl<'a, T: ?Sized + Sync> Sync for OptimisticGuard<'a, T> {}

impl<'a, T> OptimisticGuard<'a, T> {
    /// Validates all previous optimistic accesses since the creation of the guard,
    /// if validation fails an [`error::Error::Unwind`] is returned to signal that the
    /// stack should be unwinded (by conditional returns) to a safe state.
    #[inline]
    pub fn recheck(&self) -> error::Result<()> {
        if self.version != self.latch.version.load(Ordering::Acquire) {
            return Err(error::Error::Unwind)
        }
        Ok(())
    }

    /// Tries to acquire exclusive access after validation of all previous optimistic accesses on
    /// this guard.
    ///
    /// If validation fails it returns [`error::Error::Unwind`].
    #[inline]
    pub fn to_exclusive(self) -> error::Result<ExclusiveGuard<'a, T>> {
        let new_version = self.version + 1;
        let expected = self.version;
        let locked = self.latch.lock.write();
        if self.latch.version
            .compare_exchange(
                expected,
                new_version,
                Ordering::Acquire,
                Ordering::Acquire).is_err()
        {
            drop(locked);
            return Err(error::Error::Unwind)
        }

        Ok(ExclusiveGuard {
            latch: self.latch,
            guard: locked,
            data: self.data as *mut _,
            version: new_version
        })
    }

    /// Tries to acquire shared access after validation of all previous optimistic accesses on
    /// this guard.
    ///
    /// If validation fails it returns [`error::Error::Unwind`].
    #[inline]
    pub fn to_shared(self) -> error::Result<SharedGuard<'a, T>> {
        if let Some(guard) = self.latch.lock.try_read() {
            if self.version != self.latch.version.load(Ordering::Relaxed) {
                return Err(error::Error::Unwind)
            }

            Ok(SharedGuard {
                latch: self.latch,
                guard,
                data: self.data,
                version: self.version
            })
        } else {
            return Err(error::Error::Unwind)
        }
    }

    /// Returns a reference to the original `HybridLatch` struct
    pub fn latch(&self) -> &'a HybridLatch<T> {
        self.latch
    }
}

impl<'a, T> std::ops::Deref for OptimisticGuard<'a, T> {
    type Target = T;

    fn deref(&self) -> &T {
        unsafe { &*self.data }
    }
}

impl<'a, T> HybridGuard<T> for OptimisticGuard<'a, T> {
    fn inner(&self) -> &T {
        self
    }
    fn recheck(&self) -> error::Result<()> {
        self.recheck()
    }
    fn latch(&self) -> &HybridLatch<T> {
        self.latch()
    }
}

/// RAII structure used to release the exclusive write access of a latch when dropped.
pub struct ExclusiveGuard<'a, T: ?Sized> {
    latch: &'a HybridLatch<T>,
    #[allow(dead_code)]
    guard: RwLockWriteGuard<'a, ()>,
    data: *mut T,
    version: usize
}

unsafe impl<'a, T: ?Sized + Sync> Sync for ExclusiveGuard<'a, T> {}

impl<'a, T> ExclusiveGuard<'a, T> {
    /// A sanity assertion, exclusive guards do not need to be validated
    #[inline]
    pub fn recheck(&self) {
        assert!(self.version == self.latch.version.load(Ordering::Relaxed));
    }

    /// Unlocks the `HybridLatch` returning a [`OptimisticGuard`] in the current version
    #[inline]
    pub fn unlock(self) -> OptimisticGuard<'a, T> {
        let new_version = self.version + 1;
        let latch = self.latch;
        let data = self.data;
        // The version is incremented in drop
        drop(self);
        OptimisticGuard {
            latch,
            data,
            version: new_version
        }
    }

    /// Returns a reference to the original `HybridLatch` struct
    pub fn latch(&self) -> &'a HybridLatch<T> {
        self.latch
    }
}

impl<'a, T: ?Sized> Drop for ExclusiveGuard<'a, T> {
    #[inline]
    fn drop(&mut self) {
        let new_version = self.version + 1;
        self.latch.version.store(new_version, Ordering::Release);
    }
}

impl<'a, T> std::ops::Deref for ExclusiveGuard<'a, T> {
    type Target = T;

    #[inline]
    fn deref(&self) -> &T {
        unsafe { &*self.data }
    }
}

impl<'a, T> std::ops::DerefMut for ExclusiveGuard<'a, T> {
    #[inline]
    fn deref_mut(&mut self) -> &mut T {
        unsafe { &mut *self.data }
    }
}

impl<'a, T> std::convert::AsMut<T> for ExclusiveGuard<'a, T> {
    #[inline]
    fn as_mut(&mut self) -> &mut T {
        unsafe { &mut *self.data }
    }
}

impl<'a, T> HybridGuard<T> for ExclusiveGuard<'a, T> {
    fn inner(&self) -> &T {
        self
    }
    fn recheck(&self) -> error::Result<()> {
        self.recheck();
        Ok(())
    }
    fn latch(&self) -> &HybridLatch<T> {
        self.latch()
    }
}

/// RAII structure used to release the shared read access of a latch when dropped.
pub struct SharedGuard<'a, T: ?Sized> {
    latch: &'a HybridLatch<T>,
    #[allow(dead_code)]
    guard: RwLockReadGuard<'a, ()>,
    data: *const T,
    version: usize
}

unsafe impl<'a, T: ?Sized + Sync> Sync for SharedGuard<'a, T> {}

impl<'a, T> SharedGuard<'a, T> {
    /// A sanity assertion, exclusive guards do not need to be validated
    #[inline]
    pub fn recheck(&self) {
        assert!(self.version == self.latch.version.load(Ordering::Relaxed));
    }

    /// Unlocks the `HybridLatch` returning a [`OptimisticGuard`] in the current version
    #[inline]
    pub fn unlock(self) -> OptimisticGuard<'a, T> {
        OptimisticGuard {
            latch: self.latch,
            data: self.data,
            version: self.version
        }
    }

    /// Returns a [`OptimisticGuard`] in the current version without consuming the original `SharedGuard`
    #[inline]
    pub fn as_optimistic<'b>(&'b self) -> OptimisticGuard<'b, T> {
        OptimisticGuard {
            latch: self.latch,
            data: self.data,
            version: self.version
        }
    }

    /// Returns a reference to the original `HybridLatch` struct
    pub fn latch(&self) -> &'a HybridLatch<T> {
        self.latch
    }
}

impl<'a, T> std::ops::Deref for SharedGuard<'a, T> {
    type Target = T;

    #[inline]
    fn deref(&self) -> &T {
        unsafe { &*self.data }
    }
}

impl<'a, T> HybridGuard<T> for SharedGuard<'a, T> {
    fn inner(&self) -> &T {
        self
    }
    fn recheck(&self) -> error::Result<()> {
        self.recheck();
        Ok(())
    }
    fn latch(&self) -> &HybridLatch<T> {
        self.latch()
    }
}

/// Either an `OptimisticGuard` or a `SharedGuard`.
pub enum OptimisticOrShared<'a, T> {
    Optimistic(OptimisticGuard<'a, T>),
    Shared(SharedGuard<'a, T>)
}

impl<'a, T> OptimisticOrShared<'a, T> {
    #[inline]
    pub fn recheck(&self) -> error::Result<()> {
        match self {
            OptimisticOrShared::Optimistic(g) => g.recheck(),
            OptimisticOrShared::Shared(g) => {
                g.recheck();
                Ok(())
            }
        }
    }
}

/// Either an `OptimisticGuard` or an `ExclusiveGuard`.
pub enum OptimisticOrExclusive<'a, T> {
    Optimistic(OptimisticGuard<'a, T>),
    Exclusive(ExclusiveGuard<'a, T>)
}

impl<'a, T> OptimisticOrExclusive<'a, T> {
    #[inline]
    pub fn recheck(&self) -> error::Result<()> {
        match self {
            OptimisticOrExclusive::Optimistic(g) => g.recheck(),
            OptimisticOrExclusive::Exclusive(g) => {
                g.recheck();
                Ok(())
            }
        }
    }
}


#[cfg(test)]
mod tests {
    use super::HybridLatch;
    use crate::error;
    use std::cell::UnsafeCell;
    use std::sync::Arc;
    use std::thread;
    use serial_test::serial;

    struct Wrapper<T>(UnsafeCell<[T; 1000]>);
    unsafe impl<T: Send> Send for Wrapper<T> {}
    unsafe impl<T: Send + Sync> Sync for Wrapper<T> {}

    #[test]
    #[serial]
    fn single_threaded_reader_baseline() {
        let data = [1usize; 1000];
        let mut result = 1usize;
        let t0 = std::time::Instant::now();
        for _i in 0..4000000 {
            for j in 0..1000 {
                result = result.saturating_mul(data[j]);
            }
        }
        println!("Single threaded reader done in {:?}", t0.elapsed());
        assert!(result == 1);
    }

    #[test]
    #[serial]
    fn concurrent_reading_and_writing() {
        let data = Arc::new(Wrapper(UnsafeCell::new([1usize; 1000])));
        let latch = Arc::new(HybridLatch::new(()));

        let n_readers = 3;
        let n_writers = 1;
        let n = n_readers + n_writers;
        let barrier = Arc::new(std::sync::Barrier::new(n + 1));

        // unsafe { (*data.0.get())[3] = 2 };

        let mut readers = vec![];
        for _i in 0..n_readers {
            let data = data.clone();
            let latch = latch.clone();
            let barrier = barrier.clone();

            let handle = thread::spawn(move || {
                barrier.wait();
                let mut result = 1usize;
                for _i in 0..4000000 {
                    loop {
                        let res = {
                            let attempt = || {
                                let locked = latch.optimistic_or_spin();
                                let arr = data.0.get();
                                let mut result = 1usize;
                                for j in 0..1000 {
                                    result = result.saturating_mul(unsafe { (*arr)[j] });
                                }
                                locked.recheck()?;
                                error::Result::Ok(result)
                            };
                            attempt()
                        };
                        match res {
                            Ok(v) => {
                                result *= v;
                                break;
                            }
                            Err(_) => {
                                // TODO maybe backoff;
                                continue;
                            }
                        }
                    }
                    assert!(result == 1);
                }

                assert!(result == 1);
            });
            readers.push(handle);
        }

        let mut writers = vec![];
        for _i in 0..n_writers {
            let data = data.clone();
            let latch = latch.clone();
            let barrier = barrier.clone();

            let handle = thread::spawn(move || {
                barrier.wait();
                let seconds = 10f64;
                let micros_per_sec = 1_000_000;
                let freq = 100;
                let critical = 1000;
                for _i in 0..(seconds * freq as f64) as usize {
                    thread::sleep(std::time::Duration::from_micros((micros_per_sec / freq) - critical));
                    {
                        let _locked = latch.exclusive();
                        unsafe { (*data.0.get())[3] = 2 };
                        thread::sleep(std::time::Duration::from_micros(critical));
                        unsafe { (*data.0.get())[3] = 1 };
                    }
                }
            });
            writers.push(handle);
        }

        barrier.wait();
        let t0 = std::time::Instant::now();

        for handle in readers {
            handle.join().unwrap();
        }

        println!("Readers done in {:?}", t0.elapsed());

        for handle in writers {
            handle.join().unwrap();
        }

        println!("Writers done in at most {:?}", t0.elapsed());
    }

    #[test]
    #[serial]
    fn single_threaded_option_reader_baseline() {
        let data = [Some(1usize); 1000];
        let mut result = 1usize;
        let t0 = std::time::Instant::now();
        for _i in 0..4000000 {
            for j in 0..1000 {
                let opt = &data[j];
                if let Some(n) = opt {
                    result = result.saturating_mul(*n);
                } else {
                    result = 0;
                }
            }
        }
        println!("Single threaded option reader done in {:?}", t0.elapsed());
        assert!(result == 1);
    }

    #[test]
    #[serial]
    fn concurrent_option_reading_and_writing() {
        let data = Arc::new(Wrapper(UnsafeCell::new([Some(1usize); 1000])));
        let latch = Arc::new(HybridLatch::new(()));

        let n_readers = 3;
        let n_writers = 1;
        let n = n_readers + n_writers;
        let barrier = Arc::new(std::sync::Barrier::new(n + 1));

        // unsafe { (*data.0.get())[3] = 2 };

        let mut readers = vec![];
        for _i in 0..n_readers {
            let data = data.clone();
            let latch = latch.clone();
            let barrier = barrier.clone();

            let handle = thread::spawn(move || {
                barrier.wait();
                let mut result = 1usize;
                for _i in 0..4000000 {
                    loop {
                        let res = {
                            let attempt = || {
                                let locked = latch.optimistic_or_spin();
                                let arr = data.0.get();
                                let mut result = 1usize;
                                for j in 0..1000 {
                                    let opt = unsafe { &(*arr)[j] };
                                    if let Some(n) = opt {
                                        result = result.saturating_mul(*n);
                                    } else {
                                        result = 0;
                                    }
                                }
                                locked.recheck()?;
                                error::Result::Ok(result)
                            };
                            attempt()
                        };
                        match res {
                            Ok(v) => {
                                result *= v;
                                break;
                            }
                            Err(_) => {
                                // TODO maybe backoff;

                                continue;
                            }
                        }
                    }
                    assert!(result == 1);
                }

                assert!(result == 1);
            });
            readers.push(handle);
        }

        let mut writers = vec![];
        for _i in 0..n_writers {
            let data = data.clone();
            let latch = latch.clone();
            let barrier = barrier.clone();

            let handle = thread::spawn(move || {
                barrier.wait();
                let seconds = 10f64;
                let micros_per_sec = 1_000_000;
                let freq = 100;
                let critical = 1000;
                for _i in 0..(seconds * freq as f64) as usize {
                    thread::sleep(std::time::Duration::from_micros((micros_per_sec / freq) - critical));
                    {
                        let _locked = latch.exclusive();
                        unsafe { (*data.0.get())[3] = None };
                        thread::sleep(std::time::Duration::from_micros(critical));
                        unsafe { (*data.0.get())[3] = Some(1) };
                    }
                }
            });
            writers.push(handle);
        }

        barrier.wait();
        let t0 = std::time::Instant::now();

        for handle in readers {
            handle.join().unwrap();
        }

        println!("Readers done in {:?}", t0.elapsed());

        for handle in writers {
            handle.join().unwrap();
        }

        println!("Writers done in at most {:?}", t0.elapsed());
    }
}