mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-29 19:45:01 +02:00
[291241] MultiRequestMonitor is susceptible to theoretical race condition failure
This commit is contained in:
parent
0b19264891
commit
db1333778e
3 changed files with 101 additions and 21 deletions
|
@ -101,6 +101,7 @@ public abstract class AbstractExpressionVMNode extends AbstractDMVMNode
|
||||||
update.done();
|
update.done();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
multiRm.requireDoneAdding();
|
||||||
|
|
||||||
for (Object element : elements) {
|
for (Object element : elements) {
|
||||||
testElementForExpression(
|
testElementForExpression(
|
||||||
|
@ -113,6 +114,7 @@ public abstract class AbstractExpressionVMNode extends AbstractDMVMNode
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
multiRm.doneAdding();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -154,6 +154,7 @@ public class DefaultVMContentProviderStrategy implements IElementContentProvider
|
||||||
update.done();
|
update.done();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
hasChildrenMultiRequestMon.requireDoneAdding();
|
||||||
|
|
||||||
for (int j = 0; j < childNodes.length; j++) {
|
for (int j = 0; j < childNodes.length; j++) {
|
||||||
elementsUpdates[j][i] = new VMHasChildrenUpdate(update, hasChildrenMultiRequestMon
|
elementsUpdates[j][i] = new VMHasChildrenUpdate(update, hasChildrenMultiRequestMon
|
||||||
|
@ -164,6 +165,7 @@ public class DefaultVMContentProviderStrategy implements IElementContentProvider
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
hasChildrenMultiRequestMon.doneAdding();
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int j = 0; j < childNodes.length; j++) {
|
for (int j = 0; j < childNodes.length; j++) {
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
package org.eclipse.cdt.dsf.concurrent;
|
package org.eclipse.cdt.dsf.concurrent;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -28,9 +29,10 @@ import org.eclipse.core.runtime.MultiStatus;
|
||||||
* System.out.println("All complete, errors=" + !getStatus().isOK());
|
* System.out.println("All complete, errors=" + !getStatus().isOK());
|
||||||
* }
|
* }
|
||||||
* };
|
* };
|
||||||
|
* multiReqMon.requireDoneAdding();
|
||||||
*
|
*
|
||||||
* for (int i = 0; i < 10; i++) {
|
* for (int i = 0; i < 10; i++) {
|
||||||
* service.call(i, multiRequestMon.addRequestMonitor(
|
* service.call(i, multiRequestMon.add(
|
||||||
* new RequestMonitor(fExecutor, null) {
|
* new RequestMonitor(fExecutor, null) {
|
||||||
* public void handleCompleted() {
|
* public void handleCompleted() {
|
||||||
* System.out.println(Integer.toString(i) + " complete");
|
* System.out.println(Integer.toString(i) + " complete");
|
||||||
|
@ -38,15 +40,28 @@ import org.eclipse.core.runtime.MultiStatus;
|
||||||
* }
|
* }
|
||||||
* }));
|
* }));
|
||||||
* }
|
* }
|
||||||
|
*
|
||||||
|
* multiReqMon.doneAdding();
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
* @since 1.0
|
* @since 1.0
|
||||||
*/
|
*/
|
||||||
public class MultiRequestMonitor<V extends RequestMonitor> extends RequestMonitor {
|
public class MultiRequestMonitor<V extends RequestMonitor> extends RequestMonitor {
|
||||||
private List<V> fRequestMonitorList = new LinkedList<V>();
|
private List<V> fRequestMonitorList = Collections.synchronizedList(new LinkedList<V>());
|
||||||
private Map<V,Boolean> fStatusMap = new HashMap<V,Boolean>();
|
private Map<V,Boolean> fStatusMap = Collections.synchronizedMap(new HashMap<V,Boolean>());
|
||||||
private int fDoneCounter;
|
private int fDoneCounter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Has client called {@link #requireDoneAdding()}? Should be set right
|
||||||
|
* after construction so no need to synchronize
|
||||||
|
*/
|
||||||
|
private boolean fRequiresDoneAdding;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Has client called {@link #doneAdding()}?
|
||||||
|
*/
|
||||||
|
private boolean fDoneAdding;
|
||||||
|
|
||||||
public MultiRequestMonitor(Executor executor, RequestMonitor parentRequestMonitor) {
|
public MultiRequestMonitor(Executor executor, RequestMonitor parentRequestMonitor) {
|
||||||
super(executor, parentRequestMonitor);
|
super(executor, parentRequestMonitor);
|
||||||
setStatus(new MultiStatus(DsfPlugin.PLUGIN_ID, 0, "Collective status for set of sub-operations.", null)); //$NON-NLS-1$
|
setStatus(new MultiStatus(DsfPlugin.PLUGIN_ID, 0, "Collective status for set of sub-operations.", null)); //$NON-NLS-1$
|
||||||
|
@ -60,8 +75,11 @@ public class MultiRequestMonitor<V extends RequestMonitor> extends RequestMonito
|
||||||
* @return The request monitor that was just added, it allows this method to be used
|
* @return The request monitor that was just added, it allows this method to be used
|
||||||
* inlined in service method calls
|
* inlined in service method calls
|
||||||
*/
|
*/
|
||||||
public <T extends V> T add(T rm) {
|
public synchronized <T extends V> T add(T rm) {
|
||||||
assert !fStatusMap.containsKey(rm);
|
assert !fStatusMap.containsKey(rm);
|
||||||
|
if (fDoneAdding) {
|
||||||
|
throw new IllegalStateException("Can't add a monitor after having called doneAdding()"); //$NON-NLS-1$
|
||||||
|
}
|
||||||
fRequestMonitorList.add(rm);
|
fRequestMonitorList.add(rm);
|
||||||
fStatusMap.put(rm, false);
|
fStatusMap.put(rm, false);
|
||||||
fDoneCounter++;
|
fDoneCounter++;
|
||||||
|
@ -75,25 +93,41 @@ public class MultiRequestMonitor<V extends RequestMonitor> extends RequestMonito
|
||||||
* <br>
|
* <br>
|
||||||
* @param requestMonitor
|
* @param requestMonitor
|
||||||
*/
|
*/
|
||||||
public synchronized void requestMonitorDone(V requestMonitor) {
|
public void requestMonitorDone(V requestMonitor) {
|
||||||
if (getStatus() instanceof MultiStatus) {
|
// Avoid holding object lock while calling our super's done()
|
||||||
((MultiStatus)getStatus()).merge(requestMonitor.getStatus());
|
boolean callSuperDone = false;
|
||||||
}
|
|
||||||
assert fStatusMap.containsKey(requestMonitor);
|
synchronized (this) {
|
||||||
fStatusMap.put(requestMonitor, true);
|
if (getStatus() instanceof MultiStatus) {
|
||||||
assert fDoneCounter > 0;
|
((MultiStatus)getStatus()).merge(requestMonitor.getStatus());
|
||||||
fDoneCounter--;
|
}
|
||||||
if (fDoneCounter == 0) {
|
|
||||||
assert !fStatusMap.containsValue(false);
|
if (!fStatusMap.containsKey(requestMonitor)) {
|
||||||
super.done();
|
throw new IllegalArgumentException("Unknown monitor."); //$NON-NLS-1$
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fStatusMap.put(requestMonitor, true);
|
||||||
|
assert fDoneCounter > 0;
|
||||||
|
fDoneCounter--;
|
||||||
|
if (fDoneCounter == 0 && (fDoneAdding || !fRequiresDoneAdding)) {
|
||||||
|
assert !fStatusMap.containsValue(false);
|
||||||
|
callSuperDone = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (callSuperDone) {
|
||||||
|
super.done();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the list of requested monitors, sorted in order as they were added.
|
* Returns the list of requested monitors, sorted in order as they were
|
||||||
*/
|
* added. The returned list is a copy.
|
||||||
|
*/
|
||||||
public List<V> getRequestMonitors() {
|
public List<V> getRequestMonitors() {
|
||||||
return fRequestMonitorList;
|
synchronized (fRequestMonitorList) { // needed while copying, even when list is a synchronized collection
|
||||||
|
return new LinkedList<V>(fRequestMonitorList);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -103,6 +137,48 @@ public class MultiRequestMonitor<V extends RequestMonitor> extends RequestMonito
|
||||||
return fStatusMap.get(rm);
|
return fStatusMap.get(rm);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* In order to avoid a theoretical (but unlikely) race condition failure,
|
||||||
|
* clients should call this method immediately after creating the monitor.
|
||||||
|
* Doing so will require the client to use {@link #doneAdding()} to indicate
|
||||||
|
* it has finished adding monitors via {@link #add(RequestMonitor)}
|
||||||
|
*
|
||||||
|
* @since 2.1
|
||||||
|
*/
|
||||||
|
public void requireDoneAdding() {
|
||||||
|
fRequiresDoneAdding = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Client should call this after it has finished adding monitors to this
|
||||||
|
* MultiRequestMonitor. The client must have first called
|
||||||
|
* {@link #requireDoneAdding()}, otherwise an {@link IllegalStateException}
|
||||||
|
* is thrown
|
||||||
|
*
|
||||||
|
* @since 2.1
|
||||||
|
*/
|
||||||
|
public void doneAdding() {
|
||||||
|
// Avoid holding object lock while calling our super's done().
|
||||||
|
boolean callSuperDone = false;
|
||||||
|
|
||||||
|
synchronized (this) {
|
||||||
|
if (!fRequiresDoneAdding) {
|
||||||
|
throw new IllegalStateException("The method requiresDoneAdding() must be called first"); //$NON-NLS-1$
|
||||||
|
}
|
||||||
|
fDoneAdding = true;
|
||||||
|
|
||||||
|
// In theory, the added monitors may have already completed.
|
||||||
|
if (fDoneCounter == 0) {
|
||||||
|
assert !fStatusMap.containsValue(false);
|
||||||
|
callSuperDone = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (callSuperDone) {
|
||||||
|
super.done();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Multi-RequestMonitor: " + getStatus().toString(); //$NON-NLS-1$
|
return "Multi-RequestMonitor: " + getStatus().toString(); //$NON-NLS-1$
|
||||||
|
|
Loading…
Add table
Reference in a new issue