Initial version
This commit is contained in:
1 parent
2cff35f215
commit
fe8e2cb5e7
360 files changed
+253989
-74
No files matched your search
@@ -0,0 +1,137 @@
|
||||
|
||||
|
||||
macro_rules! hot_local {
|
||||
() => {};
|
||||
($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = const { $init:expr }; $($rest:tt)*) => {
|
||||
$crate::hot_local::hot_local!(@const $(#[$attr])* $vis $name, $t, $init);
|
||||
$crate::hot_local::hot_local!($($rest)*);
|
||||
};
|
||||
($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = const { $init:expr }) => {
|
||||
$crate::hot_local::hot_local!(@const $(#[$attr])* $vis $name, $t, $init);
|
||||
};
|
||||
($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = $init:expr; $($rest:tt)*) => {
|
||||
$crate::hot_local::hot_local!(@lazy $(#[$attr])* $vis $name, $t, $init);
|
||||
$crate::hot_local::hot_local!($($rest)*);
|
||||
};
|
||||
($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = $init:expr) => {
|
||||
$crate::hot_local::hot_local!(@lazy $(#[$attr])* $vis $name, $t, $init);
|
||||
};
|
||||
(@const $(#[$attr:meta])* $vis:vis $name:ident, $t:ty, $init:expr) => {
|
||||
#[cfg(not(all(windows, target_env = "gnu")))]
|
||||
::std::thread_local!($(#[$attr])* $vis static $name: $t = const { $init });
|
||||
$crate::hot_local::hot_local!(@fast $(#[$attr])* $vis $name, $t, $init);
|
||||
};
|
||||
(@lazy $(#[$attr:meta])* $vis:vis $name:ident, $t:ty, $init:expr) => {
|
||||
#[cfg(not(all(windows, target_env = "gnu")))]
|
||||
::std::thread_local!($(#[$attr])* $vis static $name: $t = $init);
|
||||
$crate::hot_local::hot_local!(@fast $(#[$attr])* $vis $name, $t, $init);
|
||||
};
|
||||
(@fast $(#[$attr:meta])* $vis:vis $name:ident, $t:ty, $init:expr) => {
|
||||
#[cfg(all(windows, target_env = "gnu"))]
|
||||
$(#[$attr])*
|
||||
$vis static $name: $crate::hot_local::FastLocal<$t> = {
|
||||
fn init() -> $t {
|
||||
$init
|
||||
}
|
||||
$crate::hot_local::FastLocal::new(init)
|
||||
};
|
||||
};
|
||||
}
|
||||
pub(crate) use hot_local;
|
||||
|
||||
#[cfg(windows)]
|
||||
#[cfg_attr(not(target_env = "gnu"), allow(dead_code))]
|
||||
pub(crate) struct FastLocal<T: 'static> {
|
||||
|
||||
index: std::sync::atomic::AtomicU32,
|
||||
init: fn() -> T,
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
unsafe impl<T: 'static> Sync for FastLocal<T> {}
|
||||
|
||||
#[cfg(windows)]
|
||||
#[cfg_attr(not(target_env = "gnu"), allow(dead_code))]
|
||||
impl<T: 'static> FastLocal<T> {
|
||||
pub(crate) const fn new(init: fn() -> T) -> Self {
|
||||
Self { index: std::sync::atomic::AtomicU32::new(0), init }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn index(&self) -> u32 {
|
||||
match self.index.load(std::sync::atomic::Ordering::Acquire) {
|
||||
0 => self.allocate(),
|
||||
n => n - 1,
|
||||
}
|
||||
}
|
||||
|
||||
#[cold]
|
||||
fn allocate(&self) -> u32 {
|
||||
use std::sync::atomic::Ordering::{AcqRel, Acquire};
|
||||
use windows_sys::Win32::System::Threading::{TLS_OUT_OF_INDEXES, TlsAlloc, TlsFree};
|
||||
|
||||
let index = unsafe { TlsAlloc() };
|
||||
assert_ne!(index, TLS_OUT_OF_INDEXES, "no TLS index left for a thread-local");
|
||||
match self.index.compare_exchange(0, index + 1, AcqRel, Acquire) {
|
||||
Ok(_) => index,
|
||||
Err(won) => {
|
||||
|
||||
unsafe { TlsFree(index) };
|
||||
won - 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn with<R>(&'static self, f: impl FnOnce(&T) -> R) -> R {
|
||||
use windows_sys::Win32::System::Threading::{TlsGetValue, TlsSetValue};
|
||||
let index = self.index();
|
||||
|
||||
let mut value = unsafe { TlsGetValue(index) }.cast::<T>().cast_const();
|
||||
if value.is_null() {
|
||||
let made = Box::into_raw(Box::new((self.init)()));
|
||||
|
||||
unsafe { TlsSetValue(index, made.cast()) };
|
||||
value = made;
|
||||
}
|
||||
|
||||
f(unsafe { &*value })
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
#[cfg_attr(not(target_env = "gnu"), allow(dead_code))]
|
||||
impl<T: Copy + 'static> FastLocal<std::cell::Cell<T>> {
|
||||
pub(crate) fn get(&'static self) -> T {
|
||||
self.with(std::cell::Cell::get)
|
||||
}
|
||||
pub(crate) fn set(&'static self, value: T) {
|
||||
self.with(|c| c.set(value));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
#[cfg_attr(not(target_env = "gnu"), allow(dead_code))]
|
||||
impl<T: 'static> FastLocal<std::cell::Cell<T>> {
|
||||
pub(crate) fn replace(&'static self, value: T) -> T {
|
||||
self.with(|c| c.replace(value))
|
||||
}
|
||||
pub(crate) fn take(&'static self) -> T
|
||||
where
|
||||
T: Default,
|
||||
{
|
||||
self.with(std::cell::Cell::take)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
#[cfg_attr(not(target_env = "gnu"), allow(dead_code))]
|
||||
impl<T: 'static> FastLocal<std::cell::RefCell<T>> {
|
||||
pub(crate) fn with_borrow<R>(&'static self, f: impl FnOnce(&T) -> R) -> R {
|
||||
self.with(|c| f(&c.borrow()))
|
||||
}
|
||||
pub(crate) fn with_borrow_mut<R>(&'static self, f: impl FnOnce(&mut T) -> R) -> R {
|
||||
self.with(|c| f(&mut c.borrow_mut()))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user