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