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
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
// 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.
/*!
Task local data management
Allows storing boxes with arbitrary types inside, to be accessed anywhere within
a task, keyed by a pointer to a global finaliser function. Useful for dynamic
variables, singletons, and interfacing with foreign code with bad callback
interfaces.
To use, declare a monomorphic (no type parameters) global function at the type
to store, and use it as the 'key' when accessing.
~~~{.rust}
use std::local_data;
fn key_int(_: @int) {}
fn key_vector(_: @~[int]) {}
unsafe {
local_data::set(key_int, @3);
assert!(local_data::get(key_int) == Some(@3));
local_data::set(key_vector, @~[3]);
assert!(local_data::get(key_vector).unwrap()[0] == 3);
}
~~~
Casting 'Arcane Sight' reveals an overwhelming aura of Transmutation
magic.
*/
use prelude::*;
use task::local_data_priv::{local_get, local_pop, local_set, Handle};
#[cfg(test)] use task;
/**
* Indexes a task-local data slot. The function's code pointer is used for
* comparison. Recommended use is to write an empty function for each desired
* task-local data slot (and use class destructors, not code inside the
* function, if specific teardown is needed). DO NOT use multiple
* instantiations of a single polymorphic function to index data of different
* types; arbitrary type coercion is possible this way.
*
* One other exception is that this global state can be used in a destructor
* context to create a circular @-box reference, which will crash during task
* failure (see issue #3039).
*
* These two cases aside, the interface is safe.
*/
pub type Key<'self,T> = &'self fn:Copy(v: T);
/**
* Remove a task-local data value from the table, returning the
* reference that was originally created to insert it.
*/
#[cfg(stage0)]
pub unsafe fn pop<T: 'static>(key: Key<@T>) -> Option<@T> {
local_pop(Handle::new(), key)
}
/**
* Remove a task-local data value from the table, returning the
* reference that was originally created to insert it.
*/
#[cfg(not(stage0))]
pub unsafe fn pop<T: 'static>(key: Key<T>) -> Option<T> {
local_pop(Handle::new(), key)
}
/**
* Retrieve a task-local data value. It will also be kept alive in the
* table until explicitly removed.
*/
#[cfg(stage0)]
pub unsafe fn get<T: 'static, U>(key: Key<@T>, f: &fn(Option<&@T>) -> U) -> U {
local_get(Handle::new(), key, f)
}
/**
* Retrieve a task-local data value. It will also be kept alive in the
* table until explicitly removed.
*/
#[cfg(not(stage0))]
pub unsafe fn get<T: 'static, U>(key: Key<T>, f: &fn(Option<&T>) -> U) -> U {
local_get(Handle::new(), key, f)
}
/**
* Store a value in task-local data. If this key already has a value,
* that value is overwritten (and its destructor is run).
*/
#[cfg(stage0)]
pub unsafe fn set<T: 'static>(key: Key<@T>, data: @T) {
local_set(Handle::new(), key, data)
}
/**
* 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))]
pub unsafe fn set<T: 'static>(key: Key<T>, data: T) {
local_set(Handle::new(), key, data)
}
/**
* Modify a task-local data value. If the function returns 'None', the
* data is removed (and its reference dropped).
*/
#[cfg(stage0)]
pub unsafe fn modify<T: 'static>(key: Key<@T>,
f: &fn(Option<@T>) -> Option<@T>) {
match f(pop(key)) {
Some(next) => { set(key, next); }
None => {}
}
}
/**
* Modify a task-local data value. If the function returns 'None', the
* data is removed (and its reference dropped).
*/
#[cfg(not(stage0))]
pub unsafe fn modify<T: 'static>(key: Key<T>,
f: &fn(Option<T>) -> Option<T>) {
match f(pop(key)) {
Some(next) => { set(key, next); }
None => {}
}
}
#[test]
fn test_tls_multitask() {
unsafe {
fn my_key(_x: @~str) { }
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
}
// 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");
}
}
#[test]
fn test_tls_overwrite() {
unsafe {
fn my_key(_x: @~str) { }
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");
}
}
#[test]
fn test_tls_pop() {
unsafe {
fn my_key(_x: @~str) { }
set(my_key, @~"weasel");
assert!(*(pop(my_key).get()) == ~"weasel");
// Pop must remove the data from the map.
assert!(pop(my_key).is_none());
}
}
#[test]
fn test_tls_modify() {
unsafe {
fn my_key(_x: @~str) { }
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");
}
}
#[test]
fn test_tls_crust_automorestack_memorial_bug() {
// 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.
fn my_key(_x: @~str) { }
do task::spawn {
unsafe { set(my_key, @~"hax"); }
}
}
#[test]
fn test_tls_multiple_types() {
fn str_key(_x: @~str) { }
fn box_key(_x: @@()) { }
fn int_key(_x: @int) { }
do task::spawn {
unsafe {
set(str_key, @~"string data");
set(box_key, @@());
set(int_key, @42);
}
}
}
#[test]
fn test_tls_overwrite_multiple_types() {
fn str_key(_x: @~str) { }
fn box_key(_x: @@()) { }
fn int_key(_x: @int) { }
do task::spawn {
unsafe {
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);
}
}
}
#[test]
#[should_fail]
#[ignore(cfg(windows))]
fn test_tls_cleanup_on_failure() {
unsafe {
fn str_key(_x: @~str) { }
fn box_key(_x: @@()) { }
fn int_key(_x: @int) { }
set(str_key, @~"parent data");
set(box_key, @@());
do task::spawn {
// spawn_linked
set(str_key, @~"string data");
set(box_key, @@());
set(int_key, @42);
fail!();
}
// Not quite nondeterministic.
set(int_key, @31337);
fail!();
}
}
#[test]
fn test_static_pointer() {
unsafe {
fn key(_x: @&'static int) { }
static VALUE: int = 0;
set(key, @&VALUE);
}
}
#[test]
fn test_owned() {
unsafe {
fn key(_x: ~int) { }
set(key, ~1);
}
}
|