From ab84ada42e41765b56f35692706046404a6dcb90 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Sat, 15 Nov 2014 17:34:47 -0800 Subject: [PATCH] Use an external tls library --- librustuv/Cargo.toml | 2 +- librustuv/src/event_loop.rs | 8 ++-- libtls/.gitignore | 2 - libtls/Cargo.toml | 5 -- libtls/src/lib.rs | 94 ------------------------------------- 5 files changed, 5 insertions(+), 106 deletions(-) delete mode 100644 libtls/.gitignore delete mode 100644 libtls/Cargo.toml delete mode 100644 libtls/src/lib.rs diff --git a/librustuv/Cargo.toml b/librustuv/Cargo.toml index e6a0bc6..9b9ff07 100644 --- a/librustuv/Cargo.toml +++ b/librustuv/Cargo.toml @@ -8,7 +8,7 @@ build = "make -C build" path = "../libgreen" [dependencies.tls] -path = "../libtls" +git = "https://github.com/alexcrichton/tls-rs" [[test]] name = "all" diff --git a/librustuv/src/event_loop.rs b/librustuv/src/event_loop.rs index aae1ac5..b7283be 100644 --- a/librustuv/src/event_loop.rs +++ b/librustuv/src/event_loop.rs @@ -18,7 +18,7 @@ use raw::{mod, Loop, Handle}; use queue::QueuePool; use homing::HomeHandle; -tls!(local_loop: Cell<(*mut EventLoop, bool)>) +scoped_tls!(static local_loop: Cell<(*mut EventLoop, bool)>) pub struct EventLoop { uv_loop: Loop, @@ -48,7 +48,7 @@ impl EventLoop { /// borrowed, then an error is returned. pub fn borrow() -> UvResult { let local = unsafe { - local_loop.get(|local| { + local_loop.with(|local| { local.and_then(|c| { match c.get() { (_, true) => None, @@ -69,7 +69,7 @@ impl EventLoop { /// Borrow an unsafe pointer to the local event loop pub unsafe fn borrow_raw() -> UvResult<*mut EventLoop> { - let local = local_loop.get(|local| local.map(|c| c.get().val0())); + let local = local_loop.with(|local| local.map(|c| c.get().val0())); match local { Some(eloop) => Ok(eloop), None => Err(UvError(uvll::UNKNOWN)) @@ -179,7 +179,7 @@ impl DerefMut for BorrowedEventLoop { impl Drop for BorrowedEventLoop { fn drop(&mut self) { unsafe { - local_loop.get(|l| l.unwrap().set((self.local, false))) + local_loop.with(|l| l.unwrap().set((self.local, false))) } } } diff --git a/libtls/.gitignore b/libtls/.gitignore deleted file mode 100644 index 4fffb2f..0000000 --- a/libtls/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -/target -/Cargo.lock diff --git a/libtls/Cargo.toml b/libtls/Cargo.toml deleted file mode 100644 index ceb2648..0000000 --- a/libtls/Cargo.toml +++ /dev/null @@ -1,5 +0,0 @@ -[package] - -name = "tls" -version = "0.0.1" -authors = ["Alex Crichton "] diff --git a/libtls/src/lib.rs b/libtls/src/lib.rs deleted file mode 100644 index 5069e92..0000000 --- a/libtls/src/lib.rs +++ /dev/null @@ -1,94 +0,0 @@ -#![feature(macro_rules, unboxed_closures, unsafe_destructor)] - -pub use imp::Key; - -#[cfg(unix)] -mod imp { - #![macro_escape] - - use std::cell::UnsafeCell; - - #[macro_export] - macro_rules! tls( - ($name:ident: $t:ty) => ( - #[thread_local] - static mut $name: ::tls::Key<$t> = ::tls::Key { - ptr: ::std::cell::UnsafeCell { value: 0 as *mut $t }, - }; - ) - ) - - pub struct Key { - pub ptr: UnsafeCell<*mut T>, - } - - struct Reset { - key: &'static Key, - val: *mut T, - } - - impl Key { - pub fn set(&'static self, t: &T, cb: || -> R) -> R { - let prev = unsafe { - let prev = *self.ptr.get(); - *self.ptr.get() = t as *const _ as *mut T; - prev - }; - let _reset = Reset { key: self, val: prev }; - cb() - } - - pub fn get(&'static self, cb: |Option<&T>| -> R) -> R { - unsafe { - let ptr = *self.ptr.get(); - if ptr.is_null() { - cb(None) - } else { - cb(Some(&*ptr)) - } - } - } - } - - #[unsafe_destructor] - impl Drop for Reset { - fn drop(&mut self) { - unsafe { *self.key.ptr.get() = self.val; } - } - } -} - -// "macro hygiene strikes again" -mod tls { - pub use Key; -} - -#[cfg(test)] -mod test { - - #[test] - fn simple() { - tls!(foo: uint) - - unsafe { - foo.get(|val| { - assert_eq!(val, None); - }); - - foo.set(&1, || { - foo.get(|val| { - assert_eq!(*val.unwrap(), 1); - }); - - let (tx, rx) = channel(); - spawn(proc() { - tx.send(foo.get(|val| { - assert_eq!(val, None); - })); - }); - rx.recv(); - }); - } - - } -}