mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2024-12-24 13:27:59 +01:00
Replaces lock object with Semaphore. SMACK-168
git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@5359 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
parent
0c71fe2624
commit
e558ee8fa6
1 changed files with 16 additions and 26 deletions
|
@ -31,6 +31,8 @@ import org.xmlpull.v1.XmlPullParserException;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.Semaphore;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* Listens for XML traffic from the XMPP server and parses it into packet objects.
|
||||
|
@ -55,7 +57,7 @@ class PacketReader {
|
|||
new ArrayList<ConnectionListener>();
|
||||
|
||||
private String connectionID = null;
|
||||
private final Object connectionIDLock = new Object();
|
||||
private Semaphore connectionSemaphore;
|
||||
|
||||
protected PacketReader(XMPPConnection connection) {
|
||||
this.connection = connection;
|
||||
|
@ -135,31 +137,21 @@ class PacketReader {
|
|||
* for more than five seconds.
|
||||
*/
|
||||
public void startup() throws XMPPException {
|
||||
connectionSemaphore = new Semaphore(1);
|
||||
|
||||
readerThread.start();
|
||||
listenerThread.start();
|
||||
// Wait for stream tag before returing. We'll wait a couple of seconds before
|
||||
// giving up and throwing an error.
|
||||
try {
|
||||
synchronized(connectionIDLock) {
|
||||
if (connectionID == null) {
|
||||
connectionSemaphore.acquire();
|
||||
|
||||
// A waiting thread may be woken up before the wait time or a notify
|
||||
// (although this is a rare thing). Therefore, we continue waiting
|
||||
// until either a connectionID has been set (and hence a notify was
|
||||
// made) or the total wait time has elapsed.
|
||||
long waitTime = SmackConfiguration.getPacketReplyTimeout();
|
||||
long start = System.currentTimeMillis();
|
||||
while (connectionID == null && !done) {
|
||||
if (waitTime <= 0) {
|
||||
break;
|
||||
}
|
||||
// Wait 3 times the standard time since TLS may take a while
|
||||
connectionIDLock.wait(waitTime * 3);
|
||||
long now = System.currentTimeMillis();
|
||||
waitTime -= now - start;
|
||||
start = now;
|
||||
}
|
||||
}
|
||||
}
|
||||
int waitTime = SmackConfiguration.getPacketReplyTimeout();
|
||||
connectionSemaphore.tryAcquire(3 * waitTime, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
catch (InterruptedException ie) {
|
||||
// Ignore.
|
||||
|
@ -390,9 +382,7 @@ class PacketReader {
|
|||
*
|
||||
*/
|
||||
private void releaseConnectionIDLock() {
|
||||
synchronized(connectionIDLock) {
|
||||
connectionIDLock.notifyAll();
|
||||
}
|
||||
connectionSemaphore.release();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue