Skip to content

Commit b771c99

Browse files
committed
core::rt: Fix the finalizer on UvTcpStream and UvTcpListener
Eliminates a lot of calls to `close`
1 parent 01b7b7d commit b771c99

File tree

2 files changed

+16
-52
lines changed

2 files changed

+16
-52
lines changed

src/libcore/rt/io/net/tcp.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
use option::{Option, Some, None};
1212
use result::{Ok, Err};
13-
use ops::Drop;
1413
use rt::sched::local_sched::unsafe_borrow_io;
1514
use rt::io::net::ip::IpAddr;
1615
use rt::io::{Reader, Writer, Listener};
@@ -79,12 +78,6 @@ impl Writer for TcpStream {
7978
fn flush(&mut self) { fail!() }
8079
}
8180

82-
impl Drop for TcpStream {
83-
fn finalize(&self) {
84-
self.rtstream.close();
85-
}
86-
}
87-
8881
pub struct TcpListener {
8982
rtlistener: ~RtioTcpListenerObject
9083
}
@@ -120,12 +113,6 @@ impl Listener<TcpStream> for TcpListener {
120113
}
121114
}
122115

123-
impl Drop for TcpListener {
124-
fn finalize(&self) {
125-
self.rtlistener.close();
126-
}
127-
}
128-
129116
#[cfg(test)]
130117
mod test {
131118
use super::*;

src/libcore/rt/uv/uvio.rs

Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ impl IoFactory for UvIoFactory {
124124
rtdebug!("connect: in connect callback");
125125
let maybe_stream = if status.is_none() {
126126
rtdebug!("status is none");
127-
Ok(~UvTcpStream(stream_watcher))
127+
Ok(~UvTcpStream { watcher: stream_watcher })
128128
} else {
129129
rtdebug!("status is some");
130130
// XXX: Wait for close
@@ -148,7 +148,7 @@ impl IoFactory for UvIoFactory {
148148
fn tcp_bind(&mut self, addr: IpAddr) -> Result<~RtioTcpListenerObject, IoError> {
149149
let mut watcher = TcpWatcher::new(self.uv_loop());
150150
match watcher.bind(addr) {
151-
Ok(_) => Ok(~UvTcpListener(watcher)),
151+
Ok(_) => Ok(~UvTcpListener { watcher: watcher }),
152152
Err(uverr) => {
153153
// XXX: Should we wait until close completes?
154154
watcher.as_stream().close(||());
@@ -158,23 +158,19 @@ impl IoFactory for UvIoFactory {
158158
}
159159
}
160160

161-
pub struct UvTcpListener(TcpWatcher);
161+
// FIXME #6090: Prefer newtype structs but Drop doesn't work
162+
pub struct UvTcpListener {
163+
watcher: TcpWatcher
164+
}
162165

163166
impl UvTcpListener {
164-
fn watcher(&self) -> TcpWatcher {
165-
match self { &UvTcpListener(w) => w }
166-
}
167-
168-
fn close(&self) {
169-
// XXX: Need to wait until close finishes before returning
170-
self.watcher().as_stream().close(||());
171-
}
167+
fn watcher(&self) -> TcpWatcher { self.watcher }
172168
}
173169

174170
impl Drop for UvTcpListener {
175171
fn finalize(&self) {
176-
// XXX: Again, this never gets called. Use .close() instead
177-
//self.watcher().as_stream().close(||());
172+
// XXX: Need to wait until close finishes before returning
173+
self.watcher().as_stream().close(||());
178174
}
179175
}
180176

@@ -200,7 +196,7 @@ impl RtioTcpListener for UvTcpListener {
200196
let client_tcp_watcher = TcpWatcher::new(&mut loop_).as_stream();
201197
// XXX: Needs to be surfaced in interface
202198
server_stream_watcher.accept(client_tcp_watcher);
203-
Ok(~UvTcpStream::new(client_tcp_watcher))
199+
Ok(~UvTcpStream { watcher: client_tcp_watcher })
204200
} else {
205201
Err(standard_error(OtherIoError))
206202
};
@@ -219,28 +215,19 @@ impl RtioTcpListener for UvTcpListener {
219215
}
220216
}
221217

222-
pub struct UvTcpStream(StreamWatcher);
218+
// FIXME #6090: Prefer newtype structs but Drop doesn't work
219+
pub struct UvTcpStream {
220+
watcher: StreamWatcher
221+
}
223222

224223
impl UvTcpStream {
225-
fn new(watcher: StreamWatcher) -> UvTcpStream {
226-
UvTcpStream(watcher)
227-
}
228-
229-
fn watcher(&self) -> StreamWatcher {
230-
match self { &UvTcpStream(w) => w }
231-
}
232-
233-
// XXX: finalize isn't working for ~UvStream???
234-
fn close(&self) {
235-
// XXX: Need to wait until this finishes before returning
236-
self.watcher().close(||());
237-
}
224+
fn watcher(&self) -> StreamWatcher { self.watcher }
238225
}
239226

240227
impl Drop for UvTcpStream {
241228
fn finalize(&self) {
242229
rtdebug!("closing stream");
243-
//self.watcher().close(||());
230+
self.watcher().close(||());
244231
}
245232
}
246233

@@ -354,8 +341,6 @@ fn test_simple_tcp_server_and_client() {
354341
rtdebug!("%u", buf[i] as uint);
355342
assert!(buf[i] == i as u8);
356343
}
357-
stream.close();
358-
listener.close();
359344
}
360345
}
361346

@@ -364,7 +349,6 @@ fn test_simple_tcp_server_and_client() {
364349
let io = local_sched::unsafe_borrow_io();
365350
let mut stream = (*io).tcp_connect(addr).unwrap();
366351
stream.write([0, 1, 2, 3, 4, 5, 6, 7]);
367-
stream.close();
368352
}
369353
}
370354
}
@@ -408,9 +392,6 @@ fn test_read_and_block() {
408392

409393
// Make sure we had multiple reads
410394
assert!(reads > 1);
411-
412-
stream.close();
413-
listener.close();
414395
}
415396

416397
do spawntask_immediately {
@@ -421,7 +402,6 @@ fn test_read_and_block() {
421402
stream.write([0, 1, 2, 3, 4, 5, 6, 7]);
422403
stream.write([0, 1, 2, 3, 4, 5, 6, 7]);
423404
stream.write([0, 1, 2, 3, 4, 5, 6, 7]);
424-
stream.close();
425405
}
426406
}
427407

@@ -445,8 +425,6 @@ fn test_read_read_read() {
445425
stream.write(buf);
446426
total_bytes_written += buf.len();
447427
}
448-
stream.close();
449-
listener.close();
450428
}
451429
}
452430

@@ -465,7 +443,6 @@ fn test_read_read_read() {
465443
}
466444
}
467445
rtdebug!("read %u bytes total", total_bytes_read as uint);
468-
stream.close();
469446
}
470447
}
471448
}

0 commit comments

Comments
 (0)