Skip to content

Commit b45a736

Browse files
committed
Remove incorrect thread_id::try_get function
This was introduced in #44 but is actually broken since a thread could be assigned the ID of an existing slot in the table if another thread previously using that ID has exited.
1 parent 63c4c62 commit b45a736

File tree

2 files changed

+27
-32
lines changed

2 files changed

+27
-32
lines changed

src/lib.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ impl<T: Send> ThreadLocal<T> {
190190

191191
/// Returns the element for the current thread, if it exists.
192192
pub fn get(&self) -> Option<&T> {
193-
thread_id::try_get().and_then(|thread| self.get_inner(thread))
193+
self.get_inner(thread_id::get())
194194
}
195195

196196
/// Returns the element for the current thread, or creates it if it doesn't
@@ -212,12 +212,11 @@ impl<T: Send> ThreadLocal<T> {
212212
where
213213
F: FnOnce() -> Result<T, E>,
214214
{
215-
let thread = thread_id::try_get();
216-
if let Some(thread) = thread {
217-
if let Some(val) = self.get_inner(thread) {
218-
return Ok(val);
219-
}
215+
let thread = thread_id::get();
216+
if let Some(val) = self.get_inner(thread) {
217+
return Ok(val);
220218
}
219+
221220
Ok(self.insert(create()?))
222221
}
223222

src/thread_id.rs

+22-26
Original file line numberDiff line numberDiff line change
@@ -97,29 +97,26 @@ cfg_if::cfg_if! {
9797
}
9898
}
9999

100-
/// Attempts to get the current thread if `get` has previously been
101-
/// called.
102-
#[inline]
103-
pub(crate) fn try_get() -> Option<Thread> {
104-
unsafe {
105-
THREAD
106-
}
107-
}
108-
109100
/// Returns a thread ID for the current thread, allocating one if needed.
110101
#[inline]
111102
pub(crate) fn get() -> Thread {
112103
if let Some(thread) = unsafe { THREAD } {
113104
thread
114105
} else {
115-
let new = Thread::new(THREAD_ID_MANAGER.lock().unwrap().alloc());
116-
unsafe {
117-
THREAD = Some(new);
118-
}
119-
THREAD_GUARD.with(|_| {});
120-
new
106+
get_slow()
121107
}
122108
}
109+
110+
/// Out-of-line slow path for allocating a thread ID.
111+
#[cold]
112+
fn get_slow() -> Thread {
113+
let new = Thread::new(THREAD_ID_MANAGER.lock().unwrap().alloc());
114+
unsafe {
115+
THREAD = Some(new);
116+
}
117+
THREAD_GUARD.with(|_| {});
118+
new
119+
}
123120
} else {
124121
use std::cell::Cell;
125122

@@ -140,27 +137,26 @@ cfg_if::cfg_if! {
140137
}
141138
}
142139

143-
/// Attempts to get the current thread if `get` has previously been
144-
/// called.
145-
#[inline]
146-
pub(crate) fn try_get() -> Option<Thread> {
147-
THREAD.with(|thread| thread.get())
148-
}
149-
150140
/// Returns a thread ID for the current thread, allocating one if needed.
151141
#[inline]
152142
pub(crate) fn get() -> Thread {
153143
THREAD.with(|thread| {
154144
if let Some(thread) = thread.get() {
155145
thread
156146
} else {
157-
let new = Thread::new(THREAD_ID_MANAGER.lock().unwrap().alloc());
158-
thread.set(Some(new));
159-
THREAD_GUARD.with(|_| {});
160-
new
147+
get_slow(thread)
161148
}
162149
})
163150
}
151+
152+
/// Out-of-line slow path for allocating a thread ID.
153+
#[cold]
154+
fn get_slow(thread: &Cell<Option<Thread>>) -> Thread {
155+
let new = Thread::new(THREAD_ID_MANAGER.lock().unwrap().alloc());
156+
thread.set(Some(new));
157+
THREAD_GUARD.with(|_| {});
158+
new
159+
}
164160
}
165161
}
166162

0 commit comments

Comments
 (0)