From b5dd01eb2a641d51f0b3135671a0e674d18f27f7 Mon Sep 17 00:00:00 2001
From: Michael Sullivan <sully@msully.net>
Date: Fri, 24 Aug 2012 12:17:08 -0700
Subject: [PATCH] Add a bunch more unsafe blocks to prepare for the new
 semantics of unsafe/closure interactions.

---
 src/libcore/task.rs          | 10 +++++-----
 src/libstd/net_ip.rs         |  6 +++---
 src/libstd/net_tcp.rs        | 18 +++++++++---------
 src/libstd/timer.rs          |  2 +-
 src/libstd/uv_global_loop.rs | 24 +++++++++++++-----------
 src/libstd/uv_iotask.rs      |  2 +-
 6 files changed, 32 insertions(+), 30 deletions(-)

diff --git a/src/libcore/task.rs b/src/libcore/task.rs
index 863efc79d4a..fb08a971385 100644
--- a/src/libcore/task.rs
+++ b/src/libcore/task.rs
@@ -2089,7 +2089,7 @@ fn test_child_doesnt_ref_parent() {
 fn test_tls_multitask() unsafe {
     fn my_key(+_x: @~str) { }
     local_data_set(my_key, @~"parent data");
-    do task::spawn {
+    do task::spawn unsafe {
         assert local_data_get(my_key) == none; // TLS shouldn't carry over.
         local_data_set(my_key, @~"child data");
         assert *(local_data_get(my_key).get()) == ~"child data";
@@ -2155,7 +2155,7 @@ fn test_tls_multiple_types() unsafe {
     fn str_key(+_x: @~str) { }
     fn box_key(+_x: @@()) { }
     fn int_key(+_x: @int) { }
-    do task::spawn {
+    do task::spawn unsafe {
         local_data_set(str_key, @~"string data");
         local_data_set(box_key, @@());
         local_data_set(int_key, @42);
@@ -2163,11 +2163,11 @@ fn test_tls_multiple_types() unsafe {
 }
 
 #[test]
-fn test_tls_overwrite_multiple_types() unsafe {
+fn test_tls_overwrite_multiple_types() {
     fn str_key(+_x: @~str) { }
     fn box_key(+_x: @@()) { }
     fn int_key(+_x: @int) { }
-    do task::spawn {
+    do task::spawn unsafe {
         local_data_set(str_key, @~"string data");
         local_data_set(int_key, @42);
         // This could cause a segfault if overwriting-destruction is done with
@@ -2185,7 +2185,7 @@ fn test_tls_cleanup_on_failure() unsafe {
     fn int_key(+_x: @int) { }
     local_data_set(str_key, @~"parent data");
     local_data_set(box_key, @@());
-    do task::spawn { // spawn_linked
+    do task::spawn unsafe { // spawn_linked
         local_data_set(str_key, @~"string data");
         local_data_set(box_key, @@());
         local_data_set(int_key, @42);
diff --git a/src/libstd/net_ip.rs b/src/libstd/net_ip.rs
index f7a7f42afcf..57cc8bd0357 100644
--- a/src/libstd/net_ip.rs
+++ b/src/libstd/net_ip.rs
@@ -85,9 +85,9 @@ enum ip_get_addr_err {
  * object in the case of failure
  */
 fn get_addr(++node: ~str, iotask: iotask)
-        -> result::result<~[ip_addr], ip_get_addr_err> unsafe {
+        -> result::result<~[ip_addr], ip_get_addr_err> {
     do core::comm::listen |output_ch| {
-        do str::as_buf(node) |node_ptr, len| {
+        do str::as_buf(node) |node_ptr, len| unsafe {
             log(debug, fmt!("slice len %?", len));
             let handle = create_uv_getaddrinfo_t();
             let handle_ptr = ptr::addr_of(handle);
@@ -95,7 +95,7 @@ fn get_addr(++node: ~str, iotask: iotask)
                 output_ch: output_ch
             };
             let handle_data_ptr = ptr::addr_of(handle_data);
-            do interact(iotask) |loop_ptr| {
+            do interact(iotask) |loop_ptr| unsafe {
                 let result = uv_getaddrinfo(
                     loop_ptr,
                     handle_ptr,
diff --git a/src/libstd/net_tcp.rs b/src/libstd/net_tcp.rs
index d1cbe221ed3..170823eb79d 100644
--- a/src/libstd/net_tcp.rs
+++ b/src/libstd/net_tcp.rs
@@ -145,7 +145,7 @@ fn connect(-input_ip: ip::ip_addr, port: uint,
     // we can send into the interact cb to be handled in libuv..
     log(debug, fmt!("stream_handle_ptr outside interact %?",
         stream_handle_ptr));
-    do iotask::interact(iotask) |loop_ptr| {
+    do iotask::interact(iotask) |loop_ptr| unsafe {
         log(debug, ~"in interact cb for tcp client connect..");
         log(debug, fmt!("stream_handle_ptr in interact %?",
             stream_handle_ptr));
@@ -571,7 +571,7 @@ fn listen(-host_ip: ip::ip_addr, port: uint, backlog: uint,
     -> result::result<(), tcp_listen_err_data> unsafe {
     do listen_common(host_ip, port, backlog, iotask, on_establish_cb)
         // on_connect_cb
-        |handle| {
+        |handle| unsafe {
             let server_data_ptr = uv::ll::get_data_for_uv_handle(handle)
                 as *tcp_listen_fc_data;
             let new_conn = new_tcp_conn(handle);
@@ -608,7 +608,7 @@ fn listen_common(-host_ip: ip::ip_addr, port: uint, backlog: uint,
         // tcp::connect (because the iotask::interact cb isn't
         // nested within a core::comm::listen block)
         let loc_ip = copy(host_ip);
-        do iotask::interact(iotask) |loop_ptr| {
+        do iotask::interact(iotask) |loop_ptr| unsafe {
             match uv::ll::tcp_init(loop_ptr, server_stream_ptr) {
               0i32 => {
                 uv::ll::set_data_for_uv_handle(
@@ -660,7 +660,7 @@ fn listen_common(-host_ip: ip::ip_addr, port: uint, backlog: uint,
     };
     match setup_result {
       some(err_data) => {
-        do iotask::interact(iotask) |loop_ptr| {
+        do iotask::interact(iotask) |loop_ptr| unsafe {
             log(debug, fmt!("tcp::listen post-kill recv hl interact %?",
                             loop_ptr));
             (*server_data_ptr).active = false;
@@ -687,7 +687,7 @@ fn listen_common(-host_ip: ip::ip_addr, port: uint, backlog: uint,
       none => {
         on_establish_cb(kill_ch);
         let kill_result = core::comm::recv(kill_po);
-        do iotask::interact(iotask) |loop_ptr| {
+        do iotask::interact(iotask) |loop_ptr| unsafe {
             log(debug, fmt!("tcp::listen post-kill recv hl interact %?",
                             loop_ptr));
             (*server_data_ptr).active = false;
@@ -844,7 +844,7 @@ fn tear_down_socket_data(socket_data: @tcp_socket_data) unsafe {
     };
     let close_data_ptr = ptr::addr_of(close_data);
     let stream_handle_ptr = (*socket_data).stream_handle_ptr;
-    do iotask::interact((*socket_data).iotask) |loop_ptr| {
+    do iotask::interact((*socket_data).iotask) |loop_ptr| unsafe {
         log(debug, fmt!("interact dtor for tcp_socket stream %? loop %?",
             stream_handle_ptr, loop_ptr));
         uv::ll::set_data_for_uv_handle(stream_handle_ptr,
@@ -902,7 +902,7 @@ fn read_stop_common_impl(socket_data: *tcp_socket_data) ->
     let stream_handle_ptr = (*socket_data).stream_handle_ptr;
     let stop_po = core::comm::port::<option<tcp_err_data>>();
     let stop_ch = core::comm::chan(stop_po);
-    do iotask::interact((*socket_data).iotask) |loop_ptr| {
+    do iotask::interact((*socket_data).iotask) |loop_ptr| unsafe {
         log(debug, ~"in interact cb for tcp::read_stop");
         match uv::ll::read_stop(stream_handle_ptr as *uv::ll::uv_stream_t) {
           0i32 => {
@@ -930,7 +930,7 @@ fn read_start_common_impl(socket_data: *tcp_socket_data)
     let start_po = core::comm::port::<option<uv::ll::uv_err_data>>();
     let start_ch = core::comm::chan(start_po);
     log(debug, ~"in tcp::read_start before interact loop");
-    do iotask::interact((*socket_data).iotask) |loop_ptr| {
+    do iotask::interact((*socket_data).iotask) |loop_ptr| unsafe {
         log(debug, fmt!("in tcp::read_start interact cb %?", loop_ptr));
         match uv::ll::read_start(stream_handle_ptr as *uv::ll::uv_stream_t,
                                on_alloc_cb,
@@ -970,7 +970,7 @@ fn write_common_impl(socket_data_ptr: *tcp_socket_data,
         result_ch: core::comm::chan(result_po)
     };
     let write_data_ptr = ptr::addr_of(write_data);
-    do iotask::interact((*socket_data_ptr).iotask) |loop_ptr| {
+    do iotask::interact((*socket_data_ptr).iotask) |loop_ptr| unsafe {
         log(debug, fmt!("in interact cb for tcp::write %?", loop_ptr));
         match uv::ll::write(write_req_ptr,
                           stream_handle_ptr,
diff --git a/src/libstd/timer.rs b/src/libstd/timer.rs
index 02c2294f8c4..d84d6632dbd 100644
--- a/src/libstd/timer.rs
+++ b/src/libstd/timer.rs
@@ -31,7 +31,7 @@ fn delayed_send<T: copy send>(iotask: iotask,
             let timer_done_ch_ptr = ptr::addr_of(timer_done_ch);
             let timer = uv::ll::timer_t();
             let timer_ptr = ptr::addr_of(timer);
-            do iotask::interact(iotask) |loop_ptr| {
+            do iotask::interact(iotask) |loop_ptr| unsafe {
                 let init_result = uv::ll::timer_init(loop_ptr, timer_ptr);
                 if (init_result == 0i32) {
                     let start_result = uv::ll::timer_start(
diff --git a/src/libstd/uv_global_loop.rs b/src/libstd/uv_global_loop.rs
index 1c301a78260..6fe2cdb8223 100644
--- a/src/libstd/uv_global_loop.rs
+++ b/src/libstd/uv_global_loop.rs
@@ -49,7 +49,7 @@ fn get_monitor_task_gl() -> iotask unsafe {
                                                 task::task().sched_mode
                                                 (task::SingleThreaded)
                                                 .unlinked()
-                                           }) |msg_po| {
+                                           }) |msg_po| unsafe {
         debug!("global monitor task starting");
 
         // As a weak task the runtime will notify us when to exit
@@ -85,20 +85,22 @@ fn get_monitor_task_gl() -> iotask unsafe {
     }
 }
 
-fn spawn_loop() -> iotask unsafe {
+fn spawn_loop() -> iotask {
     let builder = do task::task().add_wrapper |task_body| {
         fn~(move task_body) {
             // The I/O loop task also needs to be weak so it doesn't keep
             // the runtime alive
-            do weaken_task |weak_exit_po| {
-                debug!("global libuv task is now weak %?", weak_exit_po);
-                task_body();
+            unsafe {
+                do weaken_task |weak_exit_po| {
+                    debug!("global libuv task is now weak %?", weak_exit_po);
+                    task_body();
 
-                // We don't wait for the exit message on weak_exit_po
-                // because the monitor task will tell the uv loop when to
-                // exit
+                    // We don't wait for the exit message on weak_exit_po
+                    // because the monitor task will tell the uv loop when to
+                    // exit
 
-                debug!("global libuv task is leaving weakened state");
+                    debug!("global libuv task is leaving weakened state");
+                }
             }
         }
     };
@@ -120,7 +122,7 @@ mod test {
         log(debug, ~"in simple timer cb");
         ll::timer_stop(timer_ptr);
         let hl_loop = get_gl();
-        do iotask::interact(hl_loop) |_loop_ptr| {
+        do iotask::interact(hl_loop) |_loop_ptr| unsafe {
             log(debug, ~"closing timer");
             ll::close(timer_ptr, simple_timer_close_cb);
             log(debug, ~"about to deref exit_ch_ptr");
@@ -137,7 +139,7 @@ mod test {
                        exit_ch_ptr));
         let timer_handle = ll::timer_t();
         let timer_ptr = ptr::addr_of(timer_handle);
-        do iotask::interact(iotask) |loop_ptr| {
+        do iotask::interact(iotask) |loop_ptr| unsafe {
             log(debug, ~"user code inside interact loop!!!");
             let init_status = ll::timer_init(loop_ptr, timer_ptr);
             if(init_status == 0i32) {
diff --git a/src/libstd/uv_iotask.rs b/src/libstd/uv_iotask.rs
index 2643edc87bd..bb7f4f56782 100644
--- a/src/libstd/uv_iotask.rs
+++ b/src/libstd/uv_iotask.rs
@@ -193,7 +193,7 @@ mod test {
             exit_ch: exit_ch
         };
         let ah_data_ptr = ptr::addr_of(ah_data);
-        do interact(iotask) |loop_ptr| {
+        do interact(iotask) |loop_ptr| unsafe {
             ll::async_init(loop_ptr, ah_ptr, async_handle_cb);
             ll::set_data_for_uv_handle(ah_ptr, ah_data_ptr as *libc::c_void);
             ll::async_send(ah_ptr);