Skip to content

Commit a89af1f

Browse files
committed
Use purely an owned vector for storing TLS data
1 parent e388a80 commit a89af1f

File tree

4 files changed

+39
-55
lines changed

4 files changed

+39
-55
lines changed

src/libstd/task/local_data_priv.rs

Lines changed: 35 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313
use cast;
1414
use cmp::Eq;
1515
use libc;
16+
use local_data::LocalDataKey;
1617
use prelude::*;
18+
use sys;
1719
use task::rt;
18-
use local_data::LocalDataKey;
1920

2021
use super::rt::rust_task;
2122
use rt::task::{Task, LocalStorage};
@@ -61,7 +62,7 @@ impl Eq for @LocalData {
6162
// proper map.
6263
type TaskLocalElement = (*libc::c_void, *libc::c_void, @LocalData);
6364
// Has to be a pointer at outermost layer; the foreign call returns void *.
64-
type TaskLocalMap = @mut ~[Option<TaskLocalElement>];
65+
type TaskLocalMap = ~[Option<TaskLocalElement>];
6566

6667
fn cleanup_task_local_map(map_ptr: *libc::c_void) {
6768
unsafe {
@@ -74,85 +75,75 @@ fn cleanup_task_local_map(map_ptr: *libc::c_void) {
7475
}
7576

7677
// Gets the map from the runtime. Lazily initialises if not done so already.
77-
unsafe fn get_local_map(handle: Handle) -> TaskLocalMap {
78+
unsafe fn get_local_map(handle: Handle) -> &mut TaskLocalMap {
7879
match handle {
7980
OldHandle(task) => get_task_local_map(task),
8081
NewHandle(local_storage) => get_newsched_local_map(local_storage)
8182
}
8283
}
8384

84-
unsafe fn get_task_local_map(task: *rust_task) -> TaskLocalMap {
85+
unsafe fn get_task_local_map(task: *rust_task) -> &mut TaskLocalMap {
8586

8687
extern fn cleanup_task_local_map_extern_cb(map_ptr: *libc::c_void) {
8788
cleanup_task_local_map(map_ptr);
8889
}
8990

9091
// Relies on the runtime initialising the pointer to null.
91-
// Note: The map's box lives in TLS invisibly referenced once. Each time
92-
// we retrieve it for get/set, we make another reference, which get/set
93-
// drop when they finish. No "re-storing after modifying" is needed.
92+
// Note: the map is an owned pointer and is "owned" by TLS. It is moved
93+
// into the tls slot for this task, and then mutable loans are taken from
94+
// this slot to modify the map.
9495
let map_ptr = rt::rust_get_task_local_data(task);
95-
if map_ptr.is_null() {
96-
let map: TaskLocalMap = @mut ~[];
97-
// NB: This bumps the ref count before converting to an unsafe pointer,
98-
// keeping the map alive until TLS is destroyed
99-
rt::rust_set_task_local_data(task, cast::transmute(map));
96+
if (*map_ptr).is_null() {
97+
// First time TLS is used, create a new map and set up the necessary
98+
// TLS information for its safe destruction
99+
let map: TaskLocalMap = ~[];
100+
*map_ptr = cast::transmute(map);
100101
rt::rust_task_local_data_atexit(task, cleanup_task_local_map_extern_cb);
101-
map
102-
} else {
103-
let map = cast::transmute(map_ptr);
104-
let nonmut = cast::transmute::<TaskLocalMap,
105-
@~[Option<TaskLocalElement>]>(map);
106-
cast::bump_box_refcount(nonmut);
107-
map
108102
}
103+
return cast::transmute(map_ptr);
109104
}
110105

111-
unsafe fn get_newsched_local_map(local: *mut LocalStorage) -> TaskLocalMap {
106+
unsafe fn get_newsched_local_map(local: *mut LocalStorage) -> &mut TaskLocalMap {
107+
// This is based on the same idea as the oldsched code above.
112108
match &mut *local {
113-
&LocalStorage(map_ptr, Some(_)) => {
109+
// If the at_exit function is already set, then we just need to take a
110+
// loan out on the TLS map stored inside
111+
&LocalStorage(ref mut map_ptr, Some(_)) => {
114112
assert!(map_ptr.is_not_null());
115-
let map = cast::transmute(map_ptr);
116-
let nonmut = cast::transmute::<TaskLocalMap,
117-
@~[Option<TaskLocalElement>]>(map);
118-
cast::bump_box_refcount(nonmut);
119-
return map;
113+
return cast::transmute(map_ptr);
120114
}
115+
// If this is the first time we've accessed TLS, perform similar
116+
// actions to the oldsched way of doing things.
121117
&LocalStorage(ref mut map_ptr, ref mut at_exit) => {
122-
assert!((*map_ptr).is_null());
123-
let map: TaskLocalMap = @mut ~[];
118+
assert!(map_ptr.is_null());
119+
assert!(at_exit.is_none());
120+
let map: TaskLocalMap = ~[];
124121
*map_ptr = cast::transmute(map);
125-
let at_exit_fn: ~fn(*libc::c_void) = |p|cleanup_task_local_map(p);
122+
let at_exit_fn: ~fn(*libc::c_void) = |p| cleanup_task_local_map(p);
126123
*at_exit = Some(at_exit_fn);
127-
return map;
124+
return cast::transmute(map_ptr);
128125
}
129126
}
130127
}
131128

132129
unsafe fn key_to_key_value<T: 'static>(key: LocalDataKey<T>) -> *libc::c_void {
133-
// Keys are closures, which are (fnptr,envptr) pairs. Use fnptr.
134-
// Use reinterpret_cast -- transmute would leak (forget) the closure.
135-
let pair: (*libc::c_void, *libc::c_void) = cast::transmute_copy(&key);
136-
pair.first()
130+
let pair: sys::Closure = cast::transmute(key);
131+
return pair.code as *libc::c_void;
137132
}
138133

139134
// If returning Some(..), returns with @T with the map's reference. Careful!
140135
unsafe fn local_data_lookup<T: 'static>(
141-
map: TaskLocalMap, key: LocalDataKey<T>)
136+
map: &mut TaskLocalMap, key: LocalDataKey<T>)
142137
-> Option<(uint, *libc::c_void)> {
143138

144139
let key_value = key_to_key_value(key);
145-
let map_pos = (*map).iter().position(|entry|
140+
for map.iter().enumerate().advance |(i, entry)| {
146141
match *entry {
147-
Some((k,_,_)) => k == key_value,
148-
None => false
142+
Some((k, data, _)) if k == key_value => { return Some((i, data)); }
143+
_ => {}
149144
}
150-
);
151-
do map_pos.map |index| {
152-
// .get() is guaranteed because of "None { false }" above.
153-
let (_, data_ptr, _) = (*map)[*index].get();
154-
(*index, data_ptr)
155145
}
146+
return None;
156147
}
157148

158149
unsafe fn local_get_helper<T: 'static>(
@@ -215,7 +206,7 @@ pub unsafe fn local_set<T: 'static>(
215206
}
216207
None => {
217208
// Find an empty slot. If not, grow the vector.
218-
match (*map).iter().position(|x| x.is_none()) {
209+
match map.iter().position(|x| x.is_none()) {
219210
Some(empty_index) => { map[empty_index] = new_entry; }
220211
None => { map.push(new_entry); }
221212
}

src/libstd/task/rt.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,7 @@ pub extern {
6363
fn rust_task_kill_all(task: *rust_task);
6464

6565
#[rust_stack]
66-
fn rust_get_task_local_data(task: *rust_task) -> *libc::c_void;
67-
#[rust_stack]
68-
fn rust_set_task_local_data(task: *rust_task, map: *libc::c_void);
66+
fn rust_get_task_local_data(task: *rust_task) -> *mut *libc::c_void;
6967
#[rust_stack]
7068
fn rust_task_local_data_atexit(task: *rust_task, cleanup_fn: *u8);
7169
}

src/rt/rust_builtin.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -672,14 +672,10 @@ rust_unlock_little_lock(lock_and_signal *lock) {
672672
lock->unlock();
673673
}
674674

675-
// set/get/atexit task_local_data can run on the rust stack for speed.
676-
extern "C" void *
675+
// get/atexit task_local_data can run on the rust stack for speed.
676+
extern "C" void **
677677
rust_get_task_local_data(rust_task *task) {
678-
return task->task_local_data;
679-
}
680-
extern "C" void
681-
rust_set_task_local_data(rust_task *task, void *data) {
682-
task->task_local_data = data;
678+
return &task->task_local_data;
683679
}
684680
extern "C" void
685681
rust_task_local_data_atexit(rust_task *task, void (*cleanup_fn)(void *data)) {

src/rt/rustrt.def.in

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,6 @@ rust_destroy_little_lock
171171
rust_lock_little_lock
172172
rust_unlock_little_lock
173173
rust_get_task_local_data
174-
rust_set_task_local_data
175174
rust_task_local_data_atexit
176175
rust_task_ref
177176
rust_task_deref

0 commit comments

Comments
 (0)