avoid calling thread::current in channel destructor

This commit is contained in:
Ibraheem Ahmed 2022-11-12 23:13:58 -05:00
parent cd30f2751e
commit a22426916d
2 changed files with 11 additions and 13 deletions

View File

@ -1,12 +1,13 @@
//! Thread-local channel context.
use super::select::Selected;
use super::waker::current_thread_id;
use crate::cell::Cell;
use crate::ptr;
use crate::sync::atomic::{AtomicPtr, AtomicUsize, Ordering};
use crate::sync::Arc;
use crate::thread::{self, Thread, ThreadId};
use crate::thread::{self, Thread};
use crate::time::Instant;
/// Thread-local context.
@ -28,7 +29,7 @@ struct Inner {
thread: Thread,
/// Thread id.
thread_id: ThreadId,
thread_id: usize,
}
impl Context {
@ -70,7 +71,7 @@ impl Context {
select: AtomicUsize::new(Selected::Waiting.into()),
packet: AtomicPtr::new(ptr::null_mut()),
thread: thread::current(),
thread_id: thread::current().id(),
thread_id: current_thread_id(),
}),
}
}
@ -148,7 +149,7 @@ impl Context {
/// Returns the id of the thread this context belongs to.
#[inline]
pub fn thread_id(&self) -> ThreadId {
pub fn thread_id(&self) -> usize {
self.inner.thread_id
}
}

View File

@ -6,7 +6,6 @@ use super::select::{Operation, Selected};
use crate::ptr;
use crate::sync::atomic::{AtomicBool, Ordering};
use crate::sync::Mutex;
use crate::thread::{self, ThreadId};
/// Represents a thread blocked on a specific channel operation.
pub(crate) struct Entry {
@ -195,13 +194,11 @@ impl Drop for SyncWaker {
}
}
/// Returns the id of the current thread.
/// Returns a unique id for the current thread.
#[inline]
fn current_thread_id() -> ThreadId {
thread_local! {
/// Cached thread-local id.
static THREAD_ID: ThreadId = thread::current().id();
}
THREAD_ID.try_with(|id| *id).unwrap_or_else(|_| thread::current().id())
pub fn current_thread_id() -> usize {
// `u8` is not drop so this variable will be available during thread destruction,
// whereas `thread::current()` would not be
thread_local! { static DUMMY: u8 = 0 }
DUMMY.with(|x| (x as *const u8).addr())
}