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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
#![allow(clippy::unit_arg)]
use std::cmp;
use std::fmt;
use std::marker::PhantomData;
use std::mem;
use std::num::NonZeroUsize;
use crate::errors::InvalidThreadAccess;
use crate::registry;
use crate::thread_id;
use crate::StackToken;
/// A [`Sticky<T>`] keeps a value T stored in a thread.
///
/// This type works similar in nature to [`Fragile`](crate::Fragile) and exposes a
/// similar interface. The difference is that whereas [`Fragile`](crate::Fragile) has
/// its destructor called in the thread where the value was sent, a
/// [`Sticky`] that is moved to another thread will have the internal
/// destructor called when the originating thread tears down.
///
/// Because [`Sticky`] allows values to be kept alive for longer than the
/// [`Sticky`] itself, it requires all its contents to be `'static` for
/// soundness. More importantly it also requires the use of [`StackToken`]s.
/// For information about how to use stack tokens and why they are neded,
/// refer to [`stack_token!`](crate::stack_token).
///
/// As this uses TLS internally the general rules about the platform limitations
/// of destructors for TLS apply.
pub struct Sticky<T: 'static> {
item_id: registry::ItemId,
thread_id: NonZeroUsize,
_marker: PhantomData<*mut T>,
}
impl<T> Drop for Sticky<T> {
fn drop(&mut self) {
// if the type needs dropping we can only do so on the
// right thread. worst case we leak the value until the
// thread dies.
if mem::needs_drop::<T>() {
unsafe {
if self.is_valid() {
self.unsafe_take_value();
}
}
// otherwise we take the liberty to drop the value
// right here and now. We can however only do that if
// we are on the right thread. If we are not, we again
// need to wait for the thread to shut down.
} else if let Some(entry) = registry::try_remove(self.item_id, self.thread_id) {
unsafe {
(entry.drop)(entry.ptr);
}
}
}
}
impl<T> Sticky<T> {
/// Creates a new [`Sticky`] wrapping a `value`.
///
/// The value that is moved into the [`Sticky`] can be non `Send` and
/// will be anchored to the thread that created the object. If the
/// sticky wrapper type ends up being send from thread to thread
/// only the original thread can interact with the value.
pub fn new(value: T) -> Self {
let entry = registry::Entry {
ptr: Box::into_raw(Box::new(value)).cast(),
drop: |ptr| {
let ptr = ptr.cast::<T>();
// SAFETY: This callback will only be called once, with the
// above pointer.
drop(unsafe { Box::from_raw(ptr) });
},
};
let thread_id = thread_id::get();
let item_id = registry::insert(thread_id, entry);
Sticky {
item_id,
thread_id,
_marker: PhantomData,
}
}
#[inline(always)]
fn with_value<F: FnOnce(*mut T) -> R, R>(&self, f: F) -> R {
self.assert_thread();
registry::with(self.item_id, self.thread_id, |entry| {
f(entry.ptr.cast::<T>())
})
}
/// Returns `true` if the access is valid.
///
/// This will be `false` if the value was sent to another thread.
#[inline(always)]
pub fn is_valid(&self) -> bool {
thread_id::get() == self.thread_id
}
#[inline(always)]
fn assert_thread(&self) {
if !self.is_valid() {
panic!("trying to access wrapped value in sticky container from incorrect thread.");
}
}
/// Consumes the `Sticky`, returning the wrapped value.
///
/// # Panics
///
/// Panics if called from a different thread than the one where the
/// original value was created.
pub fn into_inner(mut self) -> T {
self.assert_thread();
unsafe {
let rv = self.unsafe_take_value();
mem::forget(self);
rv
}
}
unsafe fn unsafe_take_value(&mut self) -> T {
let ptr = registry::remove(self.item_id, self.thread_id)
.ptr
.cast::<T>();
*Box::from_raw(ptr)
}
/// Consumes the `Sticky`, returning the wrapped value if successful.
///
/// The wrapped value is returned if this is called from the same thread
/// as the one where the original value was created, otherwise the
/// `Sticky` is returned as `Err(self)`.
pub fn try_into_inner(self) -> Result<T, Self> {
if self.is_valid() {
Ok(self.into_inner())
} else {
Err(self)
}
}
/// Immutably borrows the wrapped value.
///
/// # Panics
///
/// Panics if the calling thread is not the one that wrapped the value.
/// For a non-panicking variant, use [`try_get`](#method.try_get`).
pub fn get<'stack>(&'stack self, _proof: &'stack StackToken) -> &'stack T {
self.with_value(|value| unsafe { &*value })
}
/// Mutably borrows the wrapped value.
///
/// # Panics
///
/// Panics if the calling thread is not the one that wrapped the value.
/// For a non-panicking variant, use [`try_get_mut`](#method.try_get_mut`).
pub fn get_mut<'stack>(&'stack mut self, _proof: &'stack StackToken) -> &'stack mut T {
self.with_value(|value| unsafe { &mut *value })
}
/// Tries to immutably borrow the wrapped value.
///
/// Returns `None` if the calling thread is not the one that wrapped the value.
pub fn try_get<'stack>(
&'stack self,
_proof: &'stack StackToken,
) -> Result<&'stack T, InvalidThreadAccess> {
if self.is_valid() {
Ok(self.with_value(|value| unsafe { &*value }))
} else {
Err(InvalidThreadAccess)
}
}
/// Tries to mutably borrow the wrapped value.
///
/// Returns `None` if the calling thread is not the one that wrapped the value.
pub fn try_get_mut<'stack>(
&'stack mut self,
_proof: &'stack StackToken,
) -> Result<&'stack mut T, InvalidThreadAccess> {
if self.is_valid() {
Ok(self.with_value(|value| unsafe { &mut *value }))
} else {
Err(InvalidThreadAccess)
}
}
}
impl<T> From<T> for Sticky<T> {
#[inline]
fn from(t: T) -> Sticky<T> {
Sticky::new(t)
}
}
impl<T: Clone> Clone for Sticky<T> {
#[inline]
fn clone(&self) -> Sticky<T> {
crate::stack_token!(tok);
Sticky::new(self.get(tok).clone())
}
}
impl<T: Default> Default for Sticky<T> {
#[inline]
fn default() -> Sticky<T> {
Sticky::new(T::default())
}
}
impl<T: PartialEq> PartialEq for Sticky<T> {
#[inline]
fn eq(&self, other: &Sticky<T>) -> bool {
crate::stack_token!(tok);
*self.get(tok) == *other.get(tok)
}
}
impl<T: Eq> Eq for Sticky<T> {}
impl<T: PartialOrd> PartialOrd for Sticky<T> {
#[inline]
fn partial_cmp(&self, other: &Sticky<T>) -> Option<cmp::Ordering> {
crate::stack_token!(tok);
self.get(tok).partial_cmp(other.get(tok))
}
#[inline]
fn lt(&self, other: &Sticky<T>) -> bool {
crate::stack_token!(tok);
*self.get(tok) < *other.get(tok)
}
#[inline]
fn le(&self, other: &Sticky<T>) -> bool {
crate::stack_token!(tok);
*self.get(tok) <= *other.get(tok)
}
#[inline]
fn gt(&self, other: &Sticky<T>) -> bool {
crate::stack_token!(tok);
*self.get(tok) > *other.get(tok)
}
#[inline]
fn ge(&self, other: &Sticky<T>) -> bool {
crate::stack_token!(tok);
*self.get(tok) >= *other.get(tok)
}
}
impl<T: Ord> Ord for Sticky<T> {
#[inline]
fn cmp(&self, other: &Sticky<T>) -> cmp::Ordering {
crate::stack_token!(tok);
self.get(tok).cmp(other.get(tok))
}
}
impl<T: fmt::Display> fmt::Display for Sticky<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
crate::stack_token!(tok);
fmt::Display::fmt(self.get(tok), f)
}
}
impl<T: fmt::Debug> fmt::Debug for Sticky<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
crate::stack_token!(tok);
match self.try_get(tok) {
Ok(value) => f.debug_struct("Sticky").field("value", value).finish(),
Err(..) => {
struct InvalidPlaceholder;
impl fmt::Debug for InvalidPlaceholder {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("<invalid thread>")
}
}
f.debug_struct("Sticky")
.field("value", &InvalidPlaceholder)
.finish()
}
}
}
}
// similar as for fragile ths type is sync because it only accesses TLS data
// which is thread local. There is nothing that needs to be synchronized.
unsafe impl<T> Sync for Sticky<T> {}
// The entire point of this type is to be Send
unsafe impl<T> Send for Sticky<T> {}
#[test]
fn test_basic() {
use std::thread;
let val = Sticky::new(true);
crate::stack_token!(tok);
assert_eq!(val.to_string(), "true");
assert_eq!(val.get(tok), &true);
assert!(val.try_get(tok).is_ok());
thread::spawn(move || {
crate::stack_token!(tok);
assert!(val.try_get(tok).is_err());
})
.join()
.unwrap();
}
#[test]
fn test_mut() {
let mut val = Sticky::new(true);
crate::stack_token!(tok);
*val.get_mut(tok) = false;
assert_eq!(val.to_string(), "false");
assert_eq!(val.get(tok), &false);
}
#[test]
#[should_panic]
fn test_access_other_thread() {
use std::thread;
let val = Sticky::new(true);
thread::spawn(move || {
crate::stack_token!(tok);
val.get(tok);
})
.join()
.unwrap();
}
#[test]
fn test_drop_same_thread() {
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
let was_called = Arc::new(AtomicBool::new(false));
struct X(Arc<AtomicBool>);
impl Drop for X {
fn drop(&mut self) {
self.0.store(true, Ordering::SeqCst);
}
}
let val = Sticky::new(X(was_called.clone()));
mem::drop(val);
assert!(was_called.load(Ordering::SeqCst));
}
#[test]
fn test_noop_drop_elsewhere() {
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::thread;
let was_called = Arc::new(AtomicBool::new(false));
{
let was_called = was_called.clone();
thread::spawn(move || {
struct X(Arc<AtomicBool>);
impl Drop for X {
fn drop(&mut self) {
self.0.store(true, Ordering::SeqCst);
}
}
let val = Sticky::new(X(was_called.clone()));
assert!(thread::spawn(move || {
// moves it here but do not deallocate
crate::stack_token!(tok);
val.try_get(tok).ok();
})
.join()
.is_ok());
assert!(!was_called.load(Ordering::SeqCst));
})
.join()
.unwrap();
}
assert!(was_called.load(Ordering::SeqCst));
}
#[test]
fn test_rc_sending() {
use std::rc::Rc;
use std::thread;
let val = Sticky::new(Rc::new(true));
thread::spawn(move || {
crate::stack_token!(tok);
assert!(val.try_get(tok).is_err());
})
.join()
.unwrap();
}
#[test]
fn test_two_stickies() {
struct Wat;
impl Drop for Wat {
fn drop(&mut self) {
// do nothing
}
}
let s1 = Sticky::new(Wat);
let s2 = Sticky::new(Wat);
// make sure all is well
drop(s1);
drop(s2);
}