1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-23 22:52:11 +02:00

Bug 310345 - [concurrent] Asynchronous Cache Programming Model (ACPM) utilities for DSF

- Removed the hidden handling of DataRequestMonitor in AbstractCache.update().
This commit is contained in:
Pawel Piech 2010-10-20 15:59:09 +00:00
parent ea02d217d5
commit a24bb7ea72
3 changed files with 54 additions and 84 deletions

View file

@ -156,11 +156,6 @@ public abstract class AbstractCache<V> implements ICache<V> {
} }
private void completeWaitingRm(RequestMonitor rm) { private void completeWaitingRm(RequestMonitor rm) {
if (rm instanceof DataRequestMonitor<?>) {
@SuppressWarnings("unchecked")
DataRequestMonitor<V> drm = (DataRequestMonitor<V>)rm;
drm.setData(fData);
}
rm.setStatus(fStatus); rm.setStatus(fStatus);
rm.removeCancelListener(fRequestCanceledListener); rm.removeCancelListener(fRequestCanceledListener);
rm.done(); rm.done();

View file

@ -25,6 +25,7 @@ import org.eclipse.cdt.dsf.concurrent.ImmediateExecutor;
import org.eclipse.cdt.dsf.concurrent.ImmediateInDsfExecutor; import org.eclipse.cdt.dsf.concurrent.ImmediateInDsfExecutor;
import org.eclipse.cdt.dsf.concurrent.Query; import org.eclipse.cdt.dsf.concurrent.Query;
import org.eclipse.cdt.dsf.concurrent.RequestCache; import org.eclipse.cdt.dsf.concurrent.RequestCache;
import org.eclipse.cdt.dsf.concurrent.RequestMonitor;
import org.eclipse.cdt.tests.dsf.TestDsfExecutor; import org.eclipse.cdt.tests.dsf.TestDsfExecutor;
import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status; import org.eclipse.core.runtime.Status;
@ -67,6 +68,19 @@ public class CacheTests {
} }
class TestQuery extends Query<Integer> {
@Override
protected void execute(final DataRequestMonitor<Integer> rm) {
fTestCache.update(new RequestMonitor(ImmediateExecutor.getInstance(), rm) {
@Override
protected void handleSuccess() {
rm.setData(fTestCache.getData());
rm.done();
}
});
}
}
/** /**
* There's no rule on how quickly the cache has to start data retrieval * There's no rule on how quickly the cache has to start data retrieval
* after it has been requested. It could do it immediately, or it could * after it has been requested. It could do it immediately, or it could
@ -158,12 +172,8 @@ public class CacheTests {
@Test @Test
public void getWithCompletionInDsfThreadTest() throws InterruptedException, ExecutionException { public void getWithCompletionInDsfThreadTest() throws InterruptedException, ExecutionException {
// Request data from cache // Request data from cache
Query<Integer> q = new Query<Integer>() { Query<Integer> q = new TestQuery();
@Override
protected void execute(DataRequestMonitor<Integer> rm) {
fTestCache.update(rm);
}
};
// Check initial state // Check initial state
Assert.assertFalse(fTestCache.isValid()); Assert.assertFalse(fTestCache.isValid());
@ -200,12 +210,7 @@ public class CacheTests {
Assert.assertFalse(fTestCache.isValid()); Assert.assertFalse(fTestCache.isValid());
// Request data from cache // Request data from cache
Query<Integer> q = new Query<Integer>() { Query<Integer> q = new TestQuery();
@Override
protected void execute(DataRequestMonitor<Integer> rm) {
fTestCache.update(rm);
}
};
fExecutor.execute(q); fExecutor.execute(q);
// Wait until the cache starts data retrieval. // Wait until the cache starts data retrieval.
@ -230,21 +235,11 @@ public class CacheTests {
Assert.assertFalse(fTestCache.isValid()); Assert.assertFalse(fTestCache.isValid());
// Request data from cache // Request data from cache
Query<Integer> q1 = new Query<Integer>() { Query<Integer> q1 = new TestQuery();
@Override
protected void execute(DataRequestMonitor<Integer> rm) {
fTestCache.update(rm);
}
};
fExecutor.execute(q1); fExecutor.execute(q1);
// Request data from cache again // Request data from cache again
Query<Integer> q2 = new Query<Integer>() { Query<Integer> q2 = new TestQuery();
@Override
protected void execute(DataRequestMonitor<Integer> rm) {
fTestCache.update(rm);
}
};
fExecutor.execute(q2); fExecutor.execute(q2);
// Wait until the cache starts data retrieval. // Wait until the cache starts data retrieval.
@ -272,12 +267,7 @@ public class CacheTests {
// Request data from cache // Request data from cache
List<Query<Integer>> qList = new ArrayList<Query<Integer>>(); List<Query<Integer>> qList = new ArrayList<Query<Integer>>();
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
Query<Integer> q = new Query<Integer>() { Query<Integer> q = new TestQuery();
@Override
protected void execute(DataRequestMonitor<Integer> rm) {
fTestCache.update(rm);
}
};
fExecutor.execute(q); fExecutor.execute(q);
qList.add(q); qList.add(q);
} }
@ -413,12 +403,7 @@ public class CacheTests {
@Test @Test
public void cancelWhilePendingTest() throws InterruptedException, ExecutionException { public void cancelWhilePendingTest() throws InterruptedException, ExecutionException {
// Request data from cache // Request data from cache
Query<Integer> q = new Query<Integer>() { Query<Integer> q = new TestQuery();
@Override
protected void execute(DataRequestMonitor<Integer> rm) {
fTestCache.update(rm);
}
};
fExecutor.execute(q); fExecutor.execute(q);
// Wait until the cache starts data retrieval. // Wait until the cache starts data retrieval.
@ -450,13 +435,20 @@ public class CacheTests {
// Request data from cache // Request data from cache
Query<Integer> q = new Query<Integer>() { Query<Integer> q = new Query<Integer>() {
@Override @Override
protected void execute(DataRequestMonitor<Integer> rm) { protected void execute(final DataRequestMonitor<Integer> rm) {
fTestCache.update(new DataRequestMonitor<Integer>(ImmediateExecutor.getInstance(), rm) {
fTestCache.update(new RequestMonitor(ImmediateExecutor.getInstance(), rm) {
@Override @Override
public synchronized void addCancelListener(ICanceledListener listener) { public synchronized void addCancelListener(ICanceledListener listener) {
// Do not add the cancel listener so that the cancel request is not // Do not add the cancel listener so that the cancel request is not
// propagated to the cache. // propagated to the cache.
} }
@Override
protected void handleSuccess() {
rm.setData(fTestCache.getData());
rm.done();
}
}); });
} }
}; };
@ -498,12 +490,12 @@ public class CacheTests {
// Create a client request with a badly behaved cancel implementation. // Create a client request with a badly behaved cancel implementation.
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
final DataRequestMonitor<Integer>[] rmBad = (DataRequestMonitor<Integer>[])new DataRequestMonitor<?>[1] ; final RequestMonitor[] rmBad = new RequestMonitor[1] ;
final boolean qBadCanceled[] = new boolean[] { false }; final boolean qBadCanceled[] = new boolean[] { false };
Query<Integer> qBad = new Query<Integer>() { Query<Integer> qBad = new Query<Integer>() {
@Override @Override
protected void execute(DataRequestMonitor<Integer> rm) { protected void execute(final DataRequestMonitor<Integer> rm) {
rmBad[0] = new DataRequestMonitor<Integer>(ImmediateExecutor.getInstance(), rm) { rmBad[0] = new RequestMonitor(ImmediateExecutor.getInstance(), rm) {
@Override @Override
public synchronized void removeCancelListener(ICanceledListener listener) { public synchronized void removeCancelListener(ICanceledListener listener) {
// Do not add the cancel listener so that the cancel request is not // Do not add the cancel listener so that the cancel request is not
@ -526,6 +518,11 @@ public class CacheTests {
public synchronized void done() { public synchronized void done() {
// Avoid clearing cancel listeners list // Avoid clearing cancel listeners list
}; };
protected void handleSuccess() {
rm.setData(fTestCache.getData());
rm.done();
};
}; };
fTestCache.update(rmBad[0]); fTestCache.update(rmBad[0]);
@ -545,12 +542,7 @@ public class CacheTests {
} }
}).get(); }).get();
Query<Integer> qGood = new Query<Integer>() { Query<Integer> qGood = new TestQuery();
@Override
protected void execute(DataRequestMonitor<Integer> rm) {
fTestCache.update(rm);
}
};
fExecutor.execute(qGood); fExecutor.execute(qGood);
// Wait until the cache starts data retrieval. // Wait until the cache starts data retrieval.
@ -577,21 +569,11 @@ public class CacheTests {
@Test @Test
public void cancelWhilePendingWithTwoClientsTest() throws InterruptedException, ExecutionException { public void cancelWhilePendingWithTwoClientsTest() throws InterruptedException, ExecutionException {
// Request data from cache // Request data from cache
Query<Integer> q1 = new Query<Integer>() { Query<Integer> q1 = new TestQuery();
@Override
protected void execute(DataRequestMonitor<Integer> rm) {
fTestCache.update(rm);
}
};
fExecutor.execute(q1); fExecutor.execute(q1);
// Request data from cache again // Request data from cache again
Query<Integer> q2 = new Query<Integer>() { Query<Integer> q2 = new TestQuery();
@Override
protected void execute(DataRequestMonitor<Integer> rm) {
fTestCache.update(rm);
}
};
fExecutor.execute(q2); fExecutor.execute(q2);
@ -632,12 +614,7 @@ public class CacheTests {
// Request data from cache // Request data from cache
List<Query<Integer>> qList = new ArrayList<Query<Integer>>(); List<Query<Integer>> qList = new ArrayList<Query<Integer>>();
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
Query<Integer> q = new Query<Integer>() { Query<Integer> q = new TestQuery();
@Override
protected void execute(DataRequestMonitor<Integer> rm) {
fTestCache.update(rm);
}
};
fExecutor.execute(q); fExecutor.execute(q);
qList.add(q); qList.add(q);
} }
@ -663,12 +640,7 @@ public class CacheTests {
// Replace canceled requests with new ones // Replace canceled requests with new ones
for (int i = 0; i < toCancel.length; i++) { for (int i = 0; i < toCancel.length; i++) {
Query<Integer> q = new Query<Integer>() { Query<Integer> q = new TestQuery();
@Override
protected void execute(DataRequestMonitor<Integer> rm) {
fTestCache.update(rm);
}
};
fExecutor.execute(q); fExecutor.execute(q);
qList.set(toCancel[i], q); qList.set(toCancel[i], q);
assertCacheWaiting(); assertCacheWaiting();
@ -698,12 +670,7 @@ public class CacheTests {
@Test @Test
public void resetWhileValidTest() throws InterruptedException, ExecutionException { public void resetWhileValidTest() throws InterruptedException, ExecutionException {
// Request data from cache // Request data from cache
Query<Integer> q = new Query<Integer>() { Query<Integer> q = new TestQuery();
@Override
protected void execute(DataRequestMonitor<Integer> rm) {
fTestCache.update(rm);
}
};
fExecutor.execute(q); fExecutor.execute(q);
// Wait until the cache starts data retrieval. // Wait until the cache starts data retrieval.

View file

@ -23,9 +23,11 @@ import org.eclipse.cdt.dsf.concurrent.DataRequestMonitor;
import org.eclipse.cdt.dsf.concurrent.DsfRunnable; import org.eclipse.cdt.dsf.concurrent.DsfRunnable;
import org.eclipse.cdt.dsf.concurrent.ICache; import org.eclipse.cdt.dsf.concurrent.ICache;
import org.eclipse.cdt.dsf.concurrent.IDsfStatusConstants; import org.eclipse.cdt.dsf.concurrent.IDsfStatusConstants;
import org.eclipse.cdt.dsf.concurrent.ImmediateExecutor;
import org.eclipse.cdt.dsf.concurrent.ImmediateInDsfExecutor; import org.eclipse.cdt.dsf.concurrent.ImmediateInDsfExecutor;
import org.eclipse.cdt.dsf.concurrent.Query; import org.eclipse.cdt.dsf.concurrent.Query;
import org.eclipse.cdt.dsf.concurrent.RangeCache; import org.eclipse.cdt.dsf.concurrent.RangeCache;
import org.eclipse.cdt.dsf.concurrent.RequestMonitor;
import org.eclipse.cdt.tests.dsf.DsfTestPlugin; import org.eclipse.cdt.tests.dsf.DsfTestPlugin;
import org.eclipse.cdt.tests.dsf.TestDsfExecutor; import org.eclipse.cdt.tests.dsf.TestDsfExecutor;
import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.IStatus;
@ -70,9 +72,15 @@ public class RangeCacheTests {
} }
@Override @Override
protected void execute(DataRequestMonitor<List<Integer>> rm) { protected void execute(final DataRequestMonitor<List<Integer>> rm) {
fRangeCache = fTestCache.getRange(fOffset, fCount); fRangeCache = fTestCache.getRange(fOffset, fCount);
fRangeCache.update(rm); fRangeCache.update(new RequestMonitor(ImmediateExecutor.getInstance(), rm) {
@Override
protected void handleSuccess() {
rm.setData(fRangeCache.getData());
rm.done();
}
});
} }
} }