]> git.lizzy.rs Git - rust.git/blob - src/librustc_data_structures/sync.rs
Auto merge of #69833 - Centril:rollup-mh74yue, r=Centril
[rust.git] / src / librustc_data_structures / sync.rs
1 //! This module defines types which are thread safe if cfg!(parallel_compiler) is true.
2 //!
3 //! `Lrc` is an alias of `Arc` if cfg!(parallel_compiler) is true, `Rc` otherwise.
4 //!
5 //! `Lock` is a mutex.
6 //! It internally uses `parking_lot::Mutex` if cfg!(parallel_compiler) is true,
7 //! `RefCell` otherwise.
8 //!
9 //! `RwLock` is a read-write lock.
10 //! It internally uses `parking_lot::RwLock` if cfg!(parallel_compiler) is true,
11 //! `RefCell` otherwise.
12 //!
13 //! `MTLock` is a mutex which disappears if cfg!(parallel_compiler) is false.
14 //!
15 //! `MTRef` is an immutable reference if cfg!(parallel_compiler), and a mutable reference otherwise.
16 //!
17 //! `rustc_erase_owner!` erases a OwningRef owner into Erased or Erased + Send + Sync
18 //! depending on the value of cfg!(parallel_compiler).
19
20 use crate::owning_ref::{Erased, OwningRef};
21 use std::collections::HashMap;
22 use std::hash::{BuildHasher, Hash};
23 use std::marker::PhantomData;
24 use std::ops::{Deref, DerefMut};
25
26 pub use std::sync::atomic::Ordering;
27 pub use std::sync::atomic::Ordering::SeqCst;
28
29 cfg_if! {
30     if #[cfg(not(parallel_compiler))] {
31         pub auto trait Send {}
32         pub auto trait Sync {}
33
34         impl<T: ?Sized> Send for T {}
35         impl<T: ?Sized> Sync for T {}
36
37         #[macro_export]
38         macro_rules! rustc_erase_owner {
39             ($v:expr) => {
40                 $v.erase_owner()
41             }
42         }
43
44         use std::ops::Add;
45         use std::panic::{resume_unwind, catch_unwind, AssertUnwindSafe};
46
47         /// This is a single threaded variant of AtomicCell provided by crossbeam.
48         /// Unlike `Atomic` this is intended for all `Copy` types,
49         /// but it lacks the explicit ordering arguments.
50         #[derive(Debug)]
51         pub struct AtomicCell<T: Copy>(Cell<T>);
52
53         impl<T: Copy> AtomicCell<T> {
54             #[inline]
55             pub fn new(v: T) -> Self {
56                 AtomicCell(Cell::new(v))
57             }
58
59             #[inline]
60             pub fn get_mut(&mut self) -> &mut T {
61                 self.0.get_mut()
62             }
63         }
64
65         impl<T: Copy> AtomicCell<T> {
66             #[inline]
67             pub fn into_inner(self) -> T {
68                 self.0.into_inner()
69             }
70
71             #[inline]
72             pub fn load(&self) -> T {
73                 self.0.get()
74             }
75
76             #[inline]
77             pub fn store(&self, val: T) {
78                 self.0.set(val)
79             }
80
81             #[inline]
82             pub fn swap(&self, val: T) -> T {
83                 self.0.replace(val)
84             }
85         }
86
87         /// This is a single threaded variant of `AtomicU64`, `AtomicUsize`, etc.
88         /// It differs from `AtomicCell` in that it has explicit ordering arguments
89         /// and is only intended for use with the native atomic types.
90         /// You should use this type through the `AtomicU64`, `AtomicUsize`, etc, type aliases
91         /// as it's not intended to be used separately.
92         #[derive(Debug)]
93         pub struct Atomic<T: Copy>(Cell<T>);
94
95         impl<T: Copy> Atomic<T> {
96             #[inline]
97             pub fn new(v: T) -> Self {
98                 Atomic(Cell::new(v))
99             }
100         }
101
102         impl<T: Copy> Atomic<T> {
103             #[inline]
104             pub fn into_inner(self) -> T {
105                 self.0.into_inner()
106             }
107
108             #[inline]
109             pub fn load(&self, _: Ordering) -> T {
110                 self.0.get()
111             }
112
113             #[inline]
114             pub fn store(&self, val: T, _: Ordering) {
115                 self.0.set(val)
116             }
117
118             #[inline]
119             pub fn swap(&self, val: T, _: Ordering) -> T {
120                 self.0.replace(val)
121             }
122         }
123
124         impl<T: Copy + PartialEq> Atomic<T> {
125             #[inline]
126             pub fn compare_exchange(&self,
127                                     current: T,
128                                     new: T,
129                                     _: Ordering,
130                                     _: Ordering)
131                                     -> Result<T, T> {
132                 let read = self.0.get();
133                 if read == current {
134                     self.0.set(new);
135                     Ok(read)
136                 } else {
137                     Err(read)
138                 }
139             }
140         }
141
142         impl<T: Add<Output=T> + Copy> Atomic<T> {
143             #[inline]
144             pub fn fetch_add(&self, val: T, _: Ordering) -> T {
145                 let old = self.0.get();
146                 self.0.set(old + val);
147                 old
148             }
149         }
150
151         pub type AtomicUsize = Atomic<usize>;
152         pub type AtomicBool = Atomic<bool>;
153         pub type AtomicU32 = Atomic<u32>;
154         pub type AtomicU64 = Atomic<u64>;
155
156         pub fn join<A, B, RA, RB>(oper_a: A, oper_b: B) -> (RA, RB)
157             where A: FnOnce() -> RA,
158                   B: FnOnce() -> RB
159         {
160             (oper_a(), oper_b())
161         }
162
163         pub struct SerialScope;
164
165         impl SerialScope {
166             pub fn spawn<F>(&self, f: F)
167                 where F: FnOnce(&SerialScope)
168             {
169                 f(self)
170             }
171         }
172
173         pub fn scope<F, R>(f: F) -> R
174             where F: FnOnce(&SerialScope) -> R
175         {
176             f(&SerialScope)
177         }
178
179         #[macro_export]
180         macro_rules! parallel {
181             ($($blocks:tt),*) => {
182                 // We catch panics here ensuring that all the blocks execute.
183                 // This makes behavior consistent with the parallel compiler.
184                 let mut panic = None;
185                 $(
186                     if let Err(p) = ::std::panic::catch_unwind(
187                         ::std::panic::AssertUnwindSafe(|| $blocks)
188                     ) {
189                         if panic.is_none() {
190                             panic = Some(p);
191                         }
192                     }
193                 )*
194                 if let Some(panic) = panic {
195                     ::std::panic::resume_unwind(panic);
196                 }
197             }
198         }
199
200         pub use std::iter::Iterator as ParallelIterator;
201
202         pub fn par_iter<T: IntoIterator>(t: T) -> T::IntoIter {
203             t.into_iter()
204         }
205
206         pub fn par_for_each_in<T: IntoIterator>(t: T, for_each: impl Fn(T::Item) + Sync + Send) {
207             // We catch panics here ensuring that all the loop iterations execute.
208             // This makes behavior consistent with the parallel compiler.
209             let mut panic = None;
210             t.into_iter().for_each(|i| {
211                 if let Err(p) = catch_unwind(AssertUnwindSafe(|| for_each(i))) {
212                     if panic.is_none() {
213                         panic = Some(p);
214                     }
215                 }
216             });
217             if let Some(panic) = panic {
218                 resume_unwind(panic);
219             }
220         }
221
222         pub type MetadataRef = OwningRef<Box<dyn Erased>, [u8]>;
223
224         pub use std::rc::Rc as Lrc;
225         pub use std::rc::Weak as Weak;
226         pub use std::cell::Ref as ReadGuard;
227         pub use std::cell::Ref as MappedReadGuard;
228         pub use std::cell::RefMut as WriteGuard;
229         pub use std::cell::RefMut as MappedWriteGuard;
230         pub use std::cell::RefMut as LockGuard;
231         pub use std::cell::RefMut as MappedLockGuard;
232
233         use std::cell::RefCell as InnerRwLock;
234         use std::cell::RefCell as InnerLock;
235
236         use std::cell::Cell;
237
238         #[derive(Debug)]
239         pub struct WorkerLocal<T>(OneThread<T>);
240
241         impl<T> WorkerLocal<T> {
242             /// Creates a new worker local where the `initial` closure computes the
243             /// value this worker local should take for each thread in the thread pool.
244             #[inline]
245             pub fn new<F: FnMut(usize) -> T>(mut f: F) -> WorkerLocal<T> {
246                 WorkerLocal(OneThread::new(f(0)))
247             }
248
249             /// Returns the worker-local value for each thread
250             #[inline]
251             pub fn into_inner(self) -> Vec<T> {
252                 vec![OneThread::into_inner(self.0)]
253             }
254         }
255
256         impl<T> Deref for WorkerLocal<T> {
257             type Target = T;
258
259             #[inline(always)]
260             fn deref(&self) -> &T {
261                 &*self.0
262             }
263         }
264
265         pub type MTRef<'a, T> = &'a mut T;
266
267         #[derive(Debug, Default)]
268         pub struct MTLock<T>(T);
269
270         impl<T> MTLock<T> {
271             #[inline(always)]
272             pub fn new(inner: T) -> Self {
273                 MTLock(inner)
274             }
275
276             #[inline(always)]
277             pub fn into_inner(self) -> T {
278                 self.0
279             }
280
281             #[inline(always)]
282             pub fn get_mut(&mut self) -> &mut T {
283                 &mut self.0
284             }
285
286             #[inline(always)]
287             pub fn lock(&self) -> &T {
288                 &self.0
289             }
290
291             #[inline(always)]
292             pub fn lock_mut(&mut self) -> &mut T {
293                 &mut self.0
294             }
295         }
296
297         // FIXME: Probably a bad idea (in the threaded case)
298         impl<T: Clone> Clone for MTLock<T> {
299             #[inline]
300             fn clone(&self) -> Self {
301                 MTLock(self.0.clone())
302             }
303         }
304     } else {
305         pub use std::marker::Send as Send;
306         pub use std::marker::Sync as Sync;
307
308         pub use parking_lot::RwLockReadGuard as ReadGuard;
309         pub use parking_lot::MappedRwLockReadGuard as MappedReadGuard;
310         pub use parking_lot::RwLockWriteGuard as WriteGuard;
311         pub use parking_lot::MappedRwLockWriteGuard as MappedWriteGuard;
312
313         pub use parking_lot::MutexGuard as LockGuard;
314         pub use parking_lot::MappedMutexGuard as MappedLockGuard;
315
316         pub use std::sync::atomic::{AtomicBool, AtomicUsize, AtomicU32, AtomicU64};
317
318         pub use crossbeam_utils::atomic::AtomicCell;
319
320         pub use std::sync::Arc as Lrc;
321         pub use std::sync::Weak as Weak;
322
323         pub type MTRef<'a, T> = &'a T;
324
325         #[derive(Debug, Default)]
326         pub struct MTLock<T>(Lock<T>);
327
328         impl<T> MTLock<T> {
329             #[inline(always)]
330             pub fn new(inner: T) -> Self {
331                 MTLock(Lock::new(inner))
332             }
333
334             #[inline(always)]
335             pub fn into_inner(self) -> T {
336                 self.0.into_inner()
337             }
338
339             #[inline(always)]
340             pub fn get_mut(&mut self) -> &mut T {
341                 self.0.get_mut()
342             }
343
344             #[inline(always)]
345             pub fn lock(&self) -> LockGuard<'_, T> {
346                 self.0.lock()
347             }
348
349             #[inline(always)]
350             pub fn lock_mut(&self) -> LockGuard<'_, T> {
351                 self.lock()
352             }
353         }
354
355         use parking_lot::Mutex as InnerLock;
356         use parking_lot::RwLock as InnerRwLock;
357
358         use std;
359         use std::thread;
360         pub use rayon::{join, scope};
361
362         /// Runs a list of blocks in parallel. The first block is executed immediately on
363         /// the current thread. Use that for the longest running block.
364         #[macro_export]
365         macro_rules! parallel {
366             (impl $fblock:tt [$($c:tt,)*] [$block:tt $(, $rest:tt)*]) => {
367                 parallel!(impl $fblock [$block, $($c,)*] [$($rest),*])
368             };
369             (impl $fblock:tt [$($blocks:tt,)*] []) => {
370                 ::rustc_data_structures::sync::scope(|s| {
371                     $(
372                         s.spawn(|_| $blocks);
373                     )*
374                     $fblock;
375                 })
376             };
377             ($fblock:tt, $($blocks:tt),*) => {
378                 // Reverse the order of the later blocks since Rayon executes them in reverse order
379                 // when using a single thread. This ensures the execution order matches that
380                 // of a single threaded rustc
381                 parallel!(impl $fblock [] [$($blocks),*]);
382             };
383         }
384
385         pub use rayon_core::WorkerLocal;
386
387         pub use rayon::iter::ParallelIterator;
388         use rayon::iter::IntoParallelIterator;
389
390         pub fn par_iter<T: IntoParallelIterator>(t: T) -> T::Iter {
391             t.into_par_iter()
392         }
393
394         pub fn par_for_each_in<T: IntoParallelIterator>(
395             t: T,
396             for_each: impl Fn(T::Item) + Sync + Send,
397         ) {
398             t.into_par_iter().for_each(for_each)
399         }
400
401         pub type MetadataRef = OwningRef<Box<dyn Erased + Send + Sync>, [u8]>;
402
403         /// This makes locks panic if they are already held.
404         /// It is only useful when you are running in a single thread
405         const ERROR_CHECKING: bool = false;
406
407         #[macro_export]
408         macro_rules! rustc_erase_owner {
409             ($v:expr) => {{
410                 let v = $v;
411                 ::rustc_data_structures::sync::assert_send_val(&v);
412                 v.erase_send_sync_owner()
413             }}
414         }
415     }
416 }
417
418 pub fn assert_sync<T: ?Sized + Sync>() {}
419 pub fn assert_send<T: ?Sized + Send>() {}
420 pub fn assert_send_val<T: ?Sized + Send>(_t: &T) {}
421 pub fn assert_send_sync_val<T: ?Sized + Sync + Send>(_t: &T) {}
422
423 pub trait HashMapExt<K, V> {
424     /// Same as HashMap::insert, but it may panic if there's already an
425     /// entry for `key` with a value not equal to `value`
426     fn insert_same(&mut self, key: K, value: V);
427 }
428
429 impl<K: Eq + Hash, V: Eq, S: BuildHasher> HashMapExt<K, V> for HashMap<K, V, S> {
430     fn insert_same(&mut self, key: K, value: V) {
431         self.entry(key).and_modify(|old| assert!(*old == value)).or_insert(value);
432     }
433 }
434
435 /// A type whose inner value can be written once and then will stay read-only
436 // This contains a PhantomData<T> since this type conceptually owns a T outside the Mutex once
437 // initialized. This ensures that Once<T> is Sync only if T is. If we did not have PhantomData<T>
438 // we could send a &Once<Cell<bool>> to multiple threads and call `get` on it to get access
439 // to &Cell<bool> on those threads.
440 pub struct Once<T>(Lock<Option<T>>, PhantomData<T>);
441
442 impl<T> Once<T> {
443     /// Creates an Once value which is uninitialized
444     #[inline(always)]
445     pub fn new() -> Self {
446         Once(Lock::new(None), PhantomData)
447     }
448
449     /// Consumes the value and returns Some(T) if it was initialized
450     #[inline(always)]
451     pub fn into_inner(self) -> Option<T> {
452         self.0.into_inner()
453     }
454
455     /// Tries to initialize the inner value to `value`.
456     /// Returns `None` if the inner value was uninitialized and `value` was consumed setting it
457     /// otherwise if the inner value was already set it returns `value` back to the caller
458     #[inline]
459     pub fn try_set(&self, value: T) -> Option<T> {
460         let mut lock = self.0.lock();
461         if lock.is_some() {
462             return Some(value);
463         }
464         *lock = Some(value);
465         None
466     }
467
468     /// Tries to initialize the inner value to `value`.
469     /// Returns `None` if the inner value was uninitialized and `value` was consumed setting it
470     /// otherwise if the inner value was already set it asserts that `value` is equal to the inner
471     /// value and then returns `value` back to the caller
472     #[inline]
473     pub fn try_set_same(&self, value: T) -> Option<T>
474     where
475         T: Eq,
476     {
477         let mut lock = self.0.lock();
478         if let Some(ref inner) = *lock {
479             assert!(*inner == value);
480             return Some(value);
481         }
482         *lock = Some(value);
483         None
484     }
485
486     /// Tries to initialize the inner value to `value` and panics if it was already initialized
487     #[inline]
488     pub fn set(&self, value: T) {
489         assert!(self.try_set(value).is_none());
490     }
491
492     /// Initializes the inner value if it wasn't already done by calling the provided closure. It
493     /// ensures that no-one else can access the value in the mean time by holding a lock for the
494     /// duration of the closure.
495     /// A reference to the inner value is returned.
496     #[inline]
497     pub fn init_locking<F: FnOnce() -> T>(&self, f: F) -> &T {
498         {
499             let mut lock = self.0.lock();
500             if lock.is_none() {
501                 *lock = Some(f());
502             }
503         }
504
505         self.borrow()
506     }
507
508     /// Tries to initialize the inner value by calling the closure without ensuring that no-one
509     /// else can access it. This mean when this is called from multiple threads, multiple
510     /// closures may concurrently be computing a value which the inner value should take.
511     /// Only one of these closures are used to actually initialize the value.
512     /// If some other closure already set the value,
513     /// we return the value our closure computed wrapped in a `Option`.
514     /// If our closure set the value, `None` is returned.
515     /// If the value is already initialized, the closure is not called and `None` is returned.
516     #[inline]
517     pub fn init_nonlocking<F: FnOnce() -> T>(&self, f: F) -> Option<T> {
518         if self.0.lock().is_some() { None } else { self.try_set(f()) }
519     }
520
521     /// Tries to initialize the inner value by calling the closure without ensuring that no-one
522     /// else can access it. This mean when this is called from multiple threads, multiple
523     /// closures may concurrently be computing a value which the inner value should take.
524     /// Only one of these closures are used to actually initialize the value.
525     /// If some other closure already set the value, we assert that it our closure computed
526     /// a value equal to the value already set and then
527     /// we return the value our closure computed wrapped in a `Option`.
528     /// If our closure set the value, `None` is returned.
529     /// If the value is already initialized, the closure is not called and `None` is returned.
530     #[inline]
531     pub fn init_nonlocking_same<F: FnOnce() -> T>(&self, f: F) -> Option<T>
532     where
533         T: Eq,
534     {
535         if self.0.lock().is_some() { None } else { self.try_set_same(f()) }
536     }
537
538     /// Tries to get a reference to the inner value, returns `None` if it is not yet initialized
539     #[inline(always)]
540     pub fn try_get(&self) -> Option<&T> {
541         let lock = &*self.0.lock();
542         if let Some(ref inner) = *lock {
543             // This is safe since we won't mutate the inner value
544             unsafe { Some(&*(inner as *const T)) }
545         } else {
546             None
547         }
548     }
549
550     /// Gets reference to the inner value, panics if it is not yet initialized
551     #[inline(always)]
552     pub fn get(&self) -> &T {
553         self.try_get().expect("value was not set")
554     }
555
556     /// Gets reference to the inner value, panics if it is not yet initialized
557     #[inline(always)]
558     pub fn borrow(&self) -> &T {
559         self.get()
560     }
561 }
562
563 #[derive(Debug)]
564 pub struct Lock<T>(InnerLock<T>);
565
566 impl<T> Lock<T> {
567     #[inline(always)]
568     pub fn new(inner: T) -> Self {
569         Lock(InnerLock::new(inner))
570     }
571
572     #[inline(always)]
573     pub fn into_inner(self) -> T {
574         self.0.into_inner()
575     }
576
577     #[inline(always)]
578     pub fn get_mut(&mut self) -> &mut T {
579         self.0.get_mut()
580     }
581
582     #[cfg(parallel_compiler)]
583     #[inline(always)]
584     pub fn try_lock(&self) -> Option<LockGuard<'_, T>> {
585         self.0.try_lock()
586     }
587
588     #[cfg(not(parallel_compiler))]
589     #[inline(always)]
590     pub fn try_lock(&self) -> Option<LockGuard<'_, T>> {
591         self.0.try_borrow_mut().ok()
592     }
593
594     #[cfg(parallel_compiler)]
595     #[inline(always)]
596     pub fn lock(&self) -> LockGuard<'_, T> {
597         if ERROR_CHECKING {
598             self.0.try_lock().expect("lock was already held")
599         } else {
600             self.0.lock()
601         }
602     }
603
604     #[cfg(not(parallel_compiler))]
605     #[inline(always)]
606     pub fn lock(&self) -> LockGuard<'_, T> {
607         self.0.borrow_mut()
608     }
609
610     #[inline(always)]
611     pub fn with_lock<F: FnOnce(&mut T) -> R, R>(&self, f: F) -> R {
612         f(&mut *self.lock())
613     }
614
615     #[inline(always)]
616     pub fn borrow(&self) -> LockGuard<'_, T> {
617         self.lock()
618     }
619
620     #[inline(always)]
621     pub fn borrow_mut(&self) -> LockGuard<'_, T> {
622         self.lock()
623     }
624 }
625
626 impl<T: Default> Default for Lock<T> {
627     #[inline]
628     fn default() -> Self {
629         Lock::new(T::default())
630     }
631 }
632
633 // FIXME: Probably a bad idea
634 impl<T: Clone> Clone for Lock<T> {
635     #[inline]
636     fn clone(&self) -> Self {
637         Lock::new(self.borrow().clone())
638     }
639 }
640
641 #[derive(Debug)]
642 pub struct RwLock<T>(InnerRwLock<T>);
643
644 impl<T> RwLock<T> {
645     #[inline(always)]
646     pub fn new(inner: T) -> Self {
647         RwLock(InnerRwLock::new(inner))
648     }
649
650     #[inline(always)]
651     pub fn into_inner(self) -> T {
652         self.0.into_inner()
653     }
654
655     #[inline(always)]
656     pub fn get_mut(&mut self) -> &mut T {
657         self.0.get_mut()
658     }
659
660     #[cfg(not(parallel_compiler))]
661     #[inline(always)]
662     pub fn read(&self) -> ReadGuard<'_, T> {
663         self.0.borrow()
664     }
665
666     #[cfg(parallel_compiler)]
667     #[inline(always)]
668     pub fn read(&self) -> ReadGuard<'_, T> {
669         if ERROR_CHECKING {
670             self.0.try_read().expect("lock was already held")
671         } else {
672             self.0.read()
673         }
674     }
675
676     #[inline(always)]
677     pub fn with_read_lock<F: FnOnce(&T) -> R, R>(&self, f: F) -> R {
678         f(&*self.read())
679     }
680
681     #[cfg(not(parallel_compiler))]
682     #[inline(always)]
683     pub fn try_write(&self) -> Result<WriteGuard<'_, T>, ()> {
684         self.0.try_borrow_mut().map_err(|_| ())
685     }
686
687     #[cfg(parallel_compiler)]
688     #[inline(always)]
689     pub fn try_write(&self) -> Result<WriteGuard<'_, T>, ()> {
690         self.0.try_write().ok_or(())
691     }
692
693     #[cfg(not(parallel_compiler))]
694     #[inline(always)]
695     pub fn write(&self) -> WriteGuard<'_, T> {
696         self.0.borrow_mut()
697     }
698
699     #[cfg(parallel_compiler)]
700     #[inline(always)]
701     pub fn write(&self) -> WriteGuard<'_, T> {
702         if ERROR_CHECKING {
703             self.0.try_write().expect("lock was already held")
704         } else {
705             self.0.write()
706         }
707     }
708
709     #[inline(always)]
710     pub fn with_write_lock<F: FnOnce(&mut T) -> R, R>(&self, f: F) -> R {
711         f(&mut *self.write())
712     }
713
714     #[inline(always)]
715     pub fn borrow(&self) -> ReadGuard<'_, T> {
716         self.read()
717     }
718
719     #[inline(always)]
720     pub fn borrow_mut(&self) -> WriteGuard<'_, T> {
721         self.write()
722     }
723 }
724
725 // FIXME: Probably a bad idea
726 impl<T: Clone> Clone for RwLock<T> {
727     #[inline]
728     fn clone(&self) -> Self {
729         RwLock::new(self.borrow().clone())
730     }
731 }
732
733 /// A type which only allows its inner value to be used in one thread.
734 /// It will panic if it is used on multiple threads.
735 #[derive(Debug)]
736 pub struct OneThread<T> {
737     #[cfg(parallel_compiler)]
738     thread: thread::ThreadId,
739     inner: T,
740 }
741
742 #[cfg(parallel_compiler)]
743 unsafe impl<T> std::marker::Sync for OneThread<T> {}
744 #[cfg(parallel_compiler)]
745 unsafe impl<T> std::marker::Send for OneThread<T> {}
746
747 impl<T> OneThread<T> {
748     #[inline(always)]
749     fn check(&self) {
750         #[cfg(parallel_compiler)]
751         assert_eq!(thread::current().id(), self.thread);
752     }
753
754     #[inline(always)]
755     pub fn new(inner: T) -> Self {
756         OneThread {
757             #[cfg(parallel_compiler)]
758             thread: thread::current().id(),
759             inner,
760         }
761     }
762
763     #[inline(always)]
764     pub fn into_inner(value: Self) -> T {
765         value.check();
766         value.inner
767     }
768 }
769
770 impl<T> Deref for OneThread<T> {
771     type Target = T;
772
773     fn deref(&self) -> &T {
774         self.check();
775         &self.inner
776     }
777 }
778
779 impl<T> DerefMut for OneThread<T> {
780     fn deref_mut(&mut self) -> &mut T {
781         self.check();
782         &mut self.inner
783     }
784 }