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