cesiumdb 0.1.0

Blazing fast, persistent key-value store for Rust
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
// Copyright (c) Sienna Satterwhite, CesiumDB Contributors
// SPDX-License-Identifier: GPL-3.0-only WITH Classpath-exception-2.0

#[cfg(target_arch = "aarch64")]
use std::sync::atomic::AtomicU128 as StdAtomicU128;
use std::{
    sync::{
        Arc,
        atomic::{
            AtomicBool,
            Ordering::Relaxed,
        },
    },
    thread,
    time::{
        Duration,
        SystemTime,
    },
};

use crate::stats::STATS;

pub trait HLC: Send + Sync {
    fn time(&self) -> u128;
}

/// How often the clock is synchronized with the source.
pub const TICK_FREQUENCY_IN_NS: u64 = 500;

pub struct HybridLogicalClock {
    #[cfg(target_arch = "aarch64")]
    last_tick: Arc<StdAtomicU128>,
    #[cfg(target_arch = "x86_64")]
    last_tick: Arc<AtomicU128>,
    done: Arc<AtomicBool>,
}

#[allow(clippy::new_without_default)]
impl HybridLogicalClock {
    #[cfg_attr(feature = "telemetry", tracing::instrument(skip_all, level = "debug"))]
    pub fn new() -> Self {
        let now = SystemTime::now()
            .duration_since(SystemTime::UNIX_EPOCH)
            .unwrap()
            .as_nanos();
        #[cfg(target_arch = "aarch64")]
        let last_tick = Arc::new(StdAtomicU128::new(now));
        #[cfg(target_arch = "x86_64")]
        let last_tick = Arc::new(AtomicU128::new(now));

        let done = Arc::new(AtomicBool::new(false));

        let last_tick_clone = last_tick.clone();
        let done_clone = done.clone();
        thread::spawn(move || {
            while !done_clone.load(Relaxed) {
                thread::sleep(Duration::from_nanos(TICK_FREQUENCY_IN_NS));
                let now = SystemTime::now()
                    .duration_since(SystemTime::UNIX_EPOCH)
                    .unwrap()
                    .as_nanos();
                let diff = now - last_tick_clone.load(Relaxed);
                if diff == 0 {
                    continue;
                }
                last_tick_clone.fetch_add(diff, Relaxed);
            }
            STATS.current_threads.fetch_sub(1, Relaxed);
        });
        STATS.current_threads.fetch_add(1, Relaxed);

        Self { last_tick, done }
    }
}

impl HLC for HybridLogicalClock {
    #[inline]
    fn time(&self) -> u128 {
        // Atomically increment and return the new value
        // fetch_add returns the OLD value, so we add 1 to get the new value
        self.last_tick.fetch_add(1, Relaxed) + 1
    }
}

impl Drop for HybridLogicalClock {
    fn drop(&mut self) {
        self.done.store(true, Relaxed);
    }
}

#[cfg(all(test, not(miri)))]
mod tests {
    #[cfg(not(loom))]
    use std::{
        sync::Arc,
        thread,
    };

    #[cfg(loom)]
    use loom::{
        sync::Arc,
        thread,
    };

    use crate::hlc::{
        HLC,
        HybridLogicalClock,
    };

    #[test]
    #[cfg(not(loom))]
    fn test_time() {
        let clock = HybridLogicalClock::new();
        let mut last_time = 0;
        for _ in 0..100 {
            let now = clock.time();
            assert_ne!(now, 0, "clock must never be zero");
            assert!(now > last_time, "now must be greater than last");
            last_time = now;
        }
    }

    #[test]
    #[cfg(not(loom))]
    fn test_concurrent_time_calls() {
        let clock = Arc::new(HybridLogicalClock::new());
        let threads: Vec<_> = (0..4)
            .map(|_| {
                let clock = clock.clone();
                thread::spawn(move || {
                    let mut times = Vec::new();
                    for _ in 0..100 {
                        times.push(clock.time());
                    }
                    times
                })
            })
            .collect();

        let mut all_times = Vec::new();
        for thread in threads {
            all_times.extend(thread.join().unwrap());
        }

        // Verify no duplicates (all timestamps are unique)
        all_times.sort();
        for window in all_times.windows(2) {
            assert_ne!(window[0], window[1], "Timestamps must be unique");
        }
    }

    // Loom tests for HLC concurrent access

    #[test]
    #[cfg(loom)]
    fn loom_hlc_concurrent_time_monotonic() {
        loom::model(|| {
            let clock = Arc::new(HybridLogicalClock::new());

            let c1 = clock.clone();
            let c2 = clock.clone();

            let t1 = thread::spawn(move || {
                let time1 = c1.time();
                let time2 = c1.time();
                assert!(time2 > time1, "Times must be monotonically increasing");
                time1
            });

            let t2 = thread::spawn(move || {
                let time1 = c2.time();
                let time2 = c2.time();
                assert!(time2 > time1, "Times must be monotonically increasing");
                time1
            });

            let _t1_first = t1.join().unwrap();
            let _t2_first = t2.join().unwrap();

            // All timestamps must be unique (no duplicates)
        });
    }

    #[test]
    #[cfg(loom)]
    fn loom_hlc_no_duplicate_timestamps() {
        loom::model(|| {
            let clock = Arc::new(HybridLogicalClock::new());

            let c1 = clock.clone();
            let c2 = clock.clone();

            let t1 = thread::spawn(move || c1.time());
            let t2 = thread::spawn(move || c2.time());

            let time1 = t1.join().unwrap();
            let time2 = t2.join().unwrap();

            // The two timestamps from different threads must be different
            assert_ne!(
                time1, time2,
                "Concurrent time() calls must return unique timestamps"
            );
        });
    }

    #[test]
    #[cfg(loom)]
    fn loom_hlc_multiple_calls() {
        loom::model(|| {
            let clock = Arc::new(HybridLogicalClock::new());

            let c1 = clock.clone();
            let c2 = clock.clone();

            let t1 = thread::spawn(move || {
                let a = c1.time();
                let b = c1.time();
                (a, b)
            });

            let t2 = thread::spawn(move || {
                let a = c2.time();
                let b = c2.time();
                (a, b)
            });

            let (t1_a, t1_b) = t1.join().unwrap();
            let (t2_a, t2_b) = t2.join().unwrap();

            // Each thread's times must be monotonic
            assert!(t1_b > t1_a);
            assert!(t2_b > t2_a);

            // All four timestamps must be unique
            let mut times = vec![t1_a, t1_b, t2_a, t2_b];
            times.sort();
            for window in times.windows(2) {
                assert_ne!(window[0], window[1], "All timestamps must be unique");
            }
        });
    }
}

use std::sync::atomic::{
    AtomicU64,
    Ordering,
};

#[cfg(target_arch = "x86_64")]
#[repr(align(16))]
pub struct AtomicU128 {
    lo: AtomicU64,
    hi: AtomicU64,
}

#[cfg(target_arch = "x86_64")]
impl AtomicU128 {
    pub const fn new(value: u128) -> Self {
        Self {
            lo: AtomicU64::new(value as u64),
            hi: AtomicU64::new((value >> 64) as u64),
        }
    }

    #[cfg_attr(feature = "telemetry", tracing::instrument(skip_all, level = "debug"))]
    pub fn load(&self, order: Ordering) -> u128 {
        // We need to be careful about the ordering here to prevent torn reads
        let hi = self.hi.load(order);
        let lo = self.lo.load(order);
        ((hi as u128) << 64) | (lo as u128)
    }

    #[cfg_attr(feature = "telemetry", tracing::instrument(skip_all, level = "debug"))]
    pub fn store(&self, value: u128, order: Ordering) {
        self.hi.store((value >> 64) as u64, order);
        self.lo.store(value as u64, order);
    }

    #[cfg_attr(feature = "telemetry", tracing::instrument(skip_all, level = "debug"))]
    pub fn compare_exchange(
        &self,
        current: u128,
        new: u128,
        success: Ordering,
        failure: Ordering,
    ) -> Result<u128, u128> {
        let current_hi = (current >> 64) as u64;
        let current_lo = current as u64;
        let new_hi = (new >> 64) as u64;
        let new_lo = new as u64;

        // First try to CAS the high bits
        match self
            .hi
            .compare_exchange(current_hi, new_hi, success, failure)
        {
            | Ok(_) => {
                // High bits matched, now try low bits
                match self
                    .lo
                    .compare_exchange(current_lo, new_lo, success, failure)
                {
                    | Ok(_) => Ok(current),
                    | Err(actual_lo) => {
                        // Low bits failed, restore high bits
                        self.hi.store(current_hi, Ordering::Release);
                        Err(((current_hi as u128) << 64) | (actual_lo as u128))
                    },
                }
            },
            | Err(actual_hi) => {
                // High bits didn't match
                Err(((actual_hi as u128) << 64) | (self.lo.load(failure) as u128))
            },
        }
    }

    #[cfg_attr(feature = "telemetry", tracing::instrument(skip_all, level = "debug"))]
    pub fn fetch_add(&self, val: u128, order: Ordering) -> u128 {
        loop {
            let current = self.load(Ordering::Relaxed);
            if let Ok(old) =
                self.compare_exchange(current, current.wrapping_add(val), order, Ordering::Relaxed)
            {
                return old;
            }
        }
    }

    #[cfg_attr(feature = "telemetry", tracing::instrument(skip_all, level = "debug"))]
    pub fn fetch_sub(&self, val: u128, order: Ordering) -> u128 {
        self.fetch_add(val.wrapping_neg(), order)
    }
}

#[cfg(test)]
#[cfg(target_arch = "x86_64")]
mod x86_atomic_tests {
    #[cfg(not(loom))]
    use std::{
        sync::Arc,
        thread,
        time::Duration,
    };

    #[cfg(loom)]
    use loom::{
        sync::Arc,
        thread,
    };

    use super::*;

    #[test]
    fn test_basic_operations() {
        let atomic = AtomicU128::new(0);

        // Test store and load
        atomic.store(u128::MAX, Ordering::SeqCst);
        assert_eq!(atomic.load(Ordering::SeqCst), u128::MAX);

        // Test compare_exchange
        assert_eq!(
            atomic.compare_exchange(u128::MAX, 42, Ordering::SeqCst, Ordering::SeqCst),
            Ok(u128::MAX)
        );
        assert_eq!(atomic.load(Ordering::SeqCst), 42);
    }

    #[test]
    fn test_edge_values() {
        let atomic = AtomicU128::new(0);
        let test_values = [
            0u128,
            1u128,
            u128::MAX,
            u128::MAX - 1,
            1u128 << 63,
            (1u128 << 64) - 1,
            1u128 << 64,
            (1u128 << 64) + 1,
            1u128 << 127,
        ];

        for &value in &test_values {
            atomic.store(value, Ordering::SeqCst);
            assert_eq!(
                atomic.load(Ordering::SeqCst),
                value,
                "Failed on value: {}",
                value
            );
        }
    }

    #[test]
    fn test_wrapping_behavior() {
        let atomic = AtomicU128::new(u128::MAX);

        // Test wrapping add
        assert_eq!(atomic.fetch_add(1, Ordering::SeqCst), u128::MAX);
        assert_eq!(atomic.load(Ordering::SeqCst), 0);

        // Test wrapping sub
        assert_eq!(atomic.fetch_sub(1, Ordering::SeqCst), 0);
        assert_eq!(atomic.load(Ordering::SeqCst), u128::MAX);
    }

    #[test]
    fn test_compare_exchange_failure() {
        let atomic = AtomicU128::new(0);

        // Expected failure
        let res = atomic.compare_exchange(42, 100, Ordering::SeqCst, Ordering::SeqCst);
        assert!(res.is_err());
        assert_eq!(res.unwrap_err(), 0);

        // Multiple attempts with different values
        let mut success = false;
        for i in 0..10 {
            match atomic.compare_exchange(0, i, Ordering::SeqCst, Ordering::SeqCst) {
                | Ok(_) => {
                    success = true;
                    break;
                },
                | Err(_) => continue,
            }
        }
        assert!(success, "Compare exchange should succeed at least once");
    }

    #[test]
    #[cfg(not(loom))]
    fn test_concurrent_increments() {
        let atomic = Arc::new(AtomicU128::new(0));
        let threads: Vec<_> = (0..4)
            .map(|_| {
                let atomic = Arc::clone(&atomic);
                thread::spawn(move || {
                    for _ in 0..1000 {
                        atomic.fetch_add(1, Ordering::SeqCst);
                    }
                })
            })
            .collect();

        for thread in threads {
            thread.join().unwrap();
        }

        assert_eq!(atomic.load(Ordering::SeqCst), 4000);
    }

    #[test]
    #[cfg(not(loom))]
    fn test_concurrent_mixed_operations() {
        let atomic = Arc::new(AtomicU128::new(1000));
        let threads: Vec<_> = (0..8)
            .map(|i| {
                let atomic = Arc::clone(&atomic);
                thread::spawn(move || {
                    for _ in 0..100 {
                        match i % 4 {
                            | 0 => {
                                atomic.fetch_add(2, Ordering::SeqCst);
                            },
                            | 1 => {
                                atomic.fetch_sub(1, Ordering::SeqCst);
                            },
                            | 2 => {
                                let current = atomic.load(Ordering::SeqCst);
                                let _ = atomic.compare_exchange(
                                    current,
                                    current + 1,
                                    Ordering::SeqCst,
                                    Ordering::SeqCst,
                                );
                            },
                            | _ => {
                                atomic.store(atomic.load(Ordering::SeqCst) + 1, Ordering::SeqCst);
                            },
                        }
                        thread::sleep(Duration::from_nanos(1));
                    }
                })
            })
            .collect();

        for thread in threads {
            thread.join().unwrap();
        }

        let final_value = atomic.load(Ordering::SeqCst);
        assert!(
            final_value > 1000,
            "Value should have increased from concurrent operations"
        );
    }

    #[test]
    fn test_ordering_combinations() {
        let atomic = AtomicU128::new(0);

        // Valid store orderings
        let store_orderings = [Ordering::SeqCst, Ordering::Release, Ordering::Relaxed];

        // Valid load orderings
        let load_orderings = [Ordering::SeqCst, Ordering::Acquire, Ordering::Relaxed];

        for &store_order in &store_orderings {
            for &load_order in &load_orderings {
                atomic.store(42, store_order);
                assert_eq!(atomic.load(load_order), 42);
            }
        }

        // Test compare_exchange with valid ordering combinations
        let success_orderings = [
            Ordering::SeqCst,
            Ordering::AcqRel,
            Ordering::Acquire,
            Ordering::Release,
            Ordering::Relaxed,
        ];

        // Failure ordering must be no stronger than success and cannot be Release or
        // AcqRel
        let failure_orderings = [Ordering::SeqCst, Ordering::Acquire, Ordering::Relaxed];

        for &success_order in &success_orderings {
            for &failure_order in &failure_orderings {
                // Skip invalid combinations where failure is stronger than success
                if (failure_order == Ordering::SeqCst && success_order != Ordering::SeqCst) {
                    continue;
                }

                let _ = atomic.compare_exchange(42, 100, success_order, failure_order);
            }
        }
    }

    #[test]
    fn test_concurrent_stress() {
        let atomic = Arc::new(AtomicU128::new(0));
        let thread_count = 16;
        let iterations = 10_000;

        let threads: Vec<_> = (0..thread_count)
            .map(|id| {
                let atomic = Arc::clone(&atomic);
                thread::spawn(move || {
                    let mut local_sum = 0u128;
                    for i in 0..iterations {
                        let value = i as u128 + id as u128;
                        let old = atomic.fetch_add(value, Ordering::SeqCst);
                        local_sum = local_sum.wrapping_add(old);
                    }
                    local_sum
                })
            })
            .collect();

        let mut total_sum = 0u128;
        for thread in threads {
            total_sum = total_sum.wrapping_add(thread.join().unwrap());
        }

        let final_value = atomic.load(Ordering::SeqCst);
        assert!(
            final_value > 0,
            "Final value should be non-zero after stress test"
        );
    }

    // Loom tests for x86_64 AtomicU128
    // These test the custom split hi/lo implementation

    #[test]
    #[cfg(loom)]
    fn loom_atomic128_concurrent_stores() {
        loom::model(|| {
            let atomic = Arc::new(AtomicU128::new(0));

            let a1 = atomic.clone();
            let a2 = atomic.clone();

            let t1 = thread::spawn(move || {
                a1.store(100, Ordering::SeqCst);
            });

            let t2 = thread::spawn(move || {
                a2.store(200, Ordering::SeqCst);
            });

            t1.join().unwrap();
            t2.join().unwrap();

            let final_val = atomic.load(Ordering::SeqCst);
            // Final value must be either 100 or 200
            assert!(final_val == 100 || final_val == 200);
        });
    }

    #[test]
    #[cfg(loom)]
    fn loom_atomic128_compare_exchange() {
        loom::model(|| {
            let atomic = Arc::new(AtomicU128::new(0));

            let a1 = atomic.clone();
            let a2 = atomic.clone();

            let t1 = thread::spawn(move || {
                a1.compare_exchange(0, 100, Ordering::SeqCst, Ordering::SeqCst)
            });

            let t2 = thread::spawn(move || {
                a2.compare_exchange(0, 200, Ordering::SeqCst, Ordering::SeqCst)
            });

            let r1 = t1.join().unwrap();
            let r2 = t2.join().unwrap();

            // Exactly one should succeed
            assert!(r1.is_ok() ^ r2.is_ok(), "Exactly one CAS should succeed");

            let final_val = atomic.load(Ordering::SeqCst);
            if r1.is_ok() {
                assert_eq!(final_val, 100);
            } else {
                assert_eq!(final_val, 200);
            }
        });
    }

    #[test]
    #[cfg(loom)]
    fn loom_atomic128_fetch_add() {
        loom::model(|| {
            let atomic = Arc::new(AtomicU128::new(0));

            let a1 = atomic.clone();
            let a2 = atomic.clone();

            let t1 = thread::spawn(move || a1.fetch_add(10, Ordering::SeqCst));

            let t2 = thread::spawn(move || a2.fetch_add(20, Ordering::SeqCst));

            let old1 = t1.join().unwrap();
            let old2 = t2.join().unwrap();

            // Both old values should be valid
            assert!(old1 == 0 || old1 == 10 || old1 == 20);
            assert!(old2 == 0 || old2 == 10 || old2 == 20);

            // Final value must be 30
            assert_eq!(atomic.load(Ordering::SeqCst), 30);
        });
    }

    #[test]
    #[cfg(loom)]
    fn loom_atomic128_load_while_storing() {
        loom::model(|| {
            let atomic = Arc::new(AtomicU128::new(100));

            let a1 = atomic.clone();
            let a2 = atomic.clone();

            let t1 = thread::spawn(move || {
                a1.store(200, Ordering::Release);
            });

            let t2 = thread::spawn(move || a2.load(Ordering::Acquire));

            t1.join().unwrap();
            let loaded = t2.join().unwrap();

            // t2 saw either 100 or 200
            assert!(loaded == 100 || loaded == 200);

            // Final must be 200
            assert_eq!(atomic.load(Ordering::SeqCst), 200);
        });
    }

    #[test]
    #[cfg(loom)]
    fn loom_atomic128_compare_exchange_rollback() {
        loom::model(|| {
            let atomic = Arc::new(AtomicU128::new(0));

            let a1 = atomic.clone();
            let a2 = atomic.clone();

            // Thread 1: changes value from 0 to 50
            let t1 = thread::spawn(move || {
                a1.store(50, Ordering::SeqCst);
            });

            // Thread 2: tries CAS with wrong expected value
            let t2 = thread::spawn(move || {
                // This should fail because value is no longer 0
                a2.compare_exchange(0, 100, Ordering::SeqCst, Ordering::SeqCst)
            });

            t1.join().unwrap();
            let cas_result = t2.join().unwrap();

            let final_val = atomic.load(Ordering::SeqCst);

            // If CAS succeeded, final = 100, else final = 50
            if cas_result.is_ok() {
                assert_eq!(final_val, 100);
            } else {
                assert_eq!(final_val, 50);
            }
        });
    }

    #[test]
    #[cfg(loom)]
    fn loom_atomic128_fetch_sub() {
        loom::model(|| {
            let atomic = Arc::new(AtomicU128::new(100));

            let a1 = atomic.clone();
            let a2 = atomic.clone();

            let t1 = thread::spawn(move || a1.fetch_sub(10, Ordering::SeqCst));

            let t2 = thread::spawn(move || a2.fetch_sub(20, Ordering::SeqCst));

            t1.join().unwrap();
            t2.join().unwrap();

            // 100 - 10 - 20 = 70
            assert_eq!(atomic.load(Ordering::SeqCst), 70);
        });
    }

    #[test]
    #[cfg(loom)]
    fn loom_atomic128_high_bits_boundary() {
        loom::model(|| {
            // Test at the boundary between lo and hi u64s
            let boundary = (1u128 << 64) - 1;
            let atomic = Arc::new(AtomicU128::new(boundary));

            let a1 = atomic.clone();
            let a2 = atomic.clone();

            let t1 = thread::spawn(move || a1.fetch_add(1, Ordering::SeqCst));

            let t2 = thread::spawn(move || a2.load(Ordering::SeqCst));

            let old1 = t1.join().unwrap();
            let loaded = t2.join().unwrap();

            let final_val = atomic.load(Ordering::SeqCst);

            // After adding 1 to (2^64 - 1), we get 2^64
            assert_eq!(final_val, 1u128 << 64);
        });
    }
}