1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
pub use abi_stable as macro_support;
use abi_stable::std_types::{RBox, RStr};
pub struct LocalKey<T: 'static> {
#[doc(hidden)]
pub id: RStr<'static>,
#[doc(hidden)]
pub init: extern "C" fn() -> RBox<()>,
#[doc(hidden)]
pub __phantom: std::marker::PhantomData<extern "C" fn() -> T>,
}
impl<T: 'static> LocalKey<T> {
#[doc(hidden)]
pub fn new(id: RStr<'static>, init: extern "C" fn() -> RBox<()>) -> Self {
LocalKey {
id,
init,
__phantom: std::marker::PhantomData,
}
}
}
#[macro_export]
macro_rules! thread_local {
() => {};
($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = $init:expr; $($rest:tt)*) => {
$crate::__thread_local_inner!($(#[$attr])* $vis $name: $t = $init);
$crate::thread_local!($($rest)*);
};
($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = $init:expr) => {
$crate::__thread_local_inner!($(#[$attr])* $vis $name: $t = $init);
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! __thread_local_inner {
($(#[$attr:meta])* $vis:vis $name:ident: $t:ty = $init:expr) => {
$(#[$attr])*
$vis static $name: $crate::LocalKey<$t> = {
use $crate::macro_support::{pointer_trait::TransmuteElement, std_types::{RBox, RStr}};
extern "C" fn __init() -> RBox<()> {
let __val: $t = $init;
unsafe { RBox::new(__val).transmute_element() }
}
$crate::LocalKey {
id: RStr::from_str(std::concat!(std::stringify!($name), std::stringify!($t), std::module_path!())),
init: __init,
__phantom: std::marker::PhantomData
}
};
}
}
#[cfg(feature = "host")]
mod host {
use abi_stable::std_types::{RBox, RStr};
use std::cell::RefCell;
use std::collections::BTreeMap as Map;
std::thread_local! {
static PLUGIN_TLS: RefCell<Map<RStr<'static>, RBox<()>>> = RefCell::new(Default::default());
}
pub unsafe extern "C" fn tls(
id: &RStr<'static>,
init: extern "C" fn() -> RBox<()>,
) -> *const () {
PLUGIN_TLS.with(|m| {
let mut m = m.borrow_mut();
if !m.contains_key(id) {
m.insert(id.clone(), init());
};
m.get(id).unwrap().as_ref() as *const ()
})
}
pub fn reset() {
PLUGIN_TLS.with(|m| m.borrow_mut().clear())
}
}
type TlsFunction = unsafe extern "C" fn(&RStr<'static>, extern "C" fn() -> RBox<()>) -> *const ();
#[repr(transparent)]
pub struct Context(TlsFunction);
impl Context {
pub unsafe fn initialize_tls(self) {
HOST_TLS = Some(self.0);
}
}
#[cfg(feature = "host")]
impl Context {
pub fn get() -> Self {
Context(host::tls)
}
pub fn reset() {
host::reset();
}
}
#[cfg(feature = "host")]
static mut HOST_TLS: Option<TlsFunction> = Some(host::tls);
#[cfg(not(feature = "host"))]
static mut HOST_TLS: Option<TlsFunction> = None;
impl<T: 'static> LocalKey<T> {
#[cfg(any(feature = "host", feature = "plugin"))]
pub fn with<F, R>(&'static self, f: F) -> R
where
F: FnOnce(&T) -> R,
{
let host_tls =
unsafe { HOST_TLS.as_ref() }.expect("host thread local storage improperly initialized");
f(unsafe {
(host_tls(&self.id, self.init) as *const T)
.as_ref()
.unwrap()
})
}
#[cfg(not(any(feature = "host", feature = "plugin")))]
pub fn with<F, R>(&'static self, _f: F) -> R
where
F: FnOnce(&T) -> R,
{
panic!("plugin_tls built without 'host' or 'plugin' enabled")
}
}