13
13
use cast;
14
14
use libc;
15
15
use local_data;
16
- use managed:: raw:: BoxRepr ;
17
16
use prelude:: * ;
18
17
use ptr;
19
18
use sys;
20
19
use task:: rt;
21
- use unstable:: intrinsics;
22
20
use util;
23
21
24
22
use super :: rt:: rust_task;
@@ -50,15 +48,15 @@ impl Handle {
50
48
trait LocalData { }
51
49
impl < T : ' static > LocalData for T { }
52
50
53
- // The task-local-map actuall stores all TLS information. Right now it's a list
51
+ // The task-local-map actually stores all TLS information. Right now it's a list
54
52
// of triples of (key, value, loans). The key is a code pointer (right now at
55
53
// least), the value is a trait so destruction can work, and the loans value
56
54
// is a count of the number of times the value is currently on loan via
57
55
// `local_data_get`.
58
56
//
59
57
// TLS is designed to be able to store owned data, so `local_data_get` must
60
58
// return a borrowed pointer to this data. In order to have a proper lifetime, a
61
- // borrowed pointer is insted yielded to a closure specified to the `get`
59
+ // borrowed pointer is instead yielded to a closure specified to the `get`
62
60
// function. As a result, it would be unsound to perform `local_data_set` on the
63
61
// same key inside of a `local_data_get`, so we ensure at runtime that this does
64
62
// not happen.
@@ -68,7 +66,7 @@ impl<T: 'static> LocalData for T {}
68
66
// n.b. If TLS is used heavily in future, this could be made more efficient with
69
67
// a proper map.
70
68
type TaskLocalMap = ~[ Option < ( * libc:: c_void , TLSValue , uint ) > ] ;
71
- type TLSValue = @ LocalData ;
69
+ type TLSValue = ~ LocalData : ;
72
70
73
71
fn cleanup_task_local_map ( map_ptr : * libc:: c_void ) {
74
72
unsafe {
@@ -136,28 +134,8 @@ unsafe fn key_to_key_value<T: 'static>(key: local_data::Key<T>) -> *libc::c_void
136
134
return pair. code as * libc:: c_void ;
137
135
}
138
136
139
- unsafe fn transmute_back < ' a , T > ( data : & ' a TLSValue ) -> ( * BoxRepr , & ' a T ) {
140
- // Currently, a TLSValue is an '@Trait' instance which means that its actual
141
- // representation is a pair of (vtable, box). Also, because of issue #7673
142
- // the box actually points to another box which has the data. Hence, to get
143
- // a pointer to the actual value that we're interested in, we decode the
144
- // trait pointer and pass through one layer of boxes to get to the actual
145
- // data we're interested in.
146
- //
147
- // The reference count of the containing @Trait box is already taken care of
148
- // because the TLSValue is owned by the containing TLS map which means that
149
- // the reference count is at least one. Extra protections are then added at
150
- // runtime to ensure that once a loan on a value in TLS has been given out,
151
- // the value isn't modified by another user.
152
- let ( _vt, box) = * cast:: transmute :: < & TLSValue , & ( uint , * BoxRepr ) > ( data) ;
153
-
154
- return ( box, cast:: transmute ( & ( * box) . data ) ) ;
155
- }
156
-
157
137
pub unsafe fn local_pop < T : ' static > ( handle : Handle ,
158
138
key : local_data:: Key < T > ) -> Option < T > {
159
- // If you've never seen horrendously unsafe code written in rust before,
160
- // just feel free to look a bit farther...
161
139
let map = get_local_map ( handle) ;
162
140
let key_value = key_to_key_value ( key) ;
163
141
@@ -175,25 +153,23 @@ pub unsafe fn local_pop<T: 'static>(handle: Handle,
175
153
None => libc:: abort ( ) ,
176
154
} ;
177
155
178
- // First, via some various cheats/hacks, we extract the value
179
- // contained within the TLS box. This leaves a big chunk of
180
- // memory which needs to be deallocated now.
181
- let ( chunk, inside) = transmute_back ( & data) ;
182
- let inside = cast:: transmute_mut ( inside) ;
183
- let ret = ptr:: read_ptr ( inside) ;
156
+ // Move `data` into transmute to get out the memory that it
157
+ // owns, we must free it manually later.
158
+ let ( _vtable, box) : ( uint , ~~T ) = cast:: transmute ( data) ;
159
+
160
+ // Read the box's value (using the compiler's built-in
161
+ // auto-deref functionality to obtain a pointer to the base)
162
+ let ret = ptr:: read_ptr ( cast:: transmute :: < & T , * mut T > ( * box) ) ;
184
163
185
- // Forget the trait box because we're about to manually
186
- // deallocate the other box. And for my next trick (kids don't
187
- // try this at home), transmute the chunk of @ memory from the
188
- // @-trait box to a pointer to a zero-sized '@' block which will
189
- // then cause it to get properly deallocated, but it won't touch
190
- // any of the uninitialized memory beyond the end.
191
- cast:: forget ( data) ;
192
- let chunk: * mut BoxRepr = cast:: transmute ( chunk) ;
193
- ( * chunk) . header . type_desc =
194
- cast:: transmute ( intrinsics:: get_tydesc :: < ( ) > ( ) ) ;
195
- let _: @( ) = cast:: transmute ( chunk) ;
164
+ // Finally free the allocated memory. we don't want this to
165
+ // actually touch the memory inside because it's all duplicated
166
+ // now, so the box is transmuted to a 0-sized type. We also use
167
+ // a type which references `T` because currently the layout
168
+ // could depend on whether T contains managed pointers or not.
169
+ let _: ~~[ T , ..0 ] = cast:: transmute ( box) ;
196
170
171
+ // Everything is now deallocated, and we own the value that was
172
+ // located inside TLS, so we now return it.
197
173
return Some ( ret) ;
198
174
}
199
175
_ => { }
@@ -213,9 +189,17 @@ pub unsafe fn local_get<T: 'static, U>(handle: Handle,
213
189
for map. mut_iter( ) . advance |entry| {
214
190
match * entry {
215
191
Some ( ( k, ref data, ref mut loans) ) if k == key_value => {
192
+ let ret;
216
193
* loans = * loans + 1 ;
217
- let ( _, val) = transmute_back( data) ;
218
- let ret = f( Some ( val) ) ;
194
+ // data was created with `~~T as ~LocalData`, so we extract
195
+ // pointer part of the trait, (as ~~T), and then use compiler
196
+ // coercions to achieve a '&' pointer
197
+ match * cast:: transmute :: < & TLSValue , & ( uint , ~~T ) > ( data) {
198
+ ( _vtable, ref box) => {
199
+ let value: & T = * * box;
200
+ ret = f ( Some ( value) ) ;
201
+ }
202
+ }
219
203
* loans = * loans - 1 ;
220
204
return ret;
221
205
}
@@ -225,44 +209,46 @@ pub unsafe fn local_get<T: 'static, U>(handle: Handle,
225
209
return f ( None ) ;
226
210
}
227
211
228
- // FIXME(#7673): This shouldn't require '@', it should use '~'
229
212
pub unsafe fn local_set < T : ' static > ( handle : Handle ,
230
- key: local_data:: Key < @ T > ,
231
- data: @ T ) {
213
+ key : local_data:: Key < T > ,
214
+ data : T ) {
232
215
let map = get_local_map ( handle) ;
233
216
let keyval = key_to_key_value ( key) ;
234
217
235
218
// When the task-local map is destroyed, all the data needs to be cleaned
236
- // up. For this reason we can't do some clever tricks to store '@ T' as a
219
+ // up. For this reason we can't do some clever tricks to store '~ T' as a
237
220
// '*c_void' or something like that. To solve the problem, we cast
238
221
// everything to a trait (LocalData) which is then stored inside the map.
239
222
// Upon destruction of the map, all the objects will be destroyed and the
240
223
// traits have enough information about them to destroy themselves.
241
- let data = @data as @LocalData ;
242
-
243
- // First, try to insert it if we already have it.
244
- for map. mut_iter( ) . advance |entry| {
245
- match * entry {
246
- Some ( ( key, ref mut value, loans) ) if key == keyval => {
247
- if loans != 0 {
248
- fail ! ( "TLS value has been loaned via get already" ) ;
224
+ //
225
+ // FIXME(#7673): This should be "~data as ~LocalData" (without the colon at
226
+ // the end, and only one sigil)
227
+ let data = ~~data as ~LocalData : ;
228
+
229
+ fn insertion_position ( map : & mut TaskLocalMap ,
230
+ key : * libc:: c_void ) -> Option < uint > {
231
+ // First see if the map contains this key already
232
+ let curspot = map. iter ( ) . position ( |entry| {
233
+ match * entry {
234
+ Some ( ( ekey, _, loans) ) if key == ekey => {
235
+ if loans != 0 {
236
+ fail ! ( "TLS value has been loaned via get already" ) ;
237
+ }
238
+ true
249
239
}
250
- util:: replace( value, data) ;
251
- return ;
240
+ _ => false ,
252
241
}
253
- _ => { }
242
+ } ) ;
243
+ // If it doesn't contain the key, just find a slot that's None
244
+ match curspot {
245
+ Some ( i) => Some ( i) ,
246
+ None => map. iter ( ) . position ( |entry| entry. is_none ( ) )
254
247
}
255
248
}
256
- // Next, search for an open spot
257
- for map. mut_iter( ) . advance |entry| {
258
- match * entry {
259
- Some( * ) => { }
260
- None => {
261
- * entry = Some ( ( keyval, data, 0 ) ) ;
262
- return ;
263
- }
264
- }
249
+
250
+ match insertion_position ( map, keyval) {
251
+ Some ( i) => { map[ i] = Some ( ( keyval, data, 0 ) ) ; }
252
+ None => { map. push ( Some ( ( keyval, data, 0 ) ) ) ; }
265
253
}
266
- // Finally push it on the end of the list
267
- map. push( Some ( ( keyval, data, 0 ) ) ) ;
268
254
}
0 commit comments