[Rxtx] Deadlock in CommPortIdentifier.open with 3 threads ?
Greg Hanna
gjhanna at gmail.com
Thu Mar 8 13:42:52 MST 2007
I've just struck an issue using rxtx in a multi-threaded environment:
If I have 3 threads trying to open *the same* comm-port I get a deadlock in
CommPortIdentifier.open
If I have just 2, it works fine - I get a PortInUseException which is nice
and I can handle it.
The 3 thread->deadlock thing is causing me issues.
(Sample code below).
This is from the java thread-dump
Found one Java-level deadlock:
=============================
"Thread-2":
waiting to lock monitor 0x0003e9ec (object 0x240b08e8, a java.lang.Object
),
which is held by "Thread-1"
"Thread-1":
waiting to lock monitor 0x0003e9cc (object 0x2450e108, a
gnu.io.CommPortIdentifier),
which is held by "Thread-2"
Java stack information for the threads listed above:
===================================================
"Thread-2":
at gnu.io.CommPortIdentifier.open(CommPortIdentifier.java:329)
- waiting to lock <0x240b08e8> (a java.lang.Object)
- locked <0x2450e108> (a gnu.io.CommPortIdentifier)
at OpenThread.run(RXTXLockTest.java:42)
"Thread-1":
at java.lang.Object.wait(Native Method)
- waiting on <0x2450e108> (a gnu.io.CommPortIdentifier)
at gnu.io.CommPortIdentifier.open(CommPortIdentifier.java:332)
- locked <0x240b08e8> (a java.lang.Object)
- locked <0x2450e108> (a gnu.io.CommPortIdentifier)
at OpenThread.run(RXTXLockTest.java:42)
(Apart from this, rxtx is working fantastically for us).
I'm thinking I'll have to implement some sort of locking mechanism that
*doesn't* use java synchronization to prevent this situation. (Which
doesn't sound like a good idea to me).
Any ideas?
Thanks,
Greg Hanna
============= RXTXLockTest.java ========================================
import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
/**
* @author Greg Hanna
* Minimal code to create a deadlock with rxtx in multithreaded open
*/
public class RXTXLockTest {
public static void main(String[] args) {
try {
CommPortIdentifier cpiCom1 =
CommPortIdentifier.getPortIdentifier ("COM14");
OpenThread t1 = new OpenThread (" one ", cpiCom1);
OpenThread t2 = new OpenThread (" two ", cpiCom1);
OpenThread t3 = new OpenThread (" 003 ", cpiCom1);
t1.start();
t2.start();
t3.start(); // comment this out and it runs with (expected)
PortInUseException
} catch (Exception e) {
e.printStackTrace();
}
}
}
class OpenThread extends Thread {
String name;
CommPortIdentifier cpi;
CommPort cp;
public OpenThread (String name, CommPortIdentifier cpi) {
this.name = name;
this.cpi = cpi;
System.out.println (System.currentTimeMillis() + name + " thread
created");
}
public void run () {
try {
System.out.println (System.currentTimeMillis() + name + "
opening");
cp = cpi.open(name, 2000);
System.out.println (System.currentTimeMillis() + name + " opened
- sleeping");
sleep (2000); // do the serial comms with the device.
System.out.println (System.currentTimeMillis() + name + " slept
- closing");
cp.close();
System.out.println (System.currentTimeMillis() + name + " closed
- exiting");
} catch (Exception e) {
System.out.println ("Error in thread: " + name );
e.printStackTrace();
}
}
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.qbang.org/pipermail/rxtx/attachments/20070309/d9a163c5/attachment-1069.htm>
More information about the Rxtx
mailing list