From 8b4d077733d443942fa4026ce262a276845c3b81 Mon Sep 17 00:00:00 2001 From: jsturm Date: Thu, 28 Mar 2002 02:08:36 +0000 Subject: [PATCH] * java/net/PlainDatagramSocketImpl.java (close): Use native implementation. (finalize): New method. * java/net/PlainSocketImpl.java (finalize): New method. * java/net/natPlainDatagramSocketImpl.cc (java/io/FileDescriptor.h): Don't include. (close): Implement method here. (create): Don't assign fd. * java/net/natPlainSocketImpl.cc (java/io/FileDescriptor.h): Don't include. (create): Don't assign fd. (accept): Likewise. (close): Synchronize. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@51492 138bc75d-0d04-0410-961f-82ee72b054a4 --- libjava/ChangeLog | 19 +++++++++++++++ libjava/java/net/PlainDatagramSocketImpl.java | 32 +++++++++----------------- libjava/java/net/PlainSocketImpl.java | 21 +++++++++++++---- libjava/java/net/natPlainDatagramSocketImpl.cc | 24 +++++++++++++++++-- libjava/java/net/natPlainSocketImpl.cc | 8 ++++--- 5 files changed, 73 insertions(+), 31 deletions(-) diff --git a/libjava/ChangeLog b/libjava/ChangeLog index 71c899f..fb9476d 100644 --- a/libjava/ChangeLog +++ b/libjava/ChangeLog @@ -1,3 +1,22 @@ +2002-03-27 Jeff Sturm + + * java/net/PlainDatagramSocketImpl.java + (close): Use native implementation. + (finalize): New method. + + * java/net/PlainSocketImpl.java (finalize): New method. + + * java/net/natPlainDatagramSocketImpl.cc + (java/io/FileDescriptor.h): Don't include. + (close): Implement method here. + (create): Don't assign fd. + + * java/net/natPlainSocketImpl.cc + (java/io/FileDescriptor.h): Don't include. + (create): Don't assign fd. + (accept): Likewise. + (close): Synchronize. + 2002-03-27 Richard Henderson * include/posix-threads.h [alpha] (_Jv_ThreadSelf): Avoid a copy. diff --git a/libjava/java/net/PlainDatagramSocketImpl.java b/libjava/java/net/PlainDatagramSocketImpl.java index 7076ccf..55ea468 100644 --- a/libjava/java/net/PlainDatagramSocketImpl.java +++ b/libjava/java/net/PlainDatagramSocketImpl.java @@ -67,27 +67,7 @@ class PlainDatagramSocketImpl extends DatagramSocketImpl public native Object getOption(int optID) throws SocketException; private native void mcastGrp(InetAddress inetaddr, boolean join) throws IOException; - - protected void close() - { - // FIXME: The close method in each of the DatagramSocket* classes does - // not throw an IOException. The issue is that FileDescriptor.close() - // in natFileDescriptorPosix.cc can throw one, so we have to catch - // it here. It seems that FileDescriptor.close is properly throwing - // the IOException on errors since many of the java.io classes depend - // on that. This probably requires a bit more research but for now, - // we'll catch the IOException here. - try - { - if (fd.valid()) - fd.close(); - } - catch (IOException e) - { - System.err.println("PlainDatagramSocketImpl.close: Error closing - " + - e.getMessage()); - } - } + protected native void close(); // Deprecated in JDK 1.2. protected byte getTTL() throws IOException @@ -110,4 +90,14 @@ class PlainDatagramSocketImpl extends DatagramSocketImpl { mcastGrp(inetaddr, false); } + + protected void finalize() throws Throwable + { + synchronized (this) + { + if (fnum != -1) + close(); + } + super.finalize(); + } } diff --git a/libjava/java/net/PlainSocketImpl.java b/libjava/java/net/PlainSocketImpl.java index 81df487..354d652 100644 --- a/libjava/java/net/PlainSocketImpl.java +++ b/libjava/java/net/PlainSocketImpl.java @@ -39,11 +39,6 @@ class PlainSocketImpl extends SocketImpl * This is used for reads and writes to/from the socket and * to close it. * - * {@link SocketImpl#fd} is created from this like so: - *
-   *   fd = new FileDescriptor (fnum);
-   * 
- * * When the socket is closed this is reset to -1. */ int fnum = -1; @@ -108,6 +103,22 @@ class PlainSocketImpl extends SocketImpl private native void write(byte[] buffer, int offset, int count) throws IOException; + protected void finalize() throws Throwable + { + synchronized (this) + { + if (fnum != -1) + try + { + close(); + } + catch (IOException ex) + { + // ignore + } + } + super.finalize(); + } /** @return the input stream attached to the socket. */ diff --git a/libjava/java/net/natPlainDatagramSocketImpl.cc b/libjava/java/net/natPlainDatagramSocketImpl.cc index 81e17cc..071d367 100644 --- a/libjava/java/net/natPlainDatagramSocketImpl.cc +++ b/libjava/java/net/natPlainDatagramSocketImpl.cc @@ -51,7 +51,6 @@ _Jv_bind (int fd, struct sockaddr *addr, int addrlen) #include #include -#include #include #include #include @@ -91,6 +90,13 @@ java::net::PlainDatagramSocketImpl::peek (java::net::InetAddress *) } void +java::net::PlainDatagramSocketImpl::close () +{ + throw new java::io::IOException ( + JvNewStringLatin1 ("DatagramSocketImpl.close: unimplemented")); +} + +void java::net::PlainDatagramSocketImpl::send (java::net::DatagramPacket *) { throw new java::io::IOException ( @@ -188,8 +194,9 @@ java::net::PlainDatagramSocketImpl::create () _Jv_platform_close_on_exec (sock); + // We use fnum in place of fd here. From leaving fd null we avoid + // the double close problem in FileDescriptor.finalize. fnum = sock; - fd = new java::io::FileDescriptor (sock); } void @@ -284,6 +291,19 @@ java::net::PlainDatagramSocketImpl::peek (java::net::InetAddress *i) throw new java::io::IOException (JvNewStringUTF (strerr)); } +// Close(shutdown) the socket. +void +java::net::PlainDatagramSocketImpl::close () +{ + // Avoid races from asynchronous finalization. + JvSynchronize sync (this); + + // The method isn't declared to throw anything, so we disregard + // the return value. + ::close (fnum); + fnum = -1; +} + void java::net::PlainDatagramSocketImpl::send (java::net::DatagramPacket *p) { diff --git a/libjava/java/net/natPlainSocketImpl.cc b/libjava/java/net/natPlainSocketImpl.cc index 3afc5d5..85f8313 100644 --- a/libjava/java/net/natPlainSocketImpl.cc +++ b/libjava/java/net/natPlainSocketImpl.cc @@ -102,7 +102,6 @@ _Jv_accept (int fd, struct sockaddr *addr, socklen_t *addrlen) #include #include #include -#include #include #include #include @@ -234,8 +233,9 @@ java::net::PlainSocketImpl::create (jboolean stream) _Jv_platform_close_on_exec (sock); + // We use fnum in place of fd here. From leaving fd null we avoid + // the double close problem in FileDescriptor.finalize. fnum = sock; - fd = new java::io::FileDescriptor (sock); } void @@ -402,7 +402,6 @@ java::net::PlainSocketImpl::accept (java::net::PlainSocketImpl *s) s->localport = localport; s->address = new InetAddress (raddr, NULL); s->port = rport; - s->fd = new java::io::FileDescriptor (new_socket); return; error: char* strerr = strerror (errno); @@ -413,6 +412,9 @@ java::net::PlainSocketImpl::accept (java::net::PlainSocketImpl *s) void java::net::PlainSocketImpl::close() { + // Avoid races from asynchronous finalization. + JvSynchronize sync (this); + // should we use shutdown here? how would that effect so_linger? int res = ::close (fnum); -- 2.7.4