]> git.lizzy.rs Git - rust.git/blob - src/libstd/local_data.rs
auto merge of #13173 : alexcrichton/rust/rustdoc-mods, r=huonw
[rust.git] / src / libstd / local_data.rs
1 // Copyright 2012-2013 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 /*!
12
13 Task local data management
14
15 Allows storing arbitrary types inside task-local-storage (TLS), to be accessed
16 anywhere within a task, keyed by a global pointer parameterized over the type of
17 the TLS slot.  Useful for dynamic variables, singletons, and interfacing with
18 foreign code with bad callback interfaces.
19
20 To declare a new key for storing local data of a particular type, use the
21 `local_data_key!` macro. This macro will expand to a `static` item appropriately
22 named and annotated. This name is then passed to the functions in this module to
23 modify/read the slot specified by the key.
24
25 ```rust
26 use std::local_data;
27
28 local_data_key!(key_int: int)
29 local_data_key!(key_vector: ~[int])
30
31 local_data::set(key_int, 3);
32 local_data::get(key_int, |opt| assert_eq!(opt.map(|x| *x), Some(3)));
33
34 local_data::set(key_vector, ~[4]);
35 local_data::get(key_vector, |opt| assert_eq!(*opt.unwrap(), ~[4]));
36  ```
37
38 */
39
40 // Casting 'Arcane Sight' reveals an overwhelming aura of Transmutation
41 // magic.
42
43 use cast;
44 use iter::{Iterator};
45 use kinds::Send;
46 use mem::replace;
47 use option::{None, Option, Some};
48 use rt::task::{Task, LocalStorage};
49 use slice::{ImmutableVector, MutableVector, OwnedVector};
50
51 /**
52  * Indexes a task-local data slot. This pointer is used for comparison to
53  * differentiate keys from one another. The actual type `T` is not used anywhere
54  * as a member of this type, except that it is parameterized with it to define
55  * the type of each key's value.
56  *
57  * The value of each Key is of the singleton enum KeyValue. These also have the
58  * same name as `Key` and their purpose is to take up space in the programs data
59  * sections to ensure that each value of the `Key` type points to a unique
60  * location.
61  */
62 pub type Key<T> = &'static KeyValue<T>;
63
64 #[allow(missing_doc)]
65 pub enum KeyValue<T> { Key }
66
67 #[allow(missing_doc)]
68 trait LocalData {}
69 impl<T: 'static> LocalData for T {}
70
71 // The task-local-map stores all TLS information for the currently running task.
72 // It is stored as an owned pointer into the runtime, and it's only allocated
73 // when TLS is used for the first time. This map must be very carefully
74 // constructed because it has many mutable loans unsoundly handed out on it to
75 // the various invocations of TLS requests.
76 //
77 // One of the most important operations is loaning a value via `get` to a
78 // caller. In doing so, the slot that the TLS entry is occupying cannot be
79 // invalidated because upon returning its loan state must be updated. Currently
80 // the TLS map is a vector, but this is possibly dangerous because the vector
81 // can be reallocated/moved when new values are pushed onto it.
82 //
83 // This problem currently isn't solved in a very elegant way. Inside the `get`
84 // function, it internally "invalidates" all references after the loan is
85 // finished and looks up into the vector again. In theory this will prevent
86 // pointers from being moved under our feet so long as LLVM doesn't go too crazy
87 // with the optimizations.
88 //
89 // n.b. If TLS is used heavily in future, this could be made more efficient with
90 //      a proper map.
91 #[doc(hidden)]
92 pub type Map = ~[Option<(*u8, TLSValue, LoanState)>];
93 type TLSValue = ~LocalData:Send;
94
95 // Gets the map from the runtime. Lazily initialises if not done so already.
96 unsafe fn get_local_map() -> &mut Map {
97     use rt::local::Local;
98
99     let task: *mut Task = Local::unsafe_borrow();
100     match &mut (*task).storage {
101         // If the at_exit function is already set, then we just need to take
102         // a loan out on the TLS map stored inside
103         &LocalStorage(Some(ref mut map_ptr)) => {
104             return map_ptr;
105         }
106         // If this is the first time we've accessed TLS, perform similar
107         // actions to the oldsched way of doing things.
108         &LocalStorage(ref mut slot) => {
109             *slot = Some(~[]);
110             match *slot {
111                 Some(ref mut map_ptr) => { return map_ptr }
112                 None => abort()
113             }
114         }
115     }
116 }
117
118 #[deriving(Eq)]
119 enum LoanState {
120     NoLoan, ImmLoan, MutLoan
121 }
122
123 impl LoanState {
124     fn describe(&self) -> &'static str {
125         match *self {
126             NoLoan => "no loan",
127             ImmLoan => "immutable",
128             MutLoan => "mutable"
129         }
130     }
131 }
132
133 fn key_to_key_value<T: 'static>(key: Key<T>) -> *u8 {
134     unsafe { cast::transmute(key) }
135 }
136
137 /// Removes a task-local value from task-local storage. This will return
138 /// Some(value) if the key was present in TLS, otherwise it will return None.
139 ///
140 /// A runtime assertion will be triggered it removal of TLS value is attempted
141 /// while the value is still loaned out via `get` or `get_mut`.
142 pub fn pop<T: 'static>(key: Key<T>) -> Option<T> {
143     let map = unsafe { get_local_map() };
144     let key_value = key_to_key_value(key);
145
146     for entry in map.mut_iter() {
147         match *entry {
148             Some((k, _, loan)) if k == key_value => {
149                 if loan != NoLoan {
150                     fail!("TLS value cannot be removed because it is currently \
151                           borrowed as {}", loan.describe());
152                 }
153                 // Move the data out of the `entry` slot via prelude::replace.
154                 // This is guaranteed to succeed because we already matched
155                 // on `Some` above.
156                 let data = match replace(entry, None) {
157                     Some((_, data, _)) => data,
158                     None => abort()
159                 };
160
161                 // Move `data` into transmute to get out the memory that it
162                 // owns, we must free it manually later.
163                 let (_vtable, alloc): (uint, ~T) = unsafe {
164                     cast::transmute(data)
165                 };
166
167                 // Now that we own `alloc`, we can just move out of it as we
168                 // would with any other data.
169                 return Some(*alloc);
170             }
171             _ => {}
172         }
173     }
174     return None;
175 }
176
177 /// Retrieves a value from TLS. The closure provided is yielded `Some` of a
178 /// reference to the value located in TLS if one exists, or `None` if the key
179 /// provided is not present in TLS currently.
180 ///
181 /// It is considered a runtime error to attempt to get a value which is already
182 /// on loan via the `get_mut` method provided.
183 pub fn get<T: 'static, U>(key: Key<T>, f: |Option<&T>| -> U) -> U {
184     get_with(key, ImmLoan, f)
185 }
186
187 /// Retrieves a mutable value from TLS. The closure provided is yielded `Some`
188 /// of a reference to the mutable value located in TLS if one exists, or `None`
189 /// if the key provided is not present in TLS currently.
190 ///
191 /// It is considered a runtime error to attempt to get a value which is already
192 /// on loan via this or the `get` methods.
193 pub fn get_mut<T: 'static, U>(key: Key<T>, f: |Option<&mut T>| -> U) -> U {
194     get_with(key, MutLoan, |x| {
195         match x {
196             None => f(None),
197             // We're violating a lot of compiler guarantees with this
198             // invocation of `transmute_mut`, but we're doing runtime checks to
199             // ensure that it's always valid (only one at a time).
200             //
201             // there is no need to be upset!
202             Some(x) => { f(Some(unsafe { cast::transmute_mut(x) })) }
203         }
204     })
205 }
206
207 fn get_with<T:'static,
208             U>(
209             key: Key<T>,
210             state: LoanState,
211             f: |Option<&T>| -> U)
212             -> U {
213     // This function must be extremely careful. Because TLS can store owned
214     // values, and we must have some form of `get` function other than `pop`,
215     // this function has to give a `&` reference back to the caller.
216     //
217     // One option is to return the reference, but this cannot be sound because
218     // the actual lifetime of the object is not known. The slot in TLS could not
219     // be modified until the object goes out of scope, but the TLS code cannot
220     // know when this happens.
221     //
222     // For this reason, the reference is yielded to a specified closure. This
223     // way the TLS code knows exactly what the lifetime of the yielded pointer
224     // is, allowing callers to acquire references to owned data. This is also
225     // sound so long as measures are taken to ensure that while a TLS slot is
226     // loaned out to a caller, it's not modified recursively.
227     let map = unsafe { get_local_map() };
228     let key_value = key_to_key_value(key);
229
230     let pos = map.iter().position(|entry| {
231         match *entry {
232             Some((k, _, _)) if k == key_value => true, _ => false
233         }
234     });
235     match pos {
236         None => { return f(None); }
237         Some(i) => {
238             let ret;
239             let mut return_loan = false;
240             match map[i] {
241                 Some((_, ref data, ref mut loan)) => {
242                     match (state, *loan) {
243                         (_, NoLoan) => {
244                             *loan = state;
245                             return_loan = true;
246                         }
247                         (ImmLoan, ImmLoan) => {}
248                         (want, cur) => {
249                             fail!("TLS slot cannot be borrowed as {} because \
250                                     it is already borrowed as {}",
251                                   want.describe(), cur.describe());
252                         }
253                     }
254                     // data was created with `~T as ~LocalData`, so we extract
255                     // pointer part of the trait, (as ~T), and then use
256                     // compiler coercions to achieve a '&' pointer.
257                     unsafe {
258                         match *cast::transmute::<&TLSValue, &(uint, ~T)>(data){
259                             (_vtable, ref alloc) => {
260                                 let value: &T = *alloc;
261                                 ret = f(Some(value));
262                             }
263                         }
264                     }
265                 }
266                 _ => abort()
267             }
268
269             // n.b. 'data' and 'loans' are both invalid pointers at the point
270             // 'f' returned because `f` could have appended more TLS items which
271             // in turn relocated the vector. Hence we do another lookup here to
272             // fixup the loans.
273             if return_loan {
274                 match map[i] {
275                     Some((_, _, ref mut loan)) => { *loan = NoLoan; }
276                     None => abort()
277                 }
278             }
279             return ret;
280         }
281     }
282 }
283
284 fn abort() -> ! {
285     use intrinsics;
286     unsafe { intrinsics::abort() }
287 }
288
289 /// Inserts a value into task local storage. If the key is already present in
290 /// TLS, then the previous value is removed and replaced with the provided data.
291 ///
292 /// It is considered a runtime error to attempt to set a key which is currently
293 /// on loan via the `get` or `get_mut` methods.
294 pub fn set<T: 'static>(key: Key<T>, data: T) {
295     let map = unsafe { get_local_map() };
296     let keyval = key_to_key_value(key);
297
298     // When the task-local map is destroyed, all the data needs to be cleaned
299     // up. For this reason we can't do some clever tricks to store '~T' as a
300     // '*c_void' or something like that. To solve the problem, we cast
301     // everything to a trait (LocalData) which is then stored inside the map.
302     // Upon destruction of the map, all the objects will be destroyed and the
303     // traits have enough information about them to destroy themselves.
304     let data = ~data as ~LocalData:;
305
306     fn insertion_position(map: &mut Map,
307                           key: *u8) -> Option<uint> {
308         // First see if the map contains this key already
309         let curspot = map.iter().position(|entry| {
310             match *entry {
311                 Some((ekey, _, loan)) if key == ekey => {
312                     if loan != NoLoan {
313                         fail!("TLS value cannot be overwritten because it is
314                                already borrowed as {}", loan.describe())
315                     }
316                     true
317                 }
318                 _ => false,
319             }
320         });
321         // If it doesn't contain the key, just find a slot that's None
322         match curspot {
323             Some(i) => Some(i),
324             None => map.iter().position(|entry| entry.is_none())
325         }
326     }
327
328     // The type of the local data map must ascribe to Send, so we do the
329     // transmute here to add the Send bound back on. This doesn't actually
330     // matter because TLS will always own the data (until its moved out) and
331     // we're not actually sending it to other schedulers or anything.
332     let data: ~LocalData:Send = unsafe { cast::transmute(data) };
333     match insertion_position(map, keyval) {
334         Some(i) => { map[i] = Some((keyval, data, NoLoan)); }
335         None => { map.push(Some((keyval, data, NoLoan))); }
336     }
337 }
338
339 /// Modifies a task-local value by temporarily removing it from task-local
340 /// storage and then re-inserting if `Some` is returned from the closure.
341 ///
342 /// This function will have the same runtime errors as generated from `pop` and
343 /// `set` (the key must not currently be on loan
344 pub fn modify<T: 'static>(key: Key<T>, f: |Option<T>| -> Option<T>) {
345     match f(pop(key)) {
346         Some(next) => { set(key, next); }
347         None => {}
348     }
349 }
350
351 #[cfg(test)]
352 mod tests {
353     use prelude::*;
354     use super::*;
355     use task;
356
357     #[test]
358     fn test_tls_multitask() {
359         static my_key: Key<~str> = &Key;
360         set(my_key, ~"parent data");
361         task::spawn(proc() {
362             // TLS shouldn't carry over.
363             assert!(get(my_key, |k| k.map(|k| (*k).clone())).is_none());
364             set(my_key, ~"child data");
365             assert!(get(my_key, |k| k.map(|k| (*k).clone())).unwrap() ==
366                     ~"child data");
367             // should be cleaned up for us
368         });
369         // Must work multiple times
370         assert!(get(my_key, |k| k.map(|k| (*k).clone())).unwrap() == ~"parent data");
371         assert!(get(my_key, |k| k.map(|k| (*k).clone())).unwrap() == ~"parent data");
372         assert!(get(my_key, |k| k.map(|k| (*k).clone())).unwrap() == ~"parent data");
373     }
374
375     #[test]
376     fn test_tls_overwrite() {
377         static my_key: Key<~str> = &Key;
378         set(my_key, ~"first data");
379         set(my_key, ~"next data"); // Shouldn't leak.
380         assert!(get(my_key, |k| k.map(|k| (*k).clone())).unwrap() == ~"next data");
381     }
382
383     #[test]
384     fn test_tls_pop() {
385         static my_key: Key<~str> = &Key;
386         set(my_key, ~"weasel");
387         assert!(pop(my_key).unwrap() == ~"weasel");
388         // Pop must remove the data from the map.
389         assert!(pop(my_key).is_none());
390     }
391
392     #[test]
393     fn test_tls_modify() {
394         static my_key: Key<~str> = &Key;
395         modify(my_key, |data| {
396             match data {
397                 Some(ref val) => fail!("unwelcome value: {}", *val),
398                 None           => Some(~"first data")
399             }
400         });
401         modify(my_key, |data| {
402             match data.as_ref().map(|s| s.as_slice()) {
403                 Some("first data") => Some(~"next data"),
404                 Some(ref val)       => fail!("wrong value: {}", *val),
405                 None                 => fail!("missing value")
406             }
407         });
408         assert!(pop(my_key).unwrap() == ~"next data");
409     }
410
411     #[test]
412     fn test_tls_crust_automorestack_memorial_bug() {
413         // This might result in a stack-canary clobber if the runtime fails to
414         // set sp_limit to 0 when calling the cleanup extern - it might
415         // automatically jump over to the rust stack, which causes next_c_sp
416         // to get recorded as something within a rust stack segment. Then a
417         // subsequent upcall (esp. for logging, think vsnprintf) would run on
418         // a stack smaller than 1 MB.
419         static my_key: Key<~str> = &Key;
420         task::spawn(proc() {
421             set(my_key, ~"hax");
422         });
423     }
424
425     #[test]
426     fn test_tls_multiple_types() {
427         static str_key: Key<~str> = &Key;
428         static box_key: Key<@()> = &Key;
429         static int_key: Key<int> = &Key;
430         task::spawn(proc() {
431             set(str_key, ~"string data");
432             set(box_key, @());
433             set(int_key, 42);
434         });
435     }
436
437     #[test]
438     #[allow(dead_code)]
439     fn test_tls_overwrite_multiple_types() {
440         static str_key: Key<~str> = &Key;
441         static box_key: Key<@()> = &Key;
442         static int_key: Key<int> = &Key;
443         task::spawn(proc() {
444             set(str_key, ~"string data");
445             set(str_key, ~"string data 2");
446             set(box_key, @());
447             set(box_key, @());
448             set(int_key, 42);
449             // This could cause a segfault if overwriting-destruction is done
450             // with the crazy polymorphic transmute rather than the provided
451             // finaliser.
452             set(int_key, 31337);
453         });
454     }
455
456     #[test]
457     #[should_fail]
458     fn test_tls_cleanup_on_failure() {
459         static str_key: Key<~str> = &Key;
460         static box_key: Key<@()> = &Key;
461         static int_key: Key<int> = &Key;
462         set(str_key, ~"parent data");
463         set(box_key, @());
464         task::spawn(proc() {
465             // spawn_linked
466             set(str_key, ~"string data");
467             set(box_key, @());
468             set(int_key, 42);
469             fail!();
470         });
471         // Not quite nondeterministic.
472         set(int_key, 31337);
473         fail!();
474     }
475
476     #[test]
477     fn test_static_pointer() {
478         static key: Key<&'static int> = &Key;
479         static VALUE: int = 0;
480         let v: &'static int = &VALUE;
481         set(key, v);
482     }
483
484     #[test]
485     fn test_owned() {
486         static key: Key<~int> = &Key;
487         set(key, ~1);
488
489         get(key, |v| {
490             get(key, |v| {
491                 get(key, |v| {
492                     assert_eq!(**v.unwrap(), 1);
493                 });
494                 assert_eq!(**v.unwrap(), 1);
495             });
496             assert_eq!(**v.unwrap(), 1);
497         });
498         set(key, ~2);
499         get(key, |v| {
500             assert_eq!(**v.unwrap(), 2);
501         })
502     }
503
504     #[test]
505     fn test_get_mut() {
506         static key: Key<int> = &Key;
507         set(key, 1);
508
509         get_mut(key, |v| {
510             *v.unwrap() = 2;
511         });
512
513         get(key, |v| {
514             assert_eq!(*v.unwrap(), 2);
515         })
516     }
517
518     #[test]
519     fn test_same_key_type() {
520         static key1: Key<int> = &Key;
521         static key2: Key<int> = &Key;
522         static key3: Key<int> = &Key;
523         static key4: Key<int> = &Key;
524         static key5: Key<int> = &Key;
525         set(key1, 1);
526         set(key2, 2);
527         set(key3, 3);
528         set(key4, 4);
529         set(key5, 5);
530
531         get(key1, |x| assert_eq!(*x.unwrap(), 1));
532         get(key2, |x| assert_eq!(*x.unwrap(), 2));
533         get(key3, |x| assert_eq!(*x.unwrap(), 3));
534         get(key4, |x| assert_eq!(*x.unwrap(), 4));
535         get(key5, |x| assert_eq!(*x.unwrap(), 5));
536     }
537
538     #[test]
539     #[should_fail]
540     fn test_nested_get_set1() {
541         static key: Key<int> = &Key;
542         set(key, 4);
543         get(key, |_| {
544             set(key, 4);
545         })
546     }
547
548     #[test]
549     #[should_fail]
550     fn test_nested_get_mut2() {
551         static key: Key<int> = &Key;
552         set(key, 4);
553         get(key, |_| {
554             get_mut(key, |_| {})
555         })
556     }
557
558     #[test]
559     #[should_fail]
560     fn test_nested_get_mut3() {
561         static key: Key<int> = &Key;
562         set(key, 4);
563         get_mut(key, |_| {
564             get(key, |_| {})
565         })
566     }
567
568     #[test]
569     #[should_fail]
570     fn test_nested_get_mut4() {
571         static key: Key<int> = &Key;
572         set(key, 4);
573         get_mut(key, |_| {
574             get_mut(key, |_| {})
575         })
576     }
577 }