[kaffe] CVS kaffe (robilad): don't synchronize native socket methods

Kaffe CVS cvs-commits at kaffe.org
Fri Jul 1 03:06:12 PDT 2005


PatchSet 6674 
Date: 2005/07/01 09:56:03
Author: robilad
Branch: HEAD
Tag: (none) 
Log:
don't synchronize native socket methods

2005-07-01  jrandom <jrandom at i2p.net>

        * gnu/java/net/PlainDatagramSocketImpl.java: Removed synchronized
        modifier for all native methods in order to make multi-threaded
        code operating on a single socket.

Members: 
	ChangeLog:1.4199->1.4200 
	libraries/javalib/gnu/java/net/PlainDatagramSocketImpl.java:INITIAL->1.5 

Index: kaffe/ChangeLog
diff -u kaffe/ChangeLog:1.4199 kaffe/ChangeLog:1.4200
--- kaffe/ChangeLog:1.4199	Thu Jun 30 14:01:59 2005
+++ kaffe/ChangeLog	Fri Jul  1 09:56:03 2005
@@ -1,3 +1,9 @@
+2005-07-01  jrandom <jrandom at i2p.net>
+
+	* gnu/java/net/PlainDatagramSocketImpl.java: Removed synchronized
+	modifier for all native methods in order to make multi-threaded 
+	code operating on a single socket.
+
 2005-06-30  Dalibor Topic  <robilad at kaffe.org>
 
         * kaffe/kaffevm/jit/Makefile.am:
===================================================================
Checking out kaffe/libraries/javalib/gnu/java/net/PlainDatagramSocketImpl.java
RCS:  /home/cvs/kaffe/kaffe/libraries/javalib/gnu/java/net/PlainDatagramSocketImpl.java,v
VERS: 1.5
***************
--- /dev/null	Sun Aug  4 19:57:58 2002
+++ kaffe/libraries/javalib/gnu/java/net/PlainDatagramSocketImpl.java	Fri Jul  1 10:06:12 2005
@@ -0,0 +1,177 @@
+/*
+ * Java core library component.
+ *
+ * Copyright (c) 1997, 1998
+ *      Transvirtual Technologies, Inc.  All rights reserved.
+ *
+ * Copyright (c) 2003, 2004
+ *      Kaffe.org's developers. Some parts from GNU Classpath.
+ *
+ * See the file "license.terms" for information on usage and redistribution
+ * of this file.
+ */
+
+package gnu.java.net;
+
+import java.io.FileDescriptor;
+import java.io.IOException;
+import java.net.DatagramPacket;
+import java.net.DatagramSocketImpl;
+import java.net.InetAddress;
+import java.net.NetworkInterface;
+import java.net.SocketAddress;
+import java.net.SocketException;
+import java.net.UnknownHostException;
+
+/* XXX: This class should not be public!
+ * It's public right now cause it's accessed from kaffe.net.
+ */
+public class PlainDatagramSocketImpl
+  extends DatagramSocketImpl {
+
+private int timeout;
+private int native_fd;
+  
+  /**
+   * Lock object to serialize threads wanting to receive 
+   */
+  private final Object RECEIVE_LOCK = new Object();
+  
+  /**
+   * Lock object to serialize threads wanting to send 
+   */
+  private final Object SEND_LOCK = new Object();
+
+
+static {
+	System.loadLibrary("net");
+}
+
+public PlainDatagramSocketImpl() {
+	timeout = -1; // = NOTIMEOUT
+	native_fd = -1;
+}
+
+public int getNativeFD() {
+	return native_fd;
+}
+
+protected void create() throws SocketException {
+	datagramSocketCreate();
+}
+
+protected void close() {
+	datagramSocketClose();
+}
+
+public void setOption(int option, Object data) throws SocketException {
+	final boolean disable = (data instanceof Boolean) && !((Boolean) data).booleanValue();
+	if (disable) {
+		data = new Integer(0);			// the common case
+	}
+	switch (option) {
+	case SO_SNDBUF:
+	case SO_RCVBUF:
+	case SO_LINGER:
+	case SO_REUSEADDR:
+		break;
+	case SO_TIMEOUT:
+		timeout = ((Integer)data).intValue();
+		return;
+	case SO_BINDADDR:
+		throw new SocketException("Read-only socket option");
+	case IP_MULTICAST_IF:
+		if (disable) {			// makes no sense
+			return;
+		}
+		data = (InetAddress) data;	// verify object type
+		break;
+	default:
+		throw new SocketException("Unknown socket option");
+	}
+	socketSetOption(option, data);
+}
+
+public Object getOption(int option) throws SocketException {
+	switch (option) {
+	case SO_SNDBUF:
+	case SO_RCVBUF:
+	case SO_LINGER:
+	case SO_REUSEADDR:
+		return new Integer(socketGetOption(option));
+	case SO_TIMEOUT:
+		return new Integer(timeout);
+	case SO_BINDADDR:
+	case IP_MULTICAST_IF:
+		int val = socketGetOption(option);
+		try {
+			return InetAddress.getByName(
+			    ((int) ((val >> 24) & 0xff)) + "." +
+			    ((int) ((val >> 16) & 0xff)) + "." +
+			    ((int) ((val >>  8) & 0xff)) + "." +
+			    ((int) ((val      ) & 0xff)) );
+		} catch (UnknownHostException e) {
+			throw new Error("impossible result");
+		}
+	default:
+		throw new SocketException("Unknown socket option");
+	}
+}
+
+protected void finalize() throws Throwable {
+  synchronized(this)
+    {
+      if (native_fd != -1)
+	close();
+    }
+  super.finalize();
+}
+
+protected void setTimeToLive(int ttl) throws IOException {
+	setTTL((byte)ttl);
+}
+
+protected int getTimeToLive() throws IOException {
+	return getTTL();
+}
+
+protected void sendUrgentData(int data) throws IOException {
+	// TODO: replace that dummy function with a real one.
+}
+
+protected void send(DatagramPacket packet) throws IOException
+{
+  synchronized(SEND_LOCK)
+    {
+      send0(packet);
+    }
+}
+
+protected void receive(DatagramPacket packet) throws IOException
+{
+  synchronized(RECEIVE_LOCK)
+    {
+      receive0(packet);
+    }
+}
+
+protected native int peekData(DatagramPacket p) throws IOException;
+protected native void bind(int lport, InetAddress laddr) throws SocketException;
+protected native void send0(DatagramPacket p) throws IOException;
+protected native int peek(InetAddress i) throws IOException;
+protected native void receive0(DatagramPacket p) throws IOException;
+protected native void setTTL(byte ttl) throws IOException;
+protected native byte getTTL() throws IOException;
+protected native void join(InetAddress inetaddr) throws IOException;
+protected native void leave(InetAddress inetaddr) throws IOException;
+protected native void joinGroup(SocketAddress inetaddr,
+                                NetworkInterface netIf) throws IOException;
+protected native void leaveGroup(SocketAddress inetaddr,
+                                 NetworkInterface netIf) throws IOException;
+protected native void socketSetOption(int option, Object value);
+protected native int socketGetOption(int option);
+private native void datagramSocketCreate();
+private native void datagramSocketClose();
+protected native void setBlocking(boolean block);
+
+}



More information about the kaffe mailing list