* java/net/PlainDatagramSocketImpl.java
authorjsturm <jsturm@138bc75d-0d04-0410-961f-82ee72b054a4>
Thu, 28 Mar 2002 02:08:36 +0000 (02:08 +0000)
committerjsturm <jsturm@138bc75d-0d04-0410-961f-82ee72b054a4>
Thu, 28 Mar 2002 02:08:36 +0000 (02:08 +0000)
(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
libjava/java/net/PlainDatagramSocketImpl.java
libjava/java/net/PlainSocketImpl.java
libjava/java/net/natPlainDatagramSocketImpl.cc
libjava/java/net/natPlainSocketImpl.cc

index 71c899f..fb9476d 100644 (file)
@@ -1,3 +1,22 @@
+2002-03-27  Jeff Sturm  <jsturm@one-point.com>
+
+       * 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  <rth@redhat.com>
 
        * include/posix-threads.h [alpha] (_Jv_ThreadSelf): Avoid a copy.
index 7076ccf..55ea468 100644 (file)
@@ -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();
+  }
 }
index 81df487..354d652 100644 (file)
@@ -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:
-   * <pre>
-   *   fd = new FileDescriptor (fnum);
-   * </pre>
-   *
    * 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.
    */
index 81e17cc..071d367 100644 (file)
@@ -51,7 +51,6 @@ _Jv_bind (int fd, struct sockaddr *addr, int addrlen)
 
 #include <gcj/cni.h>
 #include <java/io/IOException.h>
-#include <java/io/FileDescriptor.h>
 #include <java/io/InterruptedIOException.h>
 #include <java/net/BindException.h>
 #include <java/net/SocketException.h>
@@ -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)
 {
index 3afc5d5..85f8313 100644 (file)
@@ -102,7 +102,6 @@ _Jv_accept (int fd, struct sockaddr *addr, socklen_t *addrlen)
 #include <gcj/cni.h>
 #include <gcj/javaprims.h>
 #include <java/io/IOException.h>
-#include <java/io/FileDescriptor.h>
 #include <java/io/InterruptedIOException.h>
 #include <java/net/BindException.h>
 #include <java/net/ConnectException.h>
@@ -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);