• Home
  • Raw
  • Download

Lines Matching full:thread

16 /// Thread ID manager which allocates thread IDs. It attempts to aggressively
17 /// reuse thread IDs where possible to avoid cases where a ThreadLocal grows
38 .expect("Ran out of thread IDs"); in alloc()
49 /// Data which is unique to the current thread while it is running.
50 /// A thread ID may be reused after a thread exits.
52 pub(crate) struct Thread { struct
53 /// The thread ID obtained from the thread ID manager.
55 /// The bucket this thread's local storage will be in.
57 /// The size of the bucket this thread's local storage will be in.
59 /// The index into the bucket this thread's local storage is in.
62 impl Thread { argument
63 fn new(id: usize) -> Thread { in new() argument
68 Thread { in new()
79 // This is split into 2 thread-local variables so that we can check whether the
80 // thread is initialized without having to register a thread-local destructor.
84 static mut THREAD: Option<Thread> = None; constant
87 // Guard to ensure the thread ID is released on thread exit.
89 // We keep a copy of the thread ID in the ThreadGuard: we can't
90 // reliably access THREAD in our Drop impl due to the unpredictable
97 // Release the thread ID. Any further accesses to the thread ID
101 THREAD = None;
107 /// Returns a thread ID for the current thread, allocating one if needed.
109 pub(crate) fn get() -> Thread {
110 if let Some(thread) = unsafe { THREAD } {
111 thread
117 /// Out-of-line slow path for allocating a thread ID.
119 fn get_slow() -> Thread {
120 let new = Thread::new(THREAD_ID_MANAGER.lock().unwrap().alloc());
122 THREAD = Some(new);
128 // This is split into 2 thread-local variables so that we can check whether the
129 // thread is initialized without having to register a thread-local destructor.
132 thread_local! { static THREAD: Cell<Option<Thread>> = const { Cell::new(None) }; }
135 // Guard to ensure the thread ID is released on thread exit.
137 // We keep a copy of the thread ID in the ThreadGuard: we can't
138 // reliably access THREAD in our Drop impl due to the unpredictable
145 // Release the thread ID. Any further accesses to the thread ID
148 let _ = THREAD.try_with(|thread| thread.set(None));
153 /// Returns a thread ID for the current thread, allocating one if needed.
155 pub(crate) fn get() -> Thread {
156 THREAD.with(|thread| {
157 if let Some(thread) = thread.get() {
158 thread
160 get_slow(thread)
165 /// Out-of-line slow path for allocating a thread ID.
167 fn get_slow(thread: &Cell<Option<Thread>>) -> Thread {
168 let new = Thread::new(THREAD_ID_MANAGER.lock().unwrap().alloc());
169 thread.set(Some(new));
178 let thread = Thread::new(0); in test_thread() localVariable
179 assert_eq!(thread.id, 0); in test_thread()
180 assert_eq!(thread.bucket, 0); in test_thread()
181 assert_eq!(thread.bucket_size, 1); in test_thread()
182 assert_eq!(thread.index, 0); in test_thread()
184 let thread = Thread::new(1); in test_thread() localVariable
185 assert_eq!(thread.id, 1); in test_thread()
186 assert_eq!(thread.bucket, 1); in test_thread()
187 assert_eq!(thread.bucket_size, 1); in test_thread()
188 assert_eq!(thread.index, 0); in test_thread()
190 let thread = Thread::new(2); in test_thread() localVariable
191 assert_eq!(thread.id, 2); in test_thread()
192 assert_eq!(thread.bucket, 2); in test_thread()
193 assert_eq!(thread.bucket_size, 2); in test_thread()
194 assert_eq!(thread.index, 0); in test_thread()
196 let thread = Thread::new(3); in test_thread() localVariable
197 assert_eq!(thread.id, 3); in test_thread()
198 assert_eq!(thread.bucket, 2); in test_thread()
199 assert_eq!(thread.bucket_size, 2); in test_thread()
200 assert_eq!(thread.index, 1); in test_thread()
202 let thread = Thread::new(19); in test_thread() localVariable
203 assert_eq!(thread.id, 19); in test_thread()
204 assert_eq!(thread.bucket, 5); in test_thread()
205 assert_eq!(thread.bucket_size, 16); in test_thread()
206 assert_eq!(thread.index, 3); in test_thread()