David Tolnay | 3287480 | 2018-11-11 08:52:19 -0800 | [diff] [blame] | 1 | use std::fmt::{self, Debug}; |
| 2 | |
David Tolnay | 8865861 | 2018-11-11 09:52:11 -0800 | [diff] [blame] | 3 | use self::thread_id::ThreadId; |
David Tolnay | 3287480 | 2018-11-11 08:52:19 -0800 | [diff] [blame] | 4 | |
David Tolnay | 8865861 | 2018-11-11 09:52:11 -0800 | [diff] [blame] | 5 | /// ThreadBound is a Sync-maker and Send-maker that allows accessing a value |
| 6 | /// of type T only from the original thread on which the ThreadBound was |
| 7 | /// constructed. |
| 8 | pub struct ThreadBound<T> { |
| 9 | value: T, |
| 10 | thread_id: ThreadId, |
David Tolnay | 3287480 | 2018-11-11 08:52:19 -0800 | [diff] [blame] | 11 | } |
| 12 | |
David Tolnay | 8865861 | 2018-11-11 09:52:11 -0800 | [diff] [blame] | 13 | unsafe impl<T> Sync for ThreadBound<T> {} |
| 14 | |
| 15 | // Send bound requires Copy, as otherwise Drop could run in the wrong place. |
| 16 | unsafe impl<T: Copy> Send for ThreadBound<T> {} |
| 17 | |
| 18 | impl<T> ThreadBound<T> { |
| 19 | pub fn new(value: T) -> Self { |
| 20 | ThreadBound { |
| 21 | value: value, |
| 22 | thread_id: thread_id::current(), |
| 23 | } |
David Tolnay | 3287480 | 2018-11-11 08:52:19 -0800 | [diff] [blame] | 24 | } |
| 25 | |
David Tolnay | 8865861 | 2018-11-11 09:52:11 -0800 | [diff] [blame] | 26 | pub fn get(&self) -> Option<&T> { |
| 27 | if thread_id::current() == self.thread_id { |
David Tolnay | 3287480 | 2018-11-11 08:52:19 -0800 | [diff] [blame] | 28 | Some(&self.value) |
David Tolnay | 8865861 | 2018-11-11 09:52:11 -0800 | [diff] [blame] | 29 | } else { |
| 30 | None |
David Tolnay | 3287480 | 2018-11-11 08:52:19 -0800 | [diff] [blame] | 31 | } |
| 32 | } |
| 33 | } |
| 34 | |
David Tolnay | 3287480 | 2018-11-11 08:52:19 -0800 | [diff] [blame] | 35 | impl<T: Debug> Debug for ThreadBound<T> { |
| 36 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 37 | match self.get() { |
| 38 | Some(value) => Debug::fmt(value, formatter), |
| 39 | None => formatter.write_str("unknown"), |
| 40 | } |
| 41 | } |
| 42 | } |
David Tolnay | 8865861 | 2018-11-11 09:52:11 -0800 | [diff] [blame] | 43 | |
| 44 | #[cfg(syn_can_use_thread_id)] |
| 45 | mod thread_id { |
| 46 | use std::thread; |
| 47 | |
| 48 | pub use std::thread::ThreadId; |
| 49 | |
| 50 | pub fn current() -> ThreadId { |
| 51 | thread::current().id() |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | #[cfg(not(syn_can_use_thread_id))] |
| 56 | mod thread_id { |
David Tolnay | e41c441 | 2019-02-28 23:25:18 -0800 | [diff] [blame] | 57 | #[allow(deprecated)] |
David Tolnay | 8865861 | 2018-11-11 09:52:11 -0800 | [diff] [blame] | 58 | use std::sync::atomic::{AtomicUsize, Ordering, ATOMIC_USIZE_INIT}; |
| 59 | |
| 60 | thread_local! { |
| 61 | static THREAD_ID: usize = { |
David Tolnay | e41c441 | 2019-02-28 23:25:18 -0800 | [diff] [blame] | 62 | #[allow(deprecated)] |
David Tolnay | 8865861 | 2018-11-11 09:52:11 -0800 | [diff] [blame] | 63 | static NEXT_THREAD_ID: AtomicUsize = ATOMIC_USIZE_INIT; |
David Tolnay | d1f57cd | 2018-11-11 10:28:50 -0800 | [diff] [blame] | 64 | |
| 65 | // Ordering::Relaxed because our only requirement for the ids is |
| 66 | // that they are unique. It is okay for the compiler to rearrange |
| 67 | // other memory reads around this fetch. It's still an atomic |
| 68 | // fetch_add, so no two threads will be able to read the same value |
| 69 | // from it. |
| 70 | // |
| 71 | // The main thing which these orderings affect is other memory reads |
| 72 | // around the atomic read, which for our case are irrelevant as this |
| 73 | // atomic guards nothing. |
David Tolnay | 8865861 | 2018-11-11 09:52:11 -0800 | [diff] [blame] | 74 | NEXT_THREAD_ID.fetch_add(1, Ordering::Relaxed) |
| 75 | }; |
| 76 | } |
| 77 | |
| 78 | pub type ThreadId = usize; |
| 79 | |
| 80 | pub fn current() -> ThreadId { |
| 81 | THREAD_ID.with(|id| *id) |
| 82 | } |
| 83 | } |