1// Copyright 2017 Amanieu d'Antras
2//
3// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5// http://opensource.org/licenses/MIT>, at your option. This file may not be
6// copied, modified, or distributed except according to those terms.
78use crate::POINTER_WIDTH;
9use once_cell::sync::Lazy;
10use std::cmp::Reverse;
11use std::collections::BinaryHeap;
12use std::sync::Mutex;
13use std::usize;
1415/// Thread ID manager which allocates thread IDs. It attempts to aggressively
16/// reuse thread IDs where possible to avoid cases where a ThreadLocal grows
17/// indefinitely when it is used by many short-lived threads.
18struct ThreadIdManager {
19 free_from: usize,
20 free_list: BinaryHeap<Reverse<usize>>,
21}
22impl ThreadIdManager {
23fn new() -> ThreadIdManager {
24 ThreadIdManager {
25 free_from: 0,
26 free_list: BinaryHeap::new(),
27 }
28 }
29fn alloc(&mut self) -> usize {
30if let Some(id) = self.free_list.pop() {
31 id.0
32} else {
33let id = self.free_from;
34self.free_from = self
35.free_from
36 .checked_add(1)
37 .expect("Ran out of thread IDs");
38 id
39 }
40 }
41fn free(&mut self, id: usize) {
42self.free_list.push(Reverse(id));
43 }
44}
45static THREAD_ID_MANAGER: Lazy<Mutex<ThreadIdManager>> =
46 Lazy::new(|| Mutex::new(ThreadIdManager::new()));
4748/// Data which is unique to the current thread while it is running.
49/// A thread ID may be reused after a thread exits.
50#[derive(Clone, Copy)]
51pub(crate) struct Thread {
52/// The thread ID obtained from the thread ID manager.
53pub(crate) id: usize,
54/// The bucket this thread's local storage will be in.
55pub(crate) bucket: usize,
56/// The size of the bucket this thread's local storage will be in.
57pub(crate) bucket_size: usize,
58/// The index into the bucket this thread's local storage is in.
59pub(crate) index: usize,
60}
61impl Thread {
62fn new(id: usize) -> Thread {
63let bucket = usize::from(POINTER_WIDTH) - id.leading_zeros() as usize;
64let bucket_size = 1 << bucket.saturating_sub(1);
65let index = if id != 0 { id ^ bucket_size } else { 0 };
6667 Thread {
68 id,
69 bucket,
70 bucket_size,
71 index,
72 }
73 }
74}
7576/// Wrapper around `Thread` that allocates and deallocates the ID.
77struct ThreadHolder(Thread);
78impl ThreadHolder {
79fn new() -> ThreadHolder {
80 ThreadHolder(Thread::new(THREAD_ID_MANAGER.lock().unwrap().alloc()))
81 }
82}
83impl Drop for ThreadHolder {
84fn drop(&mut self) {
85 THREAD_ID_MANAGER.lock().unwrap().free(self.0.id);
86 }
87}
8889thread_local!(static THREAD_HOLDER: ThreadHolder = ThreadHolder::new());
9091/// Get the current thread.
92pub(crate) fn get() -> Thread {
93 THREAD_HOLDER.with(|holder| holder.0)
94}
9596#[test]
97fn test_thread() {
98let thread = Thread::new(0);
99assert_eq!(thread.id, 0);
100assert_eq!(thread.bucket, 0);
101assert_eq!(thread.bucket_size, 1);
102assert_eq!(thread.index, 0);
103104let thread = Thread::new(1);
105assert_eq!(thread.id, 1);
106assert_eq!(thread.bucket, 1);
107assert_eq!(thread.bucket_size, 1);
108assert_eq!(thread.index, 0);
109110let thread = Thread::new(2);
111assert_eq!(thread.id, 2);
112assert_eq!(thread.bucket, 2);
113assert_eq!(thread.bucket_size, 2);
114assert_eq!(thread.index, 0);
115116let thread = Thread::new(3);
117assert_eq!(thread.id, 3);
118assert_eq!(thread.bucket, 2);
119assert_eq!(thread.bucket_size, 2);
120assert_eq!(thread.index, 1);
121122let thread = Thread::new(19);
123assert_eq!(thread.id, 19);
124assert_eq!(thread.bucket, 5);
125assert_eq!(thread.bucket_size, 16);
126assert_eq!(thread.index, 3);
127}