aboutsummaryrefslogtreecommitdiff
path: root/ctr-std/src/thread
diff options
context:
space:
mode:
Diffstat (limited to 'ctr-std/src/thread')
-rw-r--r--ctr-std/src/thread/local.rs41
-rw-r--r--ctr-std/src/thread/mod.rs3
2 files changed, 42 insertions, 2 deletions
diff --git a/ctr-std/src/thread/local.rs b/ctr-std/src/thread/local.rs
index 99479bc..40d3280 100644
--- a/ctr-std/src/thread/local.rs
+++ b/ctr-std/src/thread/local.rs
@@ -172,12 +172,16 @@ macro_rules! __thread_local_inner {
&'static $crate::cell::UnsafeCell<
$crate::option::Option<$t>>>
{
+ #[cfg(target_arch = "wasm32")]
+ static __KEY: $crate::thread::__StaticLocalKeyInner<$t> =
+ $crate::thread::__StaticLocalKeyInner::new();
+
#[thread_local]
- #[cfg(target_thread_local)]
+ #[cfg(all(target_thread_local, not(target_arch = "wasm32")))]
static __KEY: $crate::thread::__FastLocalKeyInner<$t> =
$crate::thread::__FastLocalKeyInner::new();
- #[cfg(not(target_thread_local))]
+ #[cfg(all(not(target_thread_local), not(target_arch = "wasm32")))]
static __KEY: $crate::thread::__OsLocalKeyInner<$t> =
$crate::thread::__OsLocalKeyInner::new();
@@ -295,6 +299,39 @@ impl<T: 'static> LocalKey<T> {
}
}
+/// On some platforms like wasm32 there's no threads, so no need to generate
+/// thread locals and we can instead just use plain statics!
+#[doc(hidden)]
+#[cfg(target_arch = "wasm32")]
+pub mod statik {
+ use cell::UnsafeCell;
+ use fmt;
+
+ pub struct Key<T> {
+ inner: UnsafeCell<Option<T>>,
+ }
+
+ unsafe impl<T> ::marker::Sync for Key<T> { }
+
+ impl<T> fmt::Debug for Key<T> {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ f.pad("Key { .. }")
+ }
+ }
+
+ impl<T> Key<T> {
+ pub const fn new() -> Key<T> {
+ Key {
+ inner: UnsafeCell::new(None),
+ }
+ }
+
+ pub unsafe fn get(&self) -> Option<&'static UnsafeCell<Option<T>>> {
+ Some(&*(&self.inner as *const _))
+ }
+ }
+}
+
#[doc(hidden)]
#[cfg(target_thread_local)]
pub mod fast {
diff --git a/ctr-std/src/thread/mod.rs b/ctr-std/src/thread/mod.rs
index 71aee67..1b976b7 100644
--- a/ctr-std/src/thread/mod.rs
+++ b/ctr-std/src/thread/mod.rs
@@ -203,6 +203,9 @@ pub use self::local::{LocalKey, AccessError};
// where available, but both are needed.
#[unstable(feature = "libstd_thread_internals", issue = "0")]
+#[cfg(target_arch = "wasm32")]
+#[doc(hidden)] pub use self::local::statik::Key as __StaticLocalKeyInner;
+#[unstable(feature = "libstd_thread_internals", issue = "0")]
#[cfg(target_thread_local)]
#[doc(hidden)] pub use self::local::fast::Key as __FastLocalKeyInner;
#[unstable(feature = "libstd_thread_internals", issue = "0")]