Skip to content

std: Improve non-task-based usage #14644

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 5, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions src/libstd/local_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,22 +96,24 @@ pub type Map = Vec<Option<(*u8, TLSValue, uint)>>;
type TLSValue = Box<LocalData:Send>;

// Gets the map from the runtime. Lazily initialises if not done so already.
unsafe fn get_local_map() -> &mut Map {
unsafe fn get_local_map() -> Option<&mut Map> {
use rt::local::Local;

if !Local::exists(None::<Task>) { return None }

let task: *mut Task = Local::unsafe_borrow();
match &mut (*task).storage {
// If the at_exit function is already set, then we just need to take
// a loan out on the TLS map stored inside
&LocalStorage(Some(ref mut map_ptr)) => {
return map_ptr;
return Some(map_ptr);
}
// If this is the first time we've accessed TLS, perform similar
// actions to the oldsched way of doing things.
&LocalStorage(ref mut slot) => {
*slot = Some(vec!());
match *slot {
Some(ref mut map_ptr) => { return map_ptr }
Some(ref mut map_ptr) => { return Some(map_ptr) }
None => unreachable!(),
}
}
Expand Down Expand Up @@ -156,7 +158,10 @@ impl<T: 'static> KeyValue<T> {
/// assert_eq!(foo.replace(None), Some(4));
/// ```
pub fn replace(&'static self, data: Option<T>) -> Option<T> {
let map = unsafe { get_local_map() };
let map = match unsafe { get_local_map() } {
Some(map) => map,
None => fail!("must have a local task to insert into TLD"),
};
let keyval = key_to_key_value(self);

// When the task-local map is destroyed, all the data needs to be
Expand Down Expand Up @@ -223,7 +228,10 @@ impl<T: 'static> KeyValue<T> {
/// assert_eq!(*key.get().unwrap(), 3);
/// ```
pub fn get(&'static self) -> Option<Ref<T>> {
let map = unsafe { get_local_map() };
let map = match unsafe { get_local_map() } {
Some(map) => map,
None => return None,
};

self.find(map).map(|(pos, data, loan)| {
*loan += 1;
Expand Down Expand Up @@ -260,7 +268,7 @@ impl<T: 'static> Deref<T> for Ref<T> {
#[unsafe_destructor]
impl<T: 'static> Drop for Ref<T> {
fn drop(&mut self) {
let map = unsafe { get_local_map() };
let map = unsafe { get_local_map().unwrap() };

let (_, _, ref mut loan) = *map.get_mut(self._index).get_mut_ref();
*loan -= 1;
Expand Down
5 changes: 4 additions & 1 deletion src/libstd/rt/rtio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,10 @@ impl<'a> LocalIo<'a> {
//
// In order to get around this, we just transmute a copy out of the task
// in order to have what is likely a static lifetime (bad).
let mut t: Box<Task> = Local::take();
let mut t: Box<Task> = match Local::try_take() {
Some(t) => t,
None => return None,
};
let ret = t.local_io().map(|t| {
unsafe { mem::transmute_copy(&t) }
});
Expand Down
Loading