Skip to content

Commit e3211fa

Browse files
committed
Purge the last remnants of the old TLS api
Closes #3273
1 parent 242606c commit e3211fa

File tree

15 files changed

+291
-293
lines changed

15 files changed

+291
-293
lines changed

src/libextra/rl.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,24 +66,28 @@ pub unsafe fn read(prompt: &str) -> Option<~str> {
6666
}
6767
}
6868

69-
pub type CompletionCb<'self> = @fn(~str, &'self fn(~str));
69+
pub type CompletionCb = @fn(~str, @fn(~str));
7070

71-
fn complete_key(_v: @CompletionCb) {}
71+
#[cfg(not(stage0))]
72+
static complete_key: local_data::Key<@CompletionCb> = &[];
73+
#[cfg(stage0)]
74+
fn complete_key(_: @CompletionCb) {}
7275

7376
/// Bind to the main completion callback
7477
pub unsafe fn complete(cb: CompletionCb) {
75-
local_data::set(complete_key, @(cb));
78+
local_data::set(complete_key, @cb);
7679

7780
extern fn callback(line: *c_char, completions: *()) {
78-
unsafe {
79-
let cb = *local_data::get(complete_key, |k| k.map(|&k| *k))
80-
.get();
81-
82-
do cb(str::raw::from_c_str(line)) |suggestion| {
83-
do str::as_c_str(suggestion) |buf| {
84-
rustrt::linenoiseAddCompletion(completions, buf);
81+
do local_data::get(complete_key) |cb| {
82+
let cb = **cb.unwrap();
83+
84+
unsafe {
85+
do cb(str::raw::from_c_str(line)) |suggestion| {
86+
do str::as_c_str(suggestion) |buf| {
87+
rustrt::linenoiseAddCompletion(completions, buf);
88+
}
8589
}
86-
}
90+
}
8791
}
8892
}
8993

src/libextra/sort.rs

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,39 +1195,4 @@ mod big_tests {
11951195
isSorted(arr);
11961196
}
11971197
}
1198-
1199-
struct LVal<'self> {
1200-
val: uint,
1201-
key: &'self fn:Copy(@uint),
1202-
}
1203-
1204-
#[unsafe_destructor]
1205-
impl<'self> Drop for LVal<'self> {
1206-
fn drop(&self) {
1207-
let x = unsafe { local_data::get(self.key, |k| k.map(|&k| *k)) };
1208-
match x {
1209-
Some(@y) => {
1210-
unsafe {
1211-
local_data::set(self.key, @(y+1));
1212-
}
1213-
}
1214-
_ => fail!("Expected key to work"),
1215-
}
1216-
}
1217-
}
1218-
1219-
impl<'self> Ord for LVal<'self> {
1220-
fn lt<'a>(&self, other: &'a LVal<'self>) -> bool {
1221-
(*self).val < other.val
1222-
}
1223-
fn le<'a>(&self, other: &'a LVal<'self>) -> bool {
1224-
(*self).val <= other.val
1225-
}
1226-
fn gt<'a>(&self, other: &'a LVal<'self>) -> bool {
1227-
(*self).val > other.val
1228-
}
1229-
fn ge<'a>(&self, other: &'a LVal<'self>) -> bool {
1230-
(*self).val >= other.val
1231-
}
1232-
}
12331198
}

src/librustc/middle/trans/base.rs

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -87,49 +87,44 @@ use syntax::abi::{X86, X86_64, Arm, Mips};
8787

8888
pub use middle::trans::context::task_llcx;
8989

90-
fn task_local_insn_key(_v: @~[&'static str]) {}
90+
#[cfg(not(stage0))]
91+
static task_local_insn_key: local_data::Key<@~[&'static str]> = &[];
92+
#[cfg(stage0)]
93+
fn task_local_insn_key(_: @~[&'static str]) {}
9194

9295
pub fn with_insn_ctxt(blk: &fn(&[&'static str])) {
93-
unsafe {
94-
let opt = local_data::get(task_local_insn_key, |k| k.map(|&k| *k));
95-
if opt.is_some() {
96-
blk(*opt.unwrap());
97-
}
96+
let opt = local_data::get(task_local_insn_key, |k| k.map(|&k| *k));
97+
if opt.is_some() {
98+
blk(*opt.unwrap());
9899
}
99100
}
100101

101102
pub fn init_insn_ctxt() {
102-
unsafe {
103-
local_data::set(task_local_insn_key, @~[]);
104-
}
103+
local_data::set(task_local_insn_key, @~[]);
105104
}
106105

107106
pub struct _InsnCtxt { _x: () }
108107

109108
#[unsafe_destructor]
110109
impl Drop for _InsnCtxt {
111110
fn drop(&self) {
112-
unsafe {
113-
do local_data::modify(task_local_insn_key) |c| {
114-
do c.map_consume |ctx| {
115-
let mut ctx = copy *ctx;
116-
ctx.pop();
117-
@ctx
118-
}
111+
do local_data::modify(task_local_insn_key) |c| {
112+
do c.map_consume |ctx| {
113+
let mut ctx = copy *ctx;
114+
ctx.pop();
115+
@ctx
119116
}
120117
}
121118
}
122119
}
123120

124121
pub fn push_ctxt(s: &'static str) -> _InsnCtxt {
125122
debug!("new InsnCtxt: %s", s);
126-
unsafe {
127-
do local_data::modify(task_local_insn_key) |c| {
128-
do c.map_consume |ctx| {
129-
let mut ctx = copy *ctx;
130-
ctx.push(s);
131-
@ctx
132-
}
123+
do local_data::modify(task_local_insn_key) |c| {
124+
do c.map_consume |ctx| {
125+
let mut ctx = copy *ctx;
126+
ctx.push(s);
127+
@ctx
133128
}
134129
}
135130
_InsnCtxt { _x: () }

src/librustc/middle/trans/context.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -232,22 +232,24 @@ impl CrateContext {
232232
#[unsafe_destructor]
233233
impl Drop for CrateContext {
234234
fn drop(&self) {
235-
unsafe {
236-
unset_task_llcx();
237-
}
235+
unset_task_llcx();
238236
}
239237
}
240238

239+
#[cfg(stage0)]
241240
fn task_local_llcx_key(_v: @ContextRef) {}
241+
#[cfg(not(stage0))]
242+
static task_local_llcx_key: local_data::Key<@ContextRef> = &[];
243+
242244
pub fn task_llcx() -> ContextRef {
243-
let opt = unsafe { local_data::get(task_local_llcx_key, |k| k.map(|&k| *k)) };
245+
let opt = local_data::get(task_local_llcx_key, |k| k.map(|&k| *k));
244246
*opt.expect("task-local LLVMContextRef wasn't ever set!")
245247
}
246248

247-
unsafe fn set_task_llcx(c: ContextRef) {
249+
fn set_task_llcx(c: ContextRef) {
248250
local_data::set(task_local_llcx_key, @c);
249251
}
250252

251-
unsafe fn unset_task_llcx() {
253+
fn unset_task_llcx() {
252254
local_data::pop(task_local_llcx_key);
253255
}

src/librusti/program.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use std::cast;
1211
use std::hashmap::HashMap;
1312
use std::local_data;
14-
use std::sys;
13+
use std::vec;
1514

1615
use syntax::ast;
1716
use syntax::parse::token;
@@ -58,7 +57,7 @@ struct LocalVariable {
5857
}
5958

6059
type LocalCache = @mut HashMap<~str, @~[u8]>;
61-
fn tls_key(_k: LocalCache) {}
60+
static tls_key: local_data::Key<LocalCache> = &[];
6261

6362
impl Program {
6463
pub fn new() -> Program {
@@ -131,21 +130,18 @@ impl Program {
131130
fn main() {
132131
");
133132

134-
let key: sys::Closure = unsafe {
135-
let tls_key: &'static fn(LocalCache) = tls_key;
136-
cast::transmute(tls_key)
137-
};
133+
let key: *LocalCache = vec::raw::to_ptr(tls_key);
138134
// First, get a handle to the tls map which stores all the local
139135
// variables. This works by totally legitimately using the 'code'
140136
// pointer of the 'tls_key' function as a uint, and then casting it back
141137
// up to a function
142138
code.push_str(fmt!("
143139
let __tls_map: @mut ::std::hashmap::HashMap<~str, @~[u8]> = unsafe {
144-
let key = ::std::sys::Closure{ code: %? as *(),
145-
env: ::std::ptr::null() };
140+
let key = ::std::vec::raw::SliceRepr{ data: %? as *u8,
141+
len: 0 };
146142
let key = ::std::cast::transmute(key);
147143
::std::local_data::get(key, |k| k.map(|&x| *x)).unwrap()
148-
};\n", key.code as uint));
144+
};\n", key as uint));
149145

150146
// Using this __tls_map handle, deserialize each variable binding that
151147
// we know about
@@ -226,18 +222,14 @@ impl Program {
226222
for self.local_vars.iter().advance |(name, value)| {
227223
map.insert(copy *name, @copy value.data);
228224
}
229-
unsafe {
230-
local_data::set(tls_key, map);
231-
}
225+
local_data::set(tls_key, map);
232226
}
233227

234228
/// Once the program has finished running, this function will consume the
235229
/// task-local cache of local variables. After the program finishes running,
236230
/// it updates this cache with the new values of each local variable.
237231
pub fn consume_cache(&mut self) {
238-
let map = unsafe {
239-
local_data::pop(tls_key).expect("tls is empty")
240-
};
232+
let map = local_data::pop(tls_key).expect("tls is empty");
241233
do map.consume |name, value| {
242234
match self.local_vars.find_mut(&name) {
243235
Some(v) => { v.data = copy *value; }

src/libstd/condition.rs

Lines changed: 68 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,59 @@ pub struct Handler<T, U> {
2323
prev: Option<@Handler<T, U>>,
2424
}
2525

26+
#[cfg(stage0)]
2627
pub struct Condition<'self, T, U> {
2728
name: &'static str,
2829
key: local_data::Key<'self, @Handler<T, U>>
2930
}
31+
#[cfg(not(stage0))]
32+
pub struct Condition<T, U> {
33+
name: &'static str,
34+
key: local_data::Key<@Handler<T, U>>
35+
}
36+
37+
#[cfg(not(stage0))]
38+
impl<T, U> Condition<T, U> {
39+
pub fn trap<'a>(&'a self, h: &'a fn(T) -> U) -> Trap<'a, T, U> {
40+
unsafe {
41+
let p : *RustClosure = ::cast::transmute(&h);
42+
let prev = local_data::get(self.key, |k| k.map(|&x| *x));
43+
let h = @Handler { handle: *p, prev: prev };
44+
Trap { cond: self, handler: h }
45+
}
46+
}
47+
48+
pub fn raise(&self, t: T) -> U {
49+
let msg = fmt!("Unhandled condition: %s: %?", self.name, t);
50+
self.raise_default(t, || fail!(copy msg))
51+
}
3052

53+
pub fn raise_default(&self, t: T, default: &fn() -> U) -> U {
54+
unsafe {
55+
match local_data::pop(self.key) {
56+
None => {
57+
debug!("Condition.raise: found no handler");
58+
default()
59+
}
60+
Some(handler) => {
61+
debug!("Condition.raise: found handler");
62+
match handler.prev {
63+
None => {}
64+
Some(hp) => local_data::set(self.key, hp)
65+
}
66+
let handle : &fn(T) -> U =
67+
::cast::transmute(handler.handle);
68+
let u = handle(t);
69+
local_data::set(self.key, handler);
70+
u
71+
}
72+
}
73+
}
74+
}
75+
}
76+
#[cfg(stage0)]
3177
impl<'self, T, U> Condition<'self, T, U> {
32-
pub fn trap(&'self self, h: &'self fn(T) -> U) -> Trap<'self, T, U> {
78+
pub fn trap<'a>(&'a self, h: &'a fn(T) -> U) -> Trap<'a, T, U> {
3379
unsafe {
3480
let p : *RustClosure = ::cast::transmute(&h);
3581
let prev = local_data::get(self.key, |k| k.map(|&x| *x));
@@ -67,38 +113,45 @@ impl<'self, T, U> Condition<'self, T, U> {
67113
}
68114
}
69115

116+
#[cfg(stage0)]
70117
struct Trap<'self, T, U> {
71118
cond: &'self Condition<'self, T, U>,
72119
handler: @Handler<T, U>
73120
}
121+
#[cfg(not(stage0))]
122+
struct Trap<'self, T, U> {
123+
cond: &'self Condition<T, U>,
124+
handler: @Handler<T, U>
125+
}
74126

75127
impl<'self, T, U> Trap<'self, T, U> {
76128
pub fn in<V>(&self, inner: &'self fn() -> V) -> V {
77-
unsafe {
78-
let _g = Guard { cond: self.cond };
79-
debug!("Trap: pushing handler to TLS");
80-
local_data::set(self.cond.key, self.handler);
81-
inner()
82-
}
129+
let _g = Guard { cond: self.cond };
130+
debug!("Trap: pushing handler to TLS");
131+
local_data::set(self.cond.key, self.handler);
132+
inner()
83133
}
84134
}
85135

136+
#[cfg(stage0)]
86137
struct Guard<'self, T, U> {
87138
cond: &'self Condition<'self, T, U>
88139
}
140+
#[cfg(not(stage0))]
141+
struct Guard<'self, T, U> {
142+
cond: &'self Condition<T, U>
143+
}
89144

90145
#[unsafe_destructor]
91146
impl<'self, T, U> Drop for Guard<'self, T, U> {
92147
fn drop(&self) {
93-
unsafe {
94-
debug!("Guard: popping handler from TLS");
95-
let curr = local_data::pop(self.cond.key);
96-
match curr {
148+
debug!("Guard: popping handler from TLS");
149+
let curr = local_data::pop(self.cond.key);
150+
match curr {
151+
None => {}
152+
Some(h) => match h.prev {
97153
None => {}
98-
Some(h) => match h.prev {
99-
None => {}
100-
Some(hp) => local_data::set(self.cond.key, hp)
101-
}
154+
Some(hp) => local_data::set(self.cond.key, hp)
102155
}
103156
}
104157
}

0 commit comments

Comments
 (0)