2012-09-19 17:43:41 -07:00
|
|
|
#[doc(hidden)]; // FIXME #3538
|
|
|
|
|
2012-09-19 17:37:09 -07:00
|
|
|
use local_data::LocalDataKey;
|
2012-09-19 17:57:32 -07:00
|
|
|
use rt::rust_task;
|
2012-09-19 17:37:09 -07:00
|
|
|
|
2012-10-02 16:31:52 -07:00
|
|
|
pub trait LocalData { }
|
2012-09-19 17:37:09 -07:00
|
|
|
impl<T: Owned> @T: LocalData { }
|
|
|
|
|
2012-09-19 18:00:26 -07:00
|
|
|
impl LocalData: Eq {
|
|
|
|
pure fn eq(other: &@LocalData) -> bool unsafe {
|
|
|
|
let ptr_a: (uint, uint) = cast::reinterpret_cast(&self);
|
|
|
|
let ptr_b: (uint, uint) = cast::reinterpret_cast(other);
|
|
|
|
return ptr_a == ptr_b;
|
|
|
|
}
|
|
|
|
pure fn ne(other: &@LocalData) -> bool { !self.eq(other) }
|
|
|
|
}
|
2012-09-19 17:37:09 -07:00
|
|
|
|
|
|
|
// We use dvec because it's the best data structure in core. If TLS is used
|
|
|
|
// heavily in future, this could be made more efficient with a proper map.
|
2012-10-03 22:06:51 -07:00
|
|
|
type TaskLocalElement = (*libc::c_void, *libc::c_void, LocalData);
|
2012-09-19 17:37:09 -07:00
|
|
|
// Has to be a pointer at outermost layer; the foreign call returns void *.
|
2012-10-03 22:06:51 -07:00
|
|
|
type TaskLocalMap = @dvec::DVec<Option<TaskLocalElement>>;
|
2012-09-19 17:37:09 -07:00
|
|
|
|
2012-10-03 22:06:51 -07:00
|
|
|
extern fn cleanup_task_local_map(map_ptr: *libc::c_void) unsafe {
|
2012-09-19 17:37:09 -07:00
|
|
|
assert !map_ptr.is_null();
|
|
|
|
// Get and keep the single reference that was created at the beginning.
|
|
|
|
let _map: TaskLocalMap = cast::reinterpret_cast(&map_ptr);
|
|
|
|
// All local_data will be destroyed along with the map.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gets the map from the runtime. Lazily initialises if not done so already.
|
2012-10-03 22:06:51 -07:00
|
|
|
unsafe fn get_task_local_map(task: *rust_task) -> TaskLocalMap {
|
2012-09-19 17:37:09 -07:00
|
|
|
|
|
|
|
// Relies on the runtime initialising the pointer to null.
|
|
|
|
// NOTE: The map's box lives in TLS invisibly referenced once. Each time
|
|
|
|
// we retrieve it for get/set, we make another reference, which get/set
|
|
|
|
// drop when they finish. No "re-storing after modifying" is needed.
|
2012-09-19 17:57:32 -07:00
|
|
|
let map_ptr = rt::rust_get_task_local_data(task);
|
2012-09-19 17:37:09 -07:00
|
|
|
if map_ptr.is_null() {
|
|
|
|
let map: TaskLocalMap = @dvec::DVec();
|
|
|
|
// Use reinterpret_cast -- transmute would take map away from us also.
|
2012-09-19 17:57:32 -07:00
|
|
|
rt::rust_set_task_local_data(
|
2012-09-19 17:37:09 -07:00
|
|
|
task, cast::reinterpret_cast(&map));
|
2012-09-19 17:57:32 -07:00
|
|
|
rt::rust_task_local_data_atexit(task, cleanup_task_local_map);
|
2012-09-19 17:37:09 -07:00
|
|
|
// Also need to reference it an extra time to keep it for now.
|
|
|
|
cast::bump_box_refcount(map);
|
|
|
|
map
|
|
|
|
} else {
|
|
|
|
let map = cast::transmute(move map_ptr);
|
|
|
|
cast::bump_box_refcount(map);
|
|
|
|
map
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-03 22:06:51 -07:00
|
|
|
unsafe fn key_to_key_value<T: Owned>(
|
2012-09-19 17:37:09 -07:00
|
|
|
key: LocalDataKey<T>) -> *libc::c_void {
|
|
|
|
|
|
|
|
// Keys are closures, which are (fnptr,envptr) pairs. Use fnptr.
|
|
|
|
// Use reintepret_cast -- transmute would leak (forget) the closure.
|
|
|
|
let pair: (*libc::c_void, *libc::c_void) = cast::reinterpret_cast(&key);
|
|
|
|
pair.first()
|
|
|
|
}
|
|
|
|
|
|
|
|
// If returning Some(..), returns with @T with the map's reference. Careful!
|
2012-10-03 22:06:51 -07:00
|
|
|
unsafe fn local_data_lookup<T: Owned>(
|
2012-09-19 17:37:09 -07:00
|
|
|
map: TaskLocalMap, key: LocalDataKey<T>)
|
|
|
|
-> Option<(uint, *libc::c_void)> {
|
|
|
|
|
|
|
|
let key_value = key_to_key_value(key);
|
|
|
|
let map_pos = (*map).position(|entry|
|
2012-09-28 21:51:14 -07:00
|
|
|
match *entry {
|
2012-09-19 17:37:09 -07:00
|
|
|
Some((k,_,_)) => k == key_value,
|
|
|
|
None => false
|
|
|
|
}
|
|
|
|
);
|
|
|
|
do map_pos.map |index| {
|
|
|
|
// .get() is guaranteed because of "None { false }" above.
|
2012-09-26 16:27:12 -07:00
|
|
|
let (_, data_ptr, _) = (*map)[*index].get();
|
|
|
|
(*index, data_ptr)
|
2012-09-19 17:37:09 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-03 22:06:51 -07:00
|
|
|
unsafe fn local_get_helper<T: Owned>(
|
2012-09-19 17:37:09 -07:00
|
|
|
task: *rust_task, key: LocalDataKey<T>,
|
|
|
|
do_pop: bool) -> Option<@T> {
|
|
|
|
|
|
|
|
let map = get_task_local_map(task);
|
|
|
|
// Interpreturn our findings from the map
|
|
|
|
do local_data_lookup(map, key).map |result| {
|
|
|
|
// A reference count magically appears on 'data' out of thin air. It
|
|
|
|
// was referenced in the local_data box, though, not here, so before
|
|
|
|
// overwriting the local_data_box we need to give an extra reference.
|
|
|
|
// We must also give an extra reference when not removing.
|
2012-09-26 16:27:12 -07:00
|
|
|
let (index, data_ptr) = *result;
|
2012-09-19 17:37:09 -07:00
|
|
|
let data: @T = cast::transmute(move data_ptr);
|
|
|
|
cast::bump_box_refcount(data);
|
|
|
|
if do_pop {
|
|
|
|
(*map).set_elt(index, None);
|
|
|
|
}
|
|
|
|
data
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-02 16:31:52 -07:00
|
|
|
pub unsafe fn local_pop<T: Owned>(
|
2012-09-19 17:37:09 -07:00
|
|
|
task: *rust_task,
|
|
|
|
key: LocalDataKey<T>) -> Option<@T> {
|
|
|
|
|
|
|
|
local_get_helper(task, key, true)
|
|
|
|
}
|
|
|
|
|
2012-10-02 16:31:52 -07:00
|
|
|
pub unsafe fn local_get<T: Owned>(
|
2012-09-19 17:37:09 -07:00
|
|
|
task: *rust_task,
|
|
|
|
key: LocalDataKey<T>) -> Option<@T> {
|
|
|
|
|
|
|
|
local_get_helper(task, key, false)
|
|
|
|
}
|
|
|
|
|
2012-10-02 16:31:52 -07:00
|
|
|
pub unsafe fn local_set<T: Owned>(
|
2012-10-02 11:37:37 -07:00
|
|
|
task: *rust_task, key: LocalDataKey<T>, data: @T) {
|
2012-09-19 17:37:09 -07:00
|
|
|
|
|
|
|
let map = get_task_local_map(task);
|
|
|
|
// Store key+data as *voids. Data is invisibly referenced once; key isn't.
|
|
|
|
let keyval = key_to_key_value(key);
|
|
|
|
// We keep the data in two forms: one as an unsafe pointer, so we can get
|
|
|
|
// it back by casting; another in an existential box, so the reference we
|
|
|
|
// own on it can be dropped when the box is destroyed. The unsafe pointer
|
|
|
|
// does not have a reference associated with it, so it may become invalid
|
|
|
|
// when the box is destroyed.
|
|
|
|
let data_ptr = cast::reinterpret_cast(&data);
|
|
|
|
let data_box = data as LocalData;
|
|
|
|
// Construct new entry to store in the map.
|
|
|
|
let new_entry = Some((keyval, data_ptr, data_box));
|
|
|
|
// Find a place to put it.
|
|
|
|
match local_data_lookup(map, key) {
|
|
|
|
Some((index, _old_data_ptr)) => {
|
|
|
|
// Key already had a value set, _old_data_ptr, whose reference
|
|
|
|
// will get dropped when the local_data box is overwritten.
|
|
|
|
(*map).set_elt(index, new_entry);
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
// Find an empty slot. If not, grow the vector.
|
|
|
|
match (*map).position(|x| x.is_none()) {
|
|
|
|
Some(empty_index) => (*map).set_elt(empty_index, new_entry),
|
|
|
|
None => (*map).push(new_entry)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-02 16:31:52 -07:00
|
|
|
pub unsafe fn local_modify<T: Owned>(
|
2012-09-19 17:37:09 -07:00
|
|
|
task: *rust_task, key: LocalDataKey<T>,
|
|
|
|
modify_fn: fn(Option<@T>) -> Option<@T>) {
|
|
|
|
|
|
|
|
// Could be more efficient by doing the lookup work, but this is easy.
|
|
|
|
let newdata = modify_fn(local_pop(task, key));
|
|
|
|
if newdata.is_some() {
|
|
|
|
local_set(task, key, option::unwrap(move newdata));
|
|
|
|
}
|
|
|
|
}
|