[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Inappropriate use of synchronized in PlainSocketImpl: Submit for Classp
From: |
Wu, Gansha |
Subject: |
Inappropriate use of synchronized in PlainSocketImpl: Submit for Classpath |
Date: |
Thu, 28 Jun 2001 10:33:22 +0800 |
We have modified GNU Classpath so that ORP with Classpath can run Volano.
Here is the first of the patches needed for that.
We found that Classpath's implementation of PlainSocketImpl(in
java/net/PlainsocketImpl.java) overused synchronized keyword before almost all
the native methods such as read, write, etc.. Generally speaking native socket
implementation(e.g., Winsock) takes the responsibility for thread-safety, and
if we raise the responsibility up to Java language level, there'll be problems
here in multi-threaded senarios.
The following is a simple senario in Volano benchmark which will lead to
deadlock using current implementation of classpath:
Server: | Client:
| Thread A: Thread B:
... ... | sock1.read(.....).
| //waiting for item B
| sock1.write(.....).
sock.read(.....). | //sending item A
//waiting for item A |
sock.write(.....). \/
//sending item B time
Obviously in thread A, "sock1" blocks on read, and write of thread B will
block on the same "sock1", and Server will block in turn.
In a word, if different threads read/write the same socket, current
implementation will potentially cause dead-lock.
So our suggestion is to remove synchronized keyword from read/write native
methods.
The context compare result below is based on gnu classpath cvs snapshot of
June 22.Note that revision number is different
Index: java/net/PlainSocketImpl.java
===================================================================
RCS file: /cvsroot/classpath/java/net/PlainSocketImpl.java,v
retrieving revision 1.1.1.1
diff -c -r1.1.1.1 PlainSocketImpl.java
*** java/net/PlainSocketImpl.java 2001/06/25 01:13:33 1.1.1.1
--- java/net/PlainSocketImpl.java 2001/06/25 01:24:32
***************
*** 204,210 ****
*
* @exception IOException If an error occurs
*/
! protected native synchronized int read(byte[] buf, int offset, int len)
throws IOException;
/*************************************************************************/
--- 204,210 ----
*
* @exception IOException If an error occurs
*/
! protected native int read(byte[] buf, int offset, int len) throws IOException;
/*************************************************************************/
***************
*** 215,221 ****
*
* @exception IOException If an error occurs
*/
! protected native synchronized void write(byte[] buf, int offset, int len)
throws IOException;
/*************************************************************************/
--- 215,221 ----
*
* @exception IOException If an error occurs
*/
! protected native void write(byte[] buf, int offset, int len) throws
IOException;
/*************************************************************************/
- Inappropriate use of synchronized in PlainSocketImpl: Submit for Classpath,
Wu, Gansha <=