]> git.lizzy.rs Git - rust.git/blob - src/libstd/task/local_data_priv.rs
Add externfn macro and correctly label fixed_stack_segments
[rust.git] / src / libstd / task / local_data_priv.rs
1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 #[allow(missing_doc)];
12
13 use cast;
14 use libc;
15 use local_data;
16 use prelude::*;
17 use ptr;
18 use unstable::raw;
19 use util;
20
21 use rt::task::{Task, LocalStorage};
22
23 pub enum Handle {
24     NewHandle(*mut LocalStorage)
25 }
26
27 impl Handle {
28     pub fn new() -> Handle {
29         use rt::local::Local;
30         unsafe {
31             let task = Local::unsafe_borrow::<Task>();
32             NewHandle(&mut (*task).storage)
33         }
34     }
35 }
36
37 #[deriving(Eq)]
38 enum LoanState {
39     NoLoan, ImmLoan, MutLoan
40 }
41
42 impl LoanState {
43     fn describe(&self) -> &'static str {
44         match *self {
45             NoLoan => "no loan",
46             ImmLoan => "immutable",
47             MutLoan => "mutable"
48         }
49     }
50 }
51
52 trait LocalData {}
53 impl<T: 'static> LocalData for T {}
54
55 // The task-local-map stores all TLS information for the currently running task.
56 // It is stored as an owned pointer into the runtime, and it's only allocated
57 // when TLS is used for the first time. This map must be very carefully
58 // constructed because it has many mutable loans unsoundly handed out on it to
59 // the various invocations of TLS requests.
60 //
61 // One of the most important operations is loaning a value via `get` to a
62 // caller. In doing so, the slot that the TLS entry is occupying cannot be
63 // invalidated because upon returning it's loan state must be updated. Currently
64 // the TLS map is a vector, but this is possibly dangerous because the vector
65 // can be reallocated/moved when new values are pushed onto it.
66 //
67 // This problem currently isn't solved in a very elegant way. Inside the `get`
68 // function, it internally "invalidates" all references after the loan is
69 // finished and looks up into the vector again. In theory this will prevent
70 // pointers from being moved under our feet so long as LLVM doesn't go too crazy
71 // with the optimizations.
72 //
73 // n.b. Other structures are not sufficient right now:
74 //          * HashMap uses ~[T] internally (push reallocates/moves)
75 //          * TreeMap is plausible, but it's in extra
76 //          * dlist plausible, but not in std
77 //          * a custom owned linked list was attempted, but difficult to write
78 //            and involved a lot of extra code bloat
79 //
80 // n.b. Has to be stored with a pointer at outermost layer; the foreign call
81 //      returns void *.
82 //
83 // n.b. If TLS is used heavily in future, this could be made more efficient with
84 //      a proper map.
85 type TaskLocalMap = ~[Option<(*libc::c_void, TLSValue, LoanState)>];
86 type TLSValue = ~LocalData:;
87
88 fn cleanup_task_local_map(map_ptr: *libc::c_void) {
89     unsafe {
90         assert!(!map_ptr.is_null());
91         // Get and keep the single reference that was created at the
92         // beginning.
93         let _map: TaskLocalMap = cast::transmute(map_ptr);
94         // All local_data will be destroyed along with the map.
95     }
96 }
97
98 // Gets the map from the runtime. Lazily initialises if not done so already.
99 unsafe fn get_local_map(handle: Handle) -> &mut TaskLocalMap {
100
101     unsafe fn newsched_map(local: *mut LocalStorage) -> &mut TaskLocalMap {
102         // This is based on the same idea as the oldsched code above.
103         match &mut *local {
104             // If the at_exit function is already set, then we just need to take
105             // a loan out on the TLS map stored inside
106             &LocalStorage(ref mut map_ptr, Some(_)) => {
107                 assert!(map_ptr.is_not_null());
108                 return cast::transmute(map_ptr);
109             }
110             // If this is the first time we've accessed TLS, perform similar
111             // actions to the oldsched way of doing things.
112             &LocalStorage(ref mut map_ptr, ref mut at_exit) => {
113                 assert!(map_ptr.is_null());
114                 assert!(at_exit.is_none());
115                 let map: TaskLocalMap = ~[];
116                 *map_ptr = cast::transmute(map);
117                 *at_exit = Some(cleanup_task_local_map);
118                 return cast::transmute(map_ptr);
119             }
120         }
121     }
122
123     match handle {
124         NewHandle(local_storage) => newsched_map(local_storage)
125     }
126 }
127
128 unsafe fn key_to_key_value<T: 'static>(key: local_data::Key<T>) -> *libc::c_void {
129     let pair: raw::Closure = cast::transmute_copy(&key);
130     return pair.code as *libc::c_void;
131 }
132
133 pub unsafe fn local_pop<T: 'static>(handle: Handle,
134                                     key: local_data::Key<T>) -> Option<T> {
135     let map = get_local_map(handle);
136     let key_value = key_to_key_value(key);
137
138     for entry in map.mut_iter() {
139         match *entry {
140             Some((k, _, loan)) if k == key_value => {
141                 if loan != NoLoan {
142                     fail!("TLS value cannot be removed because it is already \
143                           borrowed as %s", loan.describe());
144                 }
145                 // Move the data out of the `entry` slot via util::replace. This
146                 // is guaranteed to succeed because we already matched on `Some`
147                 // above.
148                 let data = match util::replace(entry, None) {
149                     Some((_, data, _)) => data,
150                     None => abort(),
151                 };
152
153                 // Move `data` into transmute to get out the memory that it
154                 // owns, we must free it manually later.
155                 let (_vtable, box): (uint, ~~T) = cast::transmute(data);
156
157                 // Read the box's value (using the compiler's built-in
158                 // auto-deref functionality to obtain a pointer to the base)
159                 let ret = ptr::read_ptr(cast::transmute::<&T, *mut T>(*box));
160
161                 // Finally free the allocated memory. we don't want this to
162                 // actually touch the memory inside because it's all duplicated
163                 // now, so the box is transmuted to a 0-sized type. We also use
164                 // a type which references `T` because currently the layout
165                 // could depend on whether T contains managed pointers or not.
166                 let _: ~~[T, ..0] = cast::transmute(box);
167
168                 // Everything is now deallocated, and we own the value that was
169                 // located inside TLS, so we now return it.
170                 return Some(ret);
171             }
172             _ => {}
173         }
174     }
175     return None;
176 }
177
178 pub unsafe fn local_get<T: 'static, U>(handle: Handle,
179                                        key: local_data::Key<T>,
180                                        f: &fn(Option<&T>) -> U) -> U {
181     local_get_with(handle, key, ImmLoan, f)
182 }
183
184 pub unsafe fn local_get_mut<T: 'static, U>(handle: Handle,
185                                            key: local_data::Key<T>,
186                                            f: &fn(Option<&mut T>) -> U) -> U {
187     do local_get_with(handle, key, MutLoan) |x| {
188         match x {
189             None => f(None),
190             // We're violating a lot of compiler guarantees with this
191             // invocation of `transmute_mut`, but we're doing runtime checks to
192             // ensure that it's always valid (only one at a time).
193             //
194             // there is no need to be upset!
195             Some(x) => { f(Some(cast::transmute_mut(x))) }
196         }
197     }
198 }
199
200 unsafe fn local_get_with<T: 'static, U>(handle: Handle,
201                                         key: local_data::Key<T>,
202                                         state: LoanState,
203                                         f: &fn(Option<&T>) -> U) -> U {
204     // This function must be extremely careful. Because TLS can store owned
205     // values, and we must have some form of `get` function other than `pop`,
206     // this function has to give a `&` reference back to the caller.
207     //
208     // One option is to return the reference, but this cannot be sound because
209     // the actual lifetime of the object is not known. The slot in TLS could not
210     // be modified until the object goes out of scope, but the TLS code cannot
211     // know when this happens.
212     //
213     // For this reason, the reference is yielded to a specified closure. This
214     // way the TLS code knows exactly what the lifetime of the yielded pointer
215     // is, allowing callers to acquire references to owned data. This is also
216     // sound so long as measures are taken to ensure that while a TLS slot is
217     // loaned out to a caller, it's not modified recursively.
218     let map = get_local_map(handle);
219     let key_value = key_to_key_value(key);
220
221     let pos = map.iter().position(|entry| {
222         match *entry {
223             Some((k, _, _)) if k == key_value => true, _ => false
224         }
225     });
226     match pos {
227         None => { return f(None); }
228         Some(i) => {
229             let ret;
230             let mut return_loan = false;
231             match map[i] {
232                 Some((_, ref data, ref mut loan)) => {
233                     match (state, *loan) {
234                         (_, NoLoan) => {
235                             *loan = state;
236                             return_loan = true;
237                         }
238                         (ImmLoan, ImmLoan) => {}
239                         (want, cur) => {
240                             fail!("TLS slot cannot be borrowed as %s because \
241                                    it is already borrowed as %s",
242                                   want.describe(), cur.describe());
243                         }
244                     }
245                     // data was created with `~~T as ~LocalData`, so we extract
246                     // pointer part of the trait, (as ~~T), and then use
247                     // compiler coercions to achieve a '&' pointer.
248                     match *cast::transmute::<&TLSValue, &(uint, ~~T)>(data) {
249                         (_vtable, ref box) => {
250                             let value: &T = **box;
251                             ret = f(Some(value));
252                         }
253                     }
254                 }
255                 _ => abort()
256             }
257
258             // n.b. 'data' and 'loans' are both invalid pointers at the point
259             // 'f' returned because `f` could have appended more TLS items which
260             // in turn relocated the vector. Hence we do another lookup here to
261             // fixup the loans.
262             if return_loan {
263                 match map[i] {
264                     Some((_, _, ref mut loan)) => { *loan = NoLoan; }
265                     None => { abort(); }
266                 }
267             }
268             return ret;
269         }
270     }
271 }
272
273 fn abort() -> ! {
274     #[fixed_stack_segment]; #[inline(never)];
275
276     unsafe { libc::abort() }
277 }
278
279 pub unsafe fn local_set<T: 'static>(handle: Handle,
280                                     key: local_data::Key<T>,
281                                     data: T) {
282     let map = get_local_map(handle);
283     let keyval = key_to_key_value(key);
284
285     // When the task-local map is destroyed, all the data needs to be cleaned
286     // up. For this reason we can't do some clever tricks to store '~T' as a
287     // '*c_void' or something like that. To solve the problem, we cast
288     // everything to a trait (LocalData) which is then stored inside the map.
289     // Upon destruction of the map, all the objects will be destroyed and the
290     // traits have enough information about them to destroy themselves.
291     //
292     // FIXME(#7673): This should be "~data as ~LocalData" (without the colon at
293     //               the end, and only one sigil)
294     let data = ~~data as ~LocalData:;
295
296     fn insertion_position(map: &mut TaskLocalMap,
297                           key: *libc::c_void) -> Option<uint> {
298         // First see if the map contains this key already
299         let curspot = map.iter().position(|entry| {
300             match *entry {
301                 Some((ekey, _, loan)) if key == ekey => {
302                     if loan != NoLoan {
303                         fail!("TLS value cannot be overwritten because it is
304                                already borrowed as %s", loan.describe())
305                     }
306                     true
307                 }
308                 _ => false,
309             }
310         });
311         // If it doesn't contain the key, just find a slot that's None
312         match curspot {
313             Some(i) => Some(i),
314             None => map.iter().position(|entry| entry.is_none())
315         }
316     }
317
318     match insertion_position(map, keyval) {
319         Some(i) => { map[i] = Some((keyval, data, NoLoan)); }
320         None => { map.push(Some((keyval, data, NoLoan))); }
321     }
322 }