fixed synchornization problem that in some cases lead to race definition lookups not getting unblocked properly

Change-Id: Icd0cfd3bdfb8c37a8df1997814da4aabe38c6859
This commit is contained in:
Axel Uhl committed 2017-03-02 18:59:08 +01:00
1 parent 2c42cd0653
commit cd67b2ea2f
1 file changed
+16 -12
@@ -318,21 +318,25 @@ public class DomainFactoryImpl implements DomainFactory {
long start = System.currentTimeMillis();
RaceDefinition result = raceCache.get(raceId);
boolean interrupted = false;
while ((timeoutInMilliseconds == -1 || System.currentTimeMillis()-start < timeoutInMilliseconds) && !interrupted && result == null) {
try {
synchronized (raceCache) {
if (timeoutInMilliseconds == -1) {
raceCache.wait();
} else {
long timeToWait = timeoutInMilliseconds-(System.currentTimeMillis()-start);
if (timeToWait > 0) {
raceCache.wait(timeToWait);
if (result == null) {
synchronized (raceCache) {
// try again while under raceCache's monitor; otherwise we may miss a notification
result = raceCache.get(raceId);
while ((timeoutInMilliseconds == -1 || System.currentTimeMillis()-start < timeoutInMilliseconds) && !interrupted && result == null) {
try {
if (timeoutInMilliseconds == -1) {
raceCache.wait();
} else {
long timeToWait = timeoutInMilliseconds-(System.currentTimeMillis()-start);
if (timeToWait > 0) {
raceCache.wait(timeToWait);
}
}
result = raceCache.get(raceId);
} catch (InterruptedException e) {
interrupted = true;
}
}
result = raceCache.get(raceId);
} catch (InterruptedException e) {
interrupted = true;
}
}
return result;