about summary refs log tree commit diff
path: root/src/libstd/timer.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/libstd/timer.rs')
-rw-r--r--src/libstd/timer.rs91
1 files changed, 49 insertions, 42 deletions
diff --git a/src/libstd/timer.rs b/src/libstd/timer.rs
index d62e2cbf05d..b967f92a22e 100644
--- a/src/libstd/timer.rs
+++ b/src/libstd/timer.rs
@@ -18,7 +18,9 @@ use uv::iotask::IoTask;
 
 use core::either;
 use core::libc;
-use core::oldcomm;
+use core::libc::c_void;
+use core::cast::transmute;
+use core::pipes::{stream, Chan, SharedChan, Port, select2i};
 use core::prelude::*;
 use core::ptr;
 use core;
@@ -41,12 +43,11 @@ use core;
  */
 pub fn delayed_send<T: Owned>(iotask: &IoTask,
                               msecs: uint,
-                              ch: oldcomm::Chan<T>,
+                              ch: &Chan<T>,
                               val: T) {
         unsafe {
-            let timer_done_po = oldcomm::Port::<()>();
-            let timer_done_ch = oldcomm::Chan(&timer_done_po);
-            let timer_done_ch_ptr = ptr::addr_of(&timer_done_ch);
+            let (timer_done_po, timer_done_ch) = stream::<()>();
+            let timer_done_ch = SharedChan(timer_done_ch);
             let timer = uv::ll::timer_t();
             let timer_ptr = ptr::addr_of(&timer);
             do iotask::interact(iotask) |loop_ptr| {
@@ -56,9 +57,15 @@ pub fn delayed_send<T: Owned>(iotask: &IoTask,
                         let start_result = uv::ll::timer_start(
                             timer_ptr, delayed_send_cb, msecs, 0u);
                         if (start_result == 0i32) {
+                            // Note: putting the channel into a ~
+                            // to cast to *c_void
+                            let timer_done_ch_clone = ~timer_done_ch.clone();
+                            let timer_done_ch_ptr = transmute::<
+                                ~SharedChan<()>, *c_void>(
+                                timer_done_ch_clone);
                             uv::ll::set_data_for_uv_handle(
                                 timer_ptr,
-                                timer_done_ch_ptr as *libc::c_void);
+                                timer_done_ch_ptr);
                         } else {
                             let error_msg = uv::ll::get_last_err_info(
                                 loop_ptr);
@@ -73,11 +80,11 @@ pub fn delayed_send<T: Owned>(iotask: &IoTask,
                 }
             };
             // delayed_send_cb has been processed by libuv
-            oldcomm::recv(timer_done_po);
+            timer_done_po.recv();
             // notify the caller immediately
-            oldcomm::send(ch, move(val));
+            ch.send(val);
             // uv_close for this timer has been processed
-            oldcomm::recv(timer_done_po);
+            timer_done_po.recv();
     };
 }
 
@@ -93,10 +100,9 @@ pub fn delayed_send<T: Owned>(iotask: &IoTask,
  * * msecs - an amount of time, in milliseconds, for the current task to block
  */
 pub fn sleep(iotask: &IoTask, msecs: uint) {
-    let exit_po = oldcomm::Port::<()>();
-    let exit_ch = oldcomm::Chan(&exit_po);
-    delayed_send(iotask, msecs, exit_ch, ());
-    oldcomm::recv(exit_po);
+    let (exit_po, exit_ch) = stream::<()>();
+    delayed_send(iotask, msecs, &exit_ch, ());
+    exit_po.recv();
 }
 
 /**
@@ -121,20 +127,17 @@ pub fn sleep(iotask: &IoTask, msecs: uint) {
  */
 pub fn recv_timeout<T: Copy Owned>(iotask: &IoTask,
                                    msecs: uint,
-                                   wait_po: oldcomm::Port<T>)
+                                   wait_po: &Port<T>)
                                 -> Option<T> {
-    let timeout_po = oldcomm::Port::<()>();
-    let timeout_ch = oldcomm::Chan(&timeout_po);
-    delayed_send(iotask, msecs, timeout_ch, ());
+    let (timeout_po, timeout_ch) = stream::<()>();
+    delayed_send(iotask, msecs, &timeout_ch, ());
     // FIXME: This could be written clearer (#2618)
     either::either(
-        |left_val| {
-            log(debug, fmt!("recv_time .. left_val %?",
-                           left_val));
+        |_| {
             None
-        }, |right_val| {
-            Some(*right_val)
-        }, &oldcomm::select2(timeout_po, wait_po)
+        }, |_| {
+            Some(wait_po.recv())
+        }, &select2i(&timeout_po, wait_po)
     )
 }
 
@@ -144,11 +147,14 @@ extern fn delayed_send_cb(handle: *uv::ll::uv_timer_t,
     unsafe {
         log(debug,
             fmt!("delayed_send_cb handle %? status %?", handle, status));
-        let timer_done_ch =
-            *(uv::ll::get_data_for_uv_handle(handle) as *oldcomm::Chan<()>);
+        // Faking a borrowed pointer to our ~SharedChan
+        let timer_done_ch_ptr: &*c_void = &uv::ll::get_data_for_uv_handle(
+            handle);
+        let timer_done_ch_ptr = transmute::<&*c_void, &~SharedChan<()>>(
+            timer_done_ch_ptr);
         let stop_result = uv::ll::timer_stop(handle);
         if (stop_result == 0i32) {
-            oldcomm::send(timer_done_ch, ());
+            timer_done_ch_ptr.send(());
             uv::ll::close(handle, delayed_send_close_cb);
         } else {
             let loop_ptr = uv::ll::get_loop_for_uv_handle(handle);
@@ -161,9 +167,10 @@ extern fn delayed_send_cb(handle: *uv::ll::uv_timer_t,
 extern fn delayed_send_close_cb(handle: *uv::ll::uv_timer_t) {
     unsafe {
         log(debug, fmt!("delayed_send_close_cb handle %?", handle));
-        let timer_done_ch =
-            *(uv::ll::get_data_for_uv_handle(handle) as *oldcomm::Chan<()>);
-        oldcomm::send(timer_done_ch, ());
+        let timer_done_ch_ptr = uv::ll::get_data_for_uv_handle(handle);
+        let timer_done_ch = transmute::<*c_void, ~SharedChan<()>>(
+            timer_done_ch_ptr);
+        timer_done_ch.send(());
     }
 }
 
@@ -175,9 +182,9 @@ mod test {
     use uv;
 
     use core::iter;
-    use core::oldcomm;
     use core::rand;
     use core::task;
+    use core::pipes::{stream, SharedChan};
 
     #[test]
     pub fn test_gl_timer_simple_sleep_test() {
@@ -195,8 +202,8 @@ mod test {
 
     #[test]
     pub fn test_gl_timer_sleep_stress2() {
-        let po = oldcomm::Port();
-        let ch = oldcomm::Chan(&po);
+        let (po, ch) = stream();
+        let ch = SharedChan(ch);
         let hl_loop = &uv::global_loop::get();
 
         let repeat = 20u;
@@ -210,8 +217,10 @@ mod test {
 
         for iter::repeat(repeat) {
 
+            let ch = ch.clone();
             for spec.each |spec| {
                 let (times, maxms) = *spec;
+                let ch = ch.clone();
                 let hl_loop_clone = hl_loop.clone();
                 do task::spawn {
                     use rand::*;
@@ -219,13 +228,13 @@ mod test {
                     for iter::repeat(times) {
                         sleep(&hl_loop_clone, rng.next() as uint % maxms);
                     }
-                    oldcomm::send(ch, ());
+                    ch.send(());
                 }
             }
         }
 
         for iter::repeat(repeat * spec.len()) {
-            oldcomm::recv(po)
+            po.recv()
         }
     }
 
@@ -246,14 +255,13 @@ mod test {
             task::yield();
 
             let expected = rand::rng().gen_str(16u);
-            let test_po = core::comm::port::<str>();
-            let test_ch = core::comm::chan(test_po);
+            let (test_po, test_ch) = stream::<~str>();
 
             do task::spawn() {
-                delayed_send(hl_loop, 1u, test_ch, expected);
+                delayed_send(hl_loop, 1u, &test_ch, expected);
             };
 
-            match recv_timeout(hl_loop, 10u, test_po) {
+            match recv_timeout(hl_loop, 10u, &test_po) {
               Some(val) => {
                 assert val == expected;
                 successes += 1;
@@ -274,14 +282,13 @@ mod test {
 
         for iter::repeat(times as uint) {
             let expected = rand::Rng().gen_str(16u);
-            let test_po = oldcomm::Port::<~str>();
-            let test_ch = oldcomm::Chan(&test_po);
+            let (test_po, test_ch) = stream::<~str>();
             let hl_loop_clone = hl_loop.clone();
             do task::spawn() {
-                delayed_send(&hl_loop_clone, 50u, test_ch, expected);
+                delayed_send(&hl_loop_clone, 50u, &test_ch, expected);
             };
 
-            match recv_timeout(&hl_loop, 1u, test_po) {
+            match recv_timeout(&hl_loop, 1u, &test_po) {
               None => successes += 1,
               _ => failures += 1
             };