2013-05-30 05:16:33 -05:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 18:48:01 -06:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2012-09-19 19:29:54 -05:00
|
|
|
/*!
|
|
|
|
|
|
|
|
Task local data management
|
|
|
|
|
2013-07-12 03:38:44 -05:00
|
|
|
Allows storing arbitrary types inside task-local-storage (TLS), to be accessed
|
2013-07-14 03:43:31 -05:00
|
|
|
anywhere within a task, keyed by a global pointer parameterized over the type of
|
|
|
|
the TLS slot. Useful for dynamic variables, singletons, and interfacing with
|
|
|
|
foreign code with bad callback interfaces.
|
2012-09-19 19:29:54 -05:00
|
|
|
|
2013-07-14 03:43:31 -05:00
|
|
|
To use, declare a static variable of the type you wish to store. The
|
|
|
|
initialization should be `&local_data::Key`. This is then the key to what you
|
|
|
|
wish to store.
|
2013-07-09 19:25:28 -05:00
|
|
|
|
|
|
|
~~~{.rust}
|
|
|
|
use std::local_data;
|
|
|
|
|
2013-07-14 03:43:31 -05:00
|
|
|
static key_int: local_data::Key<int> = &local_data::Key;
|
|
|
|
static key_vector: local_data::Key<~[int]> = &local_data::Key;
|
2013-07-09 19:25:28 -05:00
|
|
|
|
2013-07-12 03:38:44 -05:00
|
|
|
local_data::set(key_int, 3);
|
|
|
|
local_data::get(key_int, |opt| assert_eq!(opt, Some(&3)));
|
2013-07-09 19:25:28 -05:00
|
|
|
|
2013-07-12 03:38:44 -05:00
|
|
|
local_data::set(key_vector, ~[4]);
|
|
|
|
local_data::get(key_int, |opt| assert_eq!(opt, Some(&~[4])));
|
2013-07-09 19:25:28 -05:00
|
|
|
~~~
|
2012-09-19 19:29:54 -05:00
|
|
|
|
2012-09-19 19:43:41 -05:00
|
|
|
Casting 'Arcane Sight' reveals an overwhelming aura of Transmutation
|
|
|
|
magic.
|
2012-09-19 19:29:54 -05:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
2013-01-08 21:37:25 -06:00
|
|
|
use prelude::*;
|
2013-05-24 21:35:29 -05:00
|
|
|
|
2013-07-16 19:20:43 -05:00
|
|
|
use task::local_data_priv::*;
|
2013-05-30 05:16:33 -05:00
|
|
|
|
|
|
|
#[cfg(test)] use task;
|
2012-09-19 19:29:54 -05:00
|
|
|
|
|
|
|
/**
|
2013-07-14 03:43:31 -05:00
|
|
|
* Indexes a task-local data slot. This pointer is used for comparison to
|
|
|
|
* differentiate keys from one another. The actual type `T` is not used anywhere
|
|
|
|
* as a member of this type, except that it is parameterized with it to define
|
|
|
|
* the type of each key's value.
|
2012-09-19 19:29:54 -05:00
|
|
|
*
|
2013-07-14 03:43:31 -05:00
|
|
|
* The value of each Key is of the singleton enum KeyValue. These also have the
|
|
|
|
* same name as `Key` and their purpose is to take up space in the programs data
|
|
|
|
* sections to ensure that each value of the `Key` type points to a unique
|
|
|
|
* location.
|
2012-09-19 19:29:54 -05:00
|
|
|
*/
|
2013-07-12 03:38:44 -05:00
|
|
|
#[cfg(not(stage0))]
|
2013-07-14 03:43:31 -05:00
|
|
|
pub type Key<T> = &'static KeyValue<T>;
|
2013-07-12 03:38:44 -05:00
|
|
|
#[cfg(stage0)]
|
2013-07-10 16:43:25 -05:00
|
|
|
pub type Key<'self,T> = &'self fn(v: T);
|
2012-09-19 19:29:54 -05:00
|
|
|
|
2013-07-14 03:43:31 -05:00
|
|
|
pub enum KeyValue<T> { Key }
|
|
|
|
|
2012-09-19 19:29:54 -05:00
|
|
|
/**
|
|
|
|
* Remove a task-local data value from the table, returning the
|
|
|
|
* reference that was originally created to insert it.
|
|
|
|
*/
|
2013-07-11 00:14:40 -05:00
|
|
|
#[cfg(stage0)]
|
2013-07-12 03:38:44 -05:00
|
|
|
pub fn pop<T: 'static>(key: Key<@T>) -> Option<@T> {
|
|
|
|
unsafe { local_pop(Handle::new(), key) }
|
2013-07-11 00:14:40 -05:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Remove a task-local data value from the table, returning the
|
|
|
|
* reference that was originally created to insert it.
|
|
|
|
*/
|
|
|
|
#[cfg(not(stage0))]
|
2013-07-12 03:38:44 -05:00
|
|
|
pub fn pop<T: 'static>(key: Key<T>) -> Option<T> {
|
|
|
|
unsafe { local_pop(Handle::new(), key) }
|
2012-09-19 19:29:54 -05:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Retrieve a task-local data value. It will also be kept alive in the
|
|
|
|
* table until explicitly removed.
|
|
|
|
*/
|
2013-07-11 00:14:40 -05:00
|
|
|
#[cfg(stage0)]
|
2013-07-12 03:38:44 -05:00
|
|
|
pub fn get<T: 'static, U>(key: Key<@T>, f: &fn(Option<&@T>) -> U) -> U {
|
|
|
|
unsafe { local_get(Handle::new(), key, f) }
|
2013-07-11 00:14:40 -05:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Retrieve a task-local data value. It will also be kept alive in the
|
|
|
|
* table until explicitly removed.
|
|
|
|
*/
|
|
|
|
#[cfg(not(stage0))]
|
2013-07-12 03:38:44 -05:00
|
|
|
pub fn get<T: 'static, U>(key: Key<T>, f: &fn(Option<&T>) -> U) -> U {
|
|
|
|
unsafe { local_get(Handle::new(), key, f) }
|
2012-09-19 19:29:54 -05:00
|
|
|
}
|
2013-07-16 19:20:43 -05:00
|
|
|
/**
|
|
|
|
* Retrieve a mutable borrowed pointer to a task-local data value.
|
|
|
|
*/
|
|
|
|
#[cfg(not(stage0))]
|
|
|
|
pub fn get_mut<T: 'static, U>(key: Key<T>, f: &fn(Option<&mut T>) -> U) -> U {
|
|
|
|
unsafe { local_get_mut(Handle::new(), key, f) }
|
|
|
|
}
|
2012-09-19 19:29:54 -05:00
|
|
|
/**
|
|
|
|
* Store a value in task-local data. If this key already has a value,
|
|
|
|
* that value is overwritten (and its destructor is run).
|
|
|
|
*/
|
2013-07-11 00:14:40 -05:00
|
|
|
#[cfg(stage0)]
|
2013-07-12 03:38:44 -05:00
|
|
|
pub fn set<T: 'static>(key: Key<@T>, data: @T) {
|
|
|
|
unsafe { local_set(Handle::new(), key, data) }
|
2012-09-19 19:29:54 -05:00
|
|
|
}
|
2013-07-11 00:14:40 -05:00
|
|
|
/**
|
|
|
|
* Store a value in task-local data. If this key already has a value,
|
|
|
|
* that value is overwritten (and its destructor is run).
|
|
|
|
*/
|
|
|
|
#[cfg(not(stage0))]
|
2013-07-12 03:38:44 -05:00
|
|
|
pub fn set<T: 'static>(key: Key<T>, data: T) {
|
|
|
|
unsafe { local_set(Handle::new(), key, data) }
|
2013-07-11 00:14:40 -05:00
|
|
|
}
|
2012-09-19 19:29:54 -05:00
|
|
|
/**
|
|
|
|
* Modify a task-local data value. If the function returns 'None', the
|
|
|
|
* data is removed (and its reference dropped).
|
|
|
|
*/
|
2013-07-11 00:14:40 -05:00
|
|
|
#[cfg(stage0)]
|
2013-07-12 03:38:44 -05:00
|
|
|
pub fn modify<T: 'static>(key: Key<@T>, f: &fn(Option<@T>) -> Option<@T>) {
|
2013-07-09 19:25:28 -05:00
|
|
|
match f(pop(key)) {
|
|
|
|
Some(next) => { set(key, next); }
|
2013-07-09 11:40:50 -05:00
|
|
|
None => {}
|
|
|
|
}
|
2012-09-19 19:29:54 -05:00
|
|
|
}
|
2013-07-11 00:14:40 -05:00
|
|
|
/**
|
|
|
|
* Modify a task-local data value. If the function returns 'None', the
|
|
|
|
* data is removed (and its reference dropped).
|
|
|
|
*/
|
|
|
|
#[cfg(not(stage0))]
|
2013-07-12 03:38:44 -05:00
|
|
|
pub fn modify<T: 'static>(key: Key<T>, f: &fn(Option<T>) -> Option<T>) {
|
2013-07-02 14:47:32 -05:00
|
|
|
unsafe {
|
|
|
|
match f(pop(::cast::unsafe_copy(&key))) {
|
|
|
|
Some(next) => { set(key, next); }
|
|
|
|
None => {}
|
|
|
|
}
|
2013-07-11 00:14:40 -05:00
|
|
|
}
|
|
|
|
}
|
2012-09-19 19:29:54 -05:00
|
|
|
|
|
|
|
#[test]
|
2013-01-23 13:43:58 -06:00
|
|
|
fn test_tls_multitask() {
|
2013-07-14 03:43:31 -05:00
|
|
|
static my_key: Key<@~str> = &Key;
|
2013-07-12 03:38:44 -05:00
|
|
|
set(my_key, @~"parent data");
|
|
|
|
do task::spawn {
|
|
|
|
// TLS shouldn't carry over.
|
|
|
|
assert!(get(my_key, |k| k.map(|&k| *k)).is_none());
|
|
|
|
set(my_key, @~"child data");
|
|
|
|
assert!(*(get(my_key, |k| k.map(|&k| *k)).get()) ==
|
|
|
|
~"child data");
|
|
|
|
// should be cleaned up for us
|
2012-09-19 19:29:54 -05:00
|
|
|
}
|
2013-07-12 03:38:44 -05:00
|
|
|
// Must work multiple times
|
|
|
|
assert!(*(get(my_key, |k| k.map(|&k| *k)).get()) == ~"parent data");
|
|
|
|
assert!(*(get(my_key, |k| k.map(|&k| *k)).get()) == ~"parent data");
|
|
|
|
assert!(*(get(my_key, |k| k.map(|&k| *k)).get()) == ~"parent data");
|
2012-09-19 19:29:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-01-23 13:43:58 -06:00
|
|
|
fn test_tls_overwrite() {
|
2013-07-14 03:43:31 -05:00
|
|
|
static my_key: Key<@~str> = &Key;
|
2013-07-12 03:38:44 -05:00
|
|
|
set(my_key, @~"first data");
|
|
|
|
set(my_key, @~"next data"); // Shouldn't leak.
|
|
|
|
assert!(*(get(my_key, |k| k.map(|&k| *k)).get()) == ~"next data");
|
2012-09-19 19:29:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-01-23 13:43:58 -06:00
|
|
|
fn test_tls_pop() {
|
2013-07-14 03:43:31 -05:00
|
|
|
static my_key: Key<@~str> = &Key;
|
2013-07-12 03:38:44 -05:00
|
|
|
set(my_key, @~"weasel");
|
|
|
|
assert!(*(pop(my_key).get()) == ~"weasel");
|
|
|
|
// Pop must remove the data from the map.
|
|
|
|
assert!(pop(my_key).is_none());
|
2012-09-19 19:29:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-01-23 13:43:58 -06:00
|
|
|
fn test_tls_modify() {
|
2013-07-14 03:43:31 -05:00
|
|
|
static my_key: Key<@~str> = &Key;
|
2013-07-12 03:38:44 -05:00
|
|
|
modify(my_key, |data| {
|
|
|
|
match data {
|
|
|
|
Some(@ref val) => fail!("unwelcome value: %s", *val),
|
|
|
|
None => Some(@~"first data")
|
|
|
|
}
|
|
|
|
});
|
|
|
|
modify(my_key, |data| {
|
|
|
|
match data {
|
|
|
|
Some(@~"first data") => Some(@~"next data"),
|
|
|
|
Some(@ref val) => fail!("wrong value: %s", *val),
|
|
|
|
None => fail!("missing value")
|
|
|
|
}
|
|
|
|
});
|
|
|
|
assert!(*(pop(my_key).get()) == ~"next data");
|
2012-09-19 19:29:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-01-23 13:43:58 -06:00
|
|
|
fn test_tls_crust_automorestack_memorial_bug() {
|
2013-04-18 19:40:37 -05:00
|
|
|
// This might result in a stack-canary clobber if the runtime fails to
|
|
|
|
// set sp_limit to 0 when calling the cleanup extern - it might
|
|
|
|
// automatically jump over to the rust stack, which causes next_c_sp
|
|
|
|
// to get recorded as something within a rust stack segment. Then a
|
|
|
|
// subsequent upcall (esp. for logging, think vsnprintf) would run on
|
|
|
|
// a stack smaller than 1 MB.
|
2013-07-14 03:43:31 -05:00
|
|
|
static my_key: Key<@~str> = &Key;
|
2013-04-18 19:40:37 -05:00
|
|
|
do task::spawn {
|
2013-07-12 03:38:44 -05:00
|
|
|
set(my_key, @~"hax");
|
2012-09-19 19:29:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-01-23 13:43:58 -06:00
|
|
|
fn test_tls_multiple_types() {
|
2013-07-14 03:43:31 -05:00
|
|
|
static str_key: Key<@~str> = &Key;
|
|
|
|
static box_key: Key<@@()> = &Key;
|
|
|
|
static int_key: Key<@int> = &Key;
|
2013-04-18 19:40:37 -05:00
|
|
|
do task::spawn {
|
2013-07-12 03:38:44 -05:00
|
|
|
set(str_key, @~"string data");
|
|
|
|
set(box_key, @@());
|
|
|
|
set(int_key, @42);
|
2012-09-19 19:29:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-10-04 00:06:51 -05:00
|
|
|
fn test_tls_overwrite_multiple_types() {
|
2013-07-14 03:43:31 -05:00
|
|
|
static str_key: Key<@~str> = &Key;
|
|
|
|
static box_key: Key<@@()> = &Key;
|
|
|
|
static int_key: Key<@int> = &Key;
|
2013-01-23 13:43:58 -06:00
|
|
|
do task::spawn {
|
2013-07-12 03:38:44 -05:00
|
|
|
set(str_key, @~"string data");
|
|
|
|
set(int_key, @42);
|
|
|
|
// This could cause a segfault if overwriting-destruction is done
|
|
|
|
// with the crazy polymorphic transmute rather than the provided
|
|
|
|
// finaliser.
|
|
|
|
set(int_key, @31337);
|
2012-09-19 19:29:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_fail]
|
|
|
|
#[ignore(cfg(windows))]
|
2013-01-23 13:43:58 -06:00
|
|
|
fn test_tls_cleanup_on_failure() {
|
2013-07-14 03:43:31 -05:00
|
|
|
static str_key: Key<@~str> = &Key;
|
|
|
|
static box_key: Key<@@()> = &Key;
|
|
|
|
static int_key: Key<@int> = &Key;
|
2013-07-12 03:38:44 -05:00
|
|
|
set(str_key, @~"parent data");
|
|
|
|
set(box_key, @@());
|
|
|
|
do task::spawn {
|
|
|
|
// spawn_linked
|
|
|
|
set(str_key, @~"string data");
|
2013-07-09 19:25:28 -05:00
|
|
|
set(box_key, @@());
|
2013-07-12 03:38:44 -05:00
|
|
|
set(int_key, @42);
|
2013-02-11 21:26:38 -06:00
|
|
|
fail!();
|
2012-09-19 19:29:54 -05:00
|
|
|
}
|
2013-07-12 03:38:44 -05:00
|
|
|
// Not quite nondeterministic.
|
|
|
|
set(int_key, @31337);
|
|
|
|
fail!();
|
2012-09-19 19:29:54 -05:00
|
|
|
}
|
2013-05-07 15:53:25 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_static_pointer() {
|
2013-07-14 03:43:31 -05:00
|
|
|
static key: Key<@&'static int> = &Key;
|
2013-07-12 03:38:44 -05:00
|
|
|
static VALUE: int = 0;
|
|
|
|
let v: @&'static int = @&VALUE;
|
|
|
|
set(key, v);
|
2013-05-05 17:18:51 -05:00
|
|
|
}
|
2013-07-11 02:19:23 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_owned() {
|
2013-07-14 03:43:31 -05:00
|
|
|
static key: Key<~int> = &Key;
|
2013-07-12 03:38:44 -05:00
|
|
|
set(key, ~1);
|
2013-07-16 19:20:43 -05:00
|
|
|
|
|
|
|
do get(key) |v| {
|
|
|
|
do get(key) |v| {
|
|
|
|
do get(key) |v| {
|
|
|
|
assert_eq!(**v.unwrap(), 1);
|
|
|
|
}
|
|
|
|
assert_eq!(**v.unwrap(), 1);
|
|
|
|
}
|
|
|
|
assert_eq!(**v.unwrap(), 1);
|
|
|
|
}
|
|
|
|
set(key, ~2);
|
|
|
|
do get(key) |v| {
|
|
|
|
assert_eq!(**v.unwrap(), 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_get_mut() {
|
|
|
|
static key: Key<int> = &Key;
|
|
|
|
set(key, 1);
|
|
|
|
|
|
|
|
do get_mut(key) |v| {
|
|
|
|
*v.unwrap() = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
do get(key) |v| {
|
|
|
|
assert_eq!(*v.unwrap(), 2);
|
|
|
|
}
|
2013-07-12 03:38:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_same_key_type() {
|
2013-07-14 03:43:31 -05:00
|
|
|
static key1: Key<int> = &Key;
|
|
|
|
static key2: Key<int> = &Key;
|
|
|
|
static key3: Key<int> = &Key;
|
|
|
|
static key4: Key<int> = &Key;
|
|
|
|
static key5: Key<int> = &Key;
|
2013-07-12 03:38:44 -05:00
|
|
|
set(key1, 1);
|
|
|
|
set(key2, 2);
|
|
|
|
set(key3, 3);
|
|
|
|
set(key4, 4);
|
|
|
|
set(key5, 5);
|
|
|
|
|
|
|
|
get(key1, |x| assert_eq!(*x.unwrap(), 1));
|
|
|
|
get(key2, |x| assert_eq!(*x.unwrap(), 2));
|
|
|
|
get(key3, |x| assert_eq!(*x.unwrap(), 3));
|
|
|
|
get(key4, |x| assert_eq!(*x.unwrap(), 4));
|
|
|
|
get(key5, |x| assert_eq!(*x.unwrap(), 5));
|
2013-07-11 02:19:23 -05:00
|
|
|
}
|
2013-07-16 19:20:43 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_fail]
|
|
|
|
fn test_nested_get_set1() {
|
|
|
|
static key: Key<int> = &Key;
|
|
|
|
set(key, 4);
|
|
|
|
do get(key) |_| {
|
|
|
|
set(key, 4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_fail]
|
|
|
|
fn test_nested_get_mut2() {
|
|
|
|
static key: Key<int> = &Key;
|
|
|
|
set(key, 4);
|
|
|
|
do get(key) |_| {
|
|
|
|
get_mut(key, |_| {})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_fail]
|
|
|
|
fn test_nested_get_mut3() {
|
|
|
|
static key: Key<int> = &Key;
|
|
|
|
set(key, 4);
|
|
|
|
do get_mut(key) |_| {
|
|
|
|
get(key, |_| {})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_fail]
|
|
|
|
fn test_nested_get_mut4() {
|
|
|
|
static key: Key<int> = &Key;
|
|
|
|
set(key, 4);
|
|
|
|
do get_mut(key) |_| {
|
|
|
|
get_mut(key, |_| {})
|
|
|
|
}
|
|
|
|
}
|