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
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
mod tid;

pub use self::tid::ThreadId;

use owned_alloc::{Cache, OwnedAlloc, UninitAlloc};
use ptr::check_null_align;
use std::{
    fmt,
    marker::PhantomData,
    mem::{forget, replace},
    ptr::{null_mut, NonNull},
    sync::atomic::{AtomicPtr, Ordering::*},
};

const BITS: usize = 8;

/// Per Object Thread Local Storage. The stored data is not dropped on thread
/// exit. It is only dropped when the structure itself is dropped. After the
/// thread exited, the data might be reused for other threads. This TLS's
/// operation are also wait-free.
///
/// # Example
/// ```
/// extern crate lockfree;
///
/// use lockfree::tls::ThreadLocal;
/// use std::{cell::Cell, sync::Arc, thread};
///
/// let tls = Arc::new(ThreadLocal::<Cell<usize>>::new());
/// let mut threads = Vec::with_capacity(32);
///
/// for i in 1 ..= 32 {
///     let tls = tls.clone();
///     threads.push(thread::spawn(move || {
///         tls.with_default().set(i);
///
///         if tls.get().map(|c| assert_eq!(c.get(), i)).is_none() {
///             // Some OSes mis-run the destructors for their TLS
///             // implementation.
///             eprintln!("Warning: OS mis-reset the global thread state")
///         }
///     }))
/// }
///
/// for thread in threads {
///     thread.join().unwrap();
/// }
/// ```
pub struct ThreadLocal<T> {
    top: OwnedAlloc<Table<T>>,
}

impl<T> ThreadLocal<T> {
    /// Creates an empty thread local storage.
    pub fn new() -> Self {
        check_null_align::<Table<T>>();
        check_null_align::<Entry<T>>();
        Self { top: Table::new_alloc() }
    }

    /// Removes and drops all entries. The TLS is considered empty then. This
    /// method is only available with exclusive references. This method is
    /// merely for optimization since the TLS is cleared at drop.
    pub fn clear(&mut self) {
        let mut tables = Vec::new();

        // Method clear means we are also resetting all node pointers to null.
        //
        // Safe because we store nodes only correctly.
        unsafe { self.top.clear(&mut tables) }

        while let Some(mut table) = tables.pop() {
            // Method free_nodes means we are only freeing node pointers but not
            // clearing them.
            //
            // Safe because we will never refer to these nodes again.
            unsafe { table.free_nodes(&mut tables) }
        }
    }

    /// Creates an iterator over immutable refereces of entries.
    pub fn iter(&self) -> Iter<T>
    where
        T: Sync,
    {
        Iter { curr_table: Some((&self.top, 0)), tables: Vec::new() }
    }

    /// Creates an iterator over mutable refereces of entries.
    pub fn iter_mut(&mut self) -> IterMut<T>
    where
        T: Send,
    {
        IterMut { curr_table: Some((&mut self.top, 0)), tables: Vec::new() }
    }

    /// Accesses the entry for the current thread. No initialization is
    /// performed.
    #[inline]
    pub fn get(&self) -> Option<&T> {
        self.get_with_id(ThreadId::current())
    }

    /// Accesses the entry for the current thread with a given cached ID.
    /// Repeated calls with cached IDs should be faster than reloading the ID
    /// everytime. No initialization is performed.
    pub fn get_with_id(&self, id: ThreadId) -> Option<&T> {
        let mut table = &*self.top;
        let mut shifted = id.bits();

        loop {
            // The index of the node for our id.
            let index = shifted & (1 << BITS) - 1;

            // Load what is in there.
            let in_place = table.nodes[index].atomic.load(Acquire);

            // Null means there is nothing.
            if in_place.is_null() {
                break None;
            }

            // Having in_place's lower bit set to 0 means it is a
            // pointer to entry.
            if in_place as usize & 1 == 0 {
                // This is safe since:
                //
                // 1. We only store nodes with cleared lower bit if it is an
                // entry.
                //
                // 2. We only delete stuff when we are behind mutable
                // references.
                let entry = unsafe { &*(in_place as *mut Entry<T>) };
                break if entry.id == id {
                    // We only have an entry for the thread if the ids
                    // match.
                    Some(&entry.data)
                } else {
                    None
                };
            }

            // The remaining case (non-null with lower bit set to 1) means
            // we have a child table.
            // Clear the pointer first lower bit so we can dereference it.
            let table_ptr = (in_place as usize & !1) as *mut Table<T>;
            // Set it as the table to be checked in the next iteration.
            // This is safe since:
            //
            // 1. We only store nodes with marked lower bit if it is an
            // table.
            //
            // 2. W cleared up the bit above so we can get the original
            // pointer.
            //
            // 3. We only delete stuff when we are behind mutable
            // references.
            table = unsafe { &*table_ptr };
            // Shift our "hash" for the next level.
            shifted >>= BITS;
        }
    }

    /// Accesses the entry for the current thread. If necessary, the `init`
    /// closure is called to initialize the entry.
    #[inline]
    pub fn with_init<F>(&self, init: F) -> &T
    where
        F: FnOnce() -> T,
    {
        self.with_id_and_init(ThreadId::current(), init)
    }

    /// Accesses the entry for the current thread with a given cached ID.
    /// Repeated calls with cached IDs should be faster than reloading the ID
    /// everytime. If necessary, the `init` closure is called to initialize the
    /// entry.
    pub fn with_id_and_init<F>(&self, id: ThreadId, init: F) -> &T
    where
        F: FnOnce() -> T,
    {
        let mut table = &*self.top;
        // The depth of the iterations.
        let mut depth = 1;
        let mut shifted = id.bits();
        // The pointer stored in place.
        let mut index = shifted & (1 << BITS) - 1;
        let mut in_place = table.nodes[index].atomic.load(Acquire);
        // Using `LazyInit` to make sure we only initialize if there is no
        // entry.
        let mut init = LazyInit::Pending(move || Entry { id, data: init() });
        let mut tbl_cache = Cache::<OwnedAlloc<Table<T>>>::new();

        loop {
            if in_place.is_null() {
                // Null means we have an empty node and also our thread has
                // not stored anything. Let's initialize.
                let nnptr = init.get();
                // First lower bit set to 0 means this is a pointer to
                // entry. This should be guaranteed by the alignment,
                // however, always good to ensure it.
                debug_assert!(nnptr.as_ptr() as usize & 1 == 0);

                // Trying to publish our freshly created entry.
                match table.nodes[index].atomic.compare_exchange(
                    in_place,
                    nnptr.as_ptr() as *mut (),
                    AcqRel,
                    Acquire,
                ) {
                    Ok(_) => {
                        // If the stored value still was null, we succeeded.
                        // Let's read the entry.
                        //
                        // This is safe since... This is the pointer we just
                        // allocated and we only delete nodes through mutable
                        // references to the TLS.
                        break unsafe { &(*nnptr.as_ptr()).data };
                    },

                    Err(new) => in_place = new,
                }
            } else if in_place as usize & 1 == 0 {
                // First lower bit set to 0 means we have an entry.
                //
                // This is safe since:
                //
                // 1. We only store nodes with cleared lower bit if it is an
                // entry.
                //
                // 2. We only delete stuff when we are behind mutable
                // references.
                let entry = unsafe { &*(in_place as *mut Entry<T>) };
                // If ids match, this is the entry for our thread.
                if entry.id == id {
                    // There is no possible way we have initialized the
                    // `LazyInit`. It will only happen if we found an empty
                    // node while searching, and the only way of putting a
                    // non-empty node is either we put it or some other
                    // thread (with different id obviously) put it.
                    debug_assert!(init.is_pending());
                    // And let's read it...
                    break &entry.data;
                }

                // Get a table allocation from the cache.
                let new_tbl = tbl_cache.take_or(Table::new_alloc);

                // Calculate index for the collided entry.
                let other_shifted = entry.id.bits() >> depth * BITS;
                let other_index = other_shifted & (1 << BITS) - 1;

                // Pre-insert it in the table from the cache.
                new_tbl.nodes[other_index].atomic.store(in_place, Relaxed);

                // Forget about the owned allocation and turn it into a
                // pointer.
                let new_tbl_ptr = new_tbl.into_raw();

                // Let's try to publish our work.
                match table.nodes[index].atomic.compare_exchange(
                    in_place,
                    // First lower bit set to 1 means it is a table
                    // pointer.
                    (new_tbl_ptr.as_ptr() as usize | 1) as *mut (),
                    AcqRel,
                    Release,
                ) {
                    Ok(_) => {
                        // If the old node was still stored, we succeeded.
                        // Let's set the new table as the table for the next
                        // iteration.
                        //
                        // This is safe since it is the table we just allocated
                        // and we only delete it through mutable references to
                        // the TLS.
                        table = unsafe { &*new_tbl_ptr.as_ptr() };
                        // We are going one depth further.
                        depth += 1;
                        // Shift our "hash" for the next level.
                        shifted >>= BITS;
                        // Load new in place pointer.
                        index = shifted & (1 << BITS) - 1;
                        in_place = table.nodes[index].atomic.load(Acquire);
                    },

                    Err(new) => {
                        // If we failed, let's rebuild the owned allocation.
                        //
                        // This is safe since it is the table we just allocated
                        // and we don't share it.
                        let new_tbl =
                            unsafe { OwnedAlloc::from_raw(new_tbl_ptr) };
                        // Clear that pre-inserted node.
                        new_tbl.nodes[other_index]
                            .atomic
                            .store(null_mut(), Relaxed);

                        // Store it into the cache for later.
                        tbl_cache.store(new_tbl);
                        in_place = new;
                    },
                }
            } else {
                // The remaining case (non-null with first lower bit set to
                // 1) is a table. Clear the pointer first lower bit so we
                // can dereference it.
                let table_ptr = (in_place as usize & !1) as *mut Table<T>;

                // Set it as table for the next iteration.
                //
                // 1. We only store nodes with marked lower bit if it is an
                // table.
                //
                // 2. W cleared up the bit above so we can get the original
                // pointer.
                //
                // 3. We only delete stuff when we are behind mutable
                // references.
                table = unsafe { &*table_ptr };
                // We are going one depth further.
                depth += 1;
                // Shift our "hash" for the next level.
                shifted >>= BITS;
                // Load new in place pointer.
                index = shifted & (1 << BITS) - 1;
                in_place = table.nodes[index].atomic.load(Acquire);
            }
        }
    }

    /// Accesses the entry for the current thread. If necessary, the entry is
    /// initialized with default value.
    #[inline]
    pub fn with_default(&self) -> &T
    where
        T: Default,
    {
        self.with_init(T::default)
    }

    /// Accesses the entry for the current thread with a given cached ID.
    /// Repeated calls with cached IDs should be faster than reloading the ID
    /// everytime. If necessary, the entry is initialized with default
    /// value.
    #[inline]
    pub fn with_id_and_default(&self, id: ThreadId) -> &T
    where
        T: Default,
    {
        self.with_id_and_init(id, T::default)
    }
}

impl<T> Drop for ThreadLocal<T> {
    fn drop(&mut self) {
        let mut tables = Vec::new();

        // Method free_nodes means we are only freeing node pointers but not
        // clearing them (no need to clear since nobody will ever use them
        // again, we are dropping the TLS).
        //
        // This is safe because we never load the nodes again.
        unsafe { self.top.free_nodes(&mut tables) }

        while let Some(mut table) = tables.pop() {
            // This is safe because we never load the nodes again.
            unsafe { table.free_nodes(&mut tables) }
        }
    }
}

impl<T> fmt::Debug for ThreadLocal<T>
where
    T: fmt::Debug,
{
    fn fmt(&self, fmtr: &mut fmt::Formatter) -> fmt::Result {
        write!(fmtr, "ThreadLocal {} storage: ", '{')?;
        match self.get() {
            Some(val) => write!(fmtr, "Some({:?})", val)?,
            None => write!(fmtr, "None")?,
        }
        write!(fmtr, "{}", '}')
    }
}

impl<T> Default for ThreadLocal<T> {
    fn default() -> Self {
        Self::new()
    }
}

unsafe impl<T> Send for ThreadLocal<T> {}
unsafe impl<T> Sync for ThreadLocal<T> {}

impl<T> IntoIterator for ThreadLocal<T>
where
    T: Send,
{
    type IntoIter = IntoIter<T>;
    type Item = T;

    fn into_iter(self) -> Self::IntoIter {
        let raw = self.top.raw();
        forget(self);
        // Safe since this is the allocation we just forgot about.
        let top = unsafe { OwnedAlloc::from_raw(raw) };

        IntoIter { curr_table: Some((top, 0)), tables: Vec::new() }
    }
}

impl<'tls, T> IntoIterator for &'tls ThreadLocal<T>
where
    T: Sync,
{
    type IntoIter = Iter<'tls, T>;
    type Item = &'tls T;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

impl<'tls, T> IntoIterator for &'tls mut ThreadLocal<T>
where
    T: Send,
{
    type IntoIter = IterMut<'tls, T>;
    type Item = &'tls mut T;

    fn into_iter(self) -> Self::IntoIter {
        self.iter_mut()
    }
}

/// An iterator over immutable references to entries of TLS.
pub struct Iter<'tls, T>
where
    T: 'tls,
{
    tables: Vec<&'tls Table<T>>,
    curr_table: Option<(&'tls Table<T>, usize)>,
}

impl<'tls, T> Iterator for Iter<'tls, T> {
    type Item = &'tls T;

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            let (table, index) = self.curr_table.take()?;
            match table.nodes.get(index).map(|node| node.atomic.load(Acquire)) {
                Some(ptr) if ptr.is_null() => {
                    self.curr_table = Some((table, index + 1))
                },

                Some(ptr) if ptr as usize & 1 == 0 => {
                    let ptr = ptr as *mut Entry<T>;
                    self.curr_table = Some((table, index + 1));
                    // This is safe since:
                    //
                    // 1. We only store nodes with cleared lower bit if it is an
                    // entry.
                    //
                    // 2. We only delete stuff when we are behind mutable
                    // references *and* there are no mutable references to the
                    // TLS as we are a shared one.
                    break Some(unsafe { &(*ptr).data });
                },

                Some(ptr) => {
                    let ptr = (ptr as usize & !1) as *mut Table<T>;
                    // Set it as table for the next iteration.
                    //
                    // 1. We only store nodes with marked lower bit if it is an
                    // table.
                    //
                    // 2. We cleared up the bit above so we can get the original
                    // pointer.
                    //
                    // 3. We only delete stuff when we are behind mutable
                    // references *and* there are no mutable references to the
                    // TLS as we are a shared one.
                    self.tables.push(unsafe { &mut *ptr });
                    self.curr_table = Some((table, index + 1));
                },

                None => self.curr_table = self.tables.pop().map(|tbl| (tbl, 0)),
            };
        }
    }
}

/// An iterator over mutable references to entries of TLS.
pub struct IterMut<'tls, T>
where
    T: 'tls,
{
    tables: Vec<&'tls mut Table<T>>,
    curr_table: Option<(&'tls mut Table<T>, usize)>,
}

impl<'tls, T> Iterator for IterMut<'tls, T> {
    type Item = &'tls mut T;

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            let (table, index) = self.curr_table.take()?;
            match table.nodes.get_mut(index).map(|node| *node.atomic.get_mut())
            {
                Some(ptr) if ptr.is_null() => {
                    self.curr_table = Some((table, index + 1))
                },

                Some(ptr) if ptr as usize & 1 == 0 => {
                    let ptr = ptr as *mut Entry<T>;
                    self.curr_table = Some((table, index + 1));
                    // This is safe since:
                    //
                    // 1. We only store nodes with cleared lower bit if it is an
                    // entry.
                    //
                    // 2. We only delete stuff when we are behind mutable
                    // references *and* we are the only mutable reference to the
                    // TLS. We are not deleting it.
                    break Some(unsafe { &mut (*ptr).data });
                },

                Some(ptr) => {
                    let ptr = (ptr as usize & !1) as *mut Table<T>;
                    // Set it as table for the next iteration.
                    //
                    // 1. We only store nodes with marked lower bit if it is an
                    // table.
                    //
                    // 2. We cleared up the bit above so we can get the original
                    // pointer.
                    //
                    // 3. We only delete stuff when we are behind mutable
                    // references *and* we are the only mutable reference to the
                    // TLS. We are not deleting it.
                    self.tables.push(unsafe { &mut *ptr });
                    self.curr_table = Some((table, index + 1));
                },

                None => self.curr_table = self.tables.pop().map(|tbl| (tbl, 0)),
            };
        }
    }
}

impl<'tls, T> fmt::Debug for IterMut<'tls, T> {
    fn fmt(&self, fmtr: &mut fmt::Formatter) -> fmt::Result {
        write!(
            fmtr,
            "IterMut {} tables: {:?}, curr_table: {:?} {}",
            '{', self.tables, self.curr_table, '}'
        )
    }
}

/// An iterator over owned references to entries of TLS.
pub struct IntoIter<T> {
    tables: Vec<OwnedAlloc<Table<T>>>,
    curr_table: Option<(OwnedAlloc<Table<T>>, usize)>,
}

impl<T> Iterator for IntoIter<T> {
    type Item = T;

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            let (mut table, index) = self.curr_table.take()?;
            match table.nodes.get_mut(index).map(|node| *node.atomic.get_mut())
            {
                Some(ptr) if ptr.is_null() => {
                    self.curr_table = Some((table, index + 1))
                },

                Some(ptr) if ptr as usize & 1 == 0 => {
                    let ptr = ptr as *mut Entry<T>;
                    // This is safe since:
                    //
                    // 1. We only store nodes with cleared lower bit if it is an
                    // entry.
                    //
                    // 2. We have ownership over the TLS, so no one else is
                    // reading or writing or deleting.
                    let alloc = unsafe {
                        OwnedAlloc::from_raw(NonNull::new_unchecked(ptr))
                    };
                    let (entry, _) = alloc.move_inner();
                    self.curr_table = Some((table, index + 1));
                    break Some(entry.data);
                },

                Some(ptr) => {
                    let ptr = (ptr as usize & !1) as *mut Table<T>;
                    // This is safe since:
                    //
                    // 1. We only store nodes with marked lower bit if it is an
                    // table.
                    //
                    // 2. We have ownership over the TLS, so no one else is
                    // reading or writing or deleting.
                    self.tables.push(unsafe {
                        OwnedAlloc::from_raw(NonNull::new_unchecked(ptr))
                    });
                    self.curr_table = Some((table, index + 1));
                },

                None => self.curr_table = self.tables.pop().map(|tbl| (tbl, 0)),
            };
        }
    }
}

impl<T> fmt::Debug for IntoIter<T> {
    fn fmt(&self, fmtr: &mut fmt::Formatter) -> fmt::Result {
        write!(
            fmtr,
            "IterMut {} tables: {:?}, curr_table: {:?} {}",
            '{', self.tables, self.curr_table, '}'
        )
    }
}

struct Node<T> {
    // lower bit marked 0 for Entry, 1 for Table
    atomic: AtomicPtr<()>,
    _marker: PhantomData<T>,
}

impl<T> Node<T> {
    // Unsafe because it is *pretty easy* to make undefined behavior out of this
    // because the pointer does not have even a fixed type.
    unsafe fn free_ptr(
        ptr: *mut (),
        tbl_stack: &mut Vec<OwnedAlloc<Table<T>>>,
    ) {
        if ptr.is_null() {
            return;
        }

        if ptr as usize & 1 == 0 {
            OwnedAlloc::from_raw(NonNull::new_unchecked(ptr as *mut Entry<T>));
        } else {
            let table_ptr = (ptr as usize & !1) as *mut Table<T>;

            debug_assert!(!table_ptr.is_null());
            tbl_stack
                .push(OwnedAlloc::from_raw(NonNull::new_unchecked(table_ptr)));
        }
    }
}

impl<T> fmt::Debug for Node<T> {
    fn fmt(&self, fmtr: &mut fmt::Formatter) -> fmt::Result {
        write!(fmtr, "Node {} pointer: {:?} {}", '{', self.atomic, '}')
    }
}

#[repr(align(/* at least */ 2))]
struct Table<T> {
    nodes: [Node<T>; 1 << BITS],
}

impl<T> Table<T> {
    #[inline]
    fn new_alloc() -> OwnedAlloc<Self> {
        // Safe because it calls a correctly a function which correctly
        // initializes uninitialized memory with, indeed, uninitialized memory.
        unsafe { UninitAlloc::<Self>::new().init_in_place(|this| this.init()) }
    }

    // Unsafe because passing ininitialized memory may cause leaks.
    #[inline]
    unsafe fn init(&mut self) {
        for node_ref in &mut self.nodes as &mut [_] {
            (node_ref as *mut Node<T>).write(Node {
                atomic: AtomicPtr::new(null_mut()),
                _marker: PhantomData,
            })
        }
    }

    // Unsafe because calling this function and using the table again later will
    // cause undefined behavior.
    #[inline]
    unsafe fn free_nodes(&mut self, tbl_stack: &mut Vec<OwnedAlloc<Table<T>>>) {
        for node in &mut self.nodes as &mut [Node<T>] {
            Node::free_ptr(*node.atomic.get_mut(), tbl_stack);
        }
    }

    // Unsafe because storing the wrong pointers in the table will lead to
    // undefined behavior.
    #[inline]
    unsafe fn clear(&mut self, tbl_stack: &mut Vec<OwnedAlloc<Table<T>>>) {
        for node in &mut self.nodes as &mut [Node<T>] {
            let ptr = node.atomic.get_mut();
            Node::free_ptr(*ptr, tbl_stack);
            *ptr = null_mut();
        }
    }
}

impl<T> fmt::Debug for Table<T> {
    fn fmt(&self, fmtr: &mut fmt::Formatter) -> fmt::Result {
        write!(
            fmtr,
            "Table {} nodes: {:?} {}",
            '{', &self.nodes as &[Node<T>], '}'
        )
    }
}

#[repr(align(64))]
struct Entry<T> {
    data: T,
    id: ThreadId,
}

enum LazyInit<T, F> {
    Done(NonNull<T>),
    Pending(F),
}

impl<T, F> LazyInit<T, F>
where
    F: FnOnce() -> T,
{
    fn is_pending(&self) -> bool {
        match self {
            LazyInit::Pending(_) => true,
            _ => false,
        }
    }

    fn get(&mut self) -> NonNull<T> {
        let old = replace(self, LazyInit::Done(NonNull::dangling()));

        let ptr = match old {
            LazyInit::Done(ptr) => ptr,

            LazyInit::Pending(init) => OwnedAlloc::new(init()).into_raw(),
        };

        *self = LazyInit::Done(ptr);
        ptr
    }
}

#[cfg(test)]
mod test {
    use super::ThreadLocal;
    use std::{
        sync::{Arc, Barrier},
        thread,
    };

    #[test]
    fn threads_with_their_id() {
        const THREADS: usize = 32;

        let tls = Arc::new(ThreadLocal::new());
        let mut threads = Vec::with_capacity(THREADS);
        // prevent IDs from being reused.
        let barrier = Arc::new(Barrier::new(THREADS));

        for i in 0 .. THREADS {
            let tls = tls.clone();
            let barrier = barrier.clone();
            threads.push(thread::spawn(move || {
                assert_eq!(*tls.with_init(|| i), i);
                barrier.wait();
            }))
        }

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

    #[test]
    fn iter() {
        const THREADS: usize = 32;

        let tls = Arc::new(ThreadLocal::new());
        let mut threads = Vec::with_capacity(THREADS);
        // prevent IDs from being reused.
        let barrier = Arc::new(Barrier::new(THREADS));

        for i in 0 .. THREADS {
            let tls = tls.clone();
            let barrier = barrier.clone();
            threads.push(thread::spawn(move || {
                tls.with_init(|| i);
                barrier.wait();
            }))
        }

        for entry in &*tls {
            assert!(*entry < THREADS);
        }
    }

    #[test]
    fn iter_mut() {
        const THREADS: usize = 32;

        let tls = Arc::new(ThreadLocal::new());
        let mut threads = Vec::with_capacity(THREADS);
        // prevent IDs from being reused.
        let barrier = Arc::new(Barrier::new(THREADS));

        for i in 0 .. THREADS {
            let tls = tls.clone();
            let barrier = barrier.clone();
            threads.push(thread::spawn(move || {
                tls.with_init(|| i);
                barrier.wait();
            }))
        }

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

        let mut done = [0; THREADS];
        let mut tls = Arc::try_unwrap(tls).unwrap();

        for entry in &mut tls {
            done[*entry] += 1;
            *entry = (*entry + 1) % THREADS;
        }

        for entry in tls {
            done[entry] += 1;
        }

        for &status in &done as &[_] {
            assert_eq!(status, 2);
        }
    }
}