use of java.util.concurrent.locks.ReentrantReadWriteLock in project geode by apache.
the class DeadlockDetectorIntegrationTest method testReadLockDeadlock.
@Ignore("ReadWriteLock deadlock detection is not currently supported by DeadlockDetector")
@Test
public void testReadLockDeadlock() throws Exception {
final ReadWriteLock lock1 = new ReentrantReadWriteLock();
final ReadWriteLock lock2 = new ReentrantReadWriteLock();
Thread thread1 = new Thread() {
@Override
public void run() {
stuckThreads.add(Thread.currentThread());
lock1.readLock().lock();
Thread thread2 = new Thread() {
@Override
public void run() {
stuckThreads.add(Thread.currentThread());
lock2.readLock().lock();
try {
lock1.writeLock().tryLock(10, TimeUnit.SECONDS);
} catch (InterruptedException ignore) {
}
lock2.readLock().unlock();
}
};
thread2.start();
try {
Thread.sleep(1000);
lock2.writeLock().tryLock(10, TimeUnit.SECONDS);
} catch (InterruptedException ignore) {
}
lock1.readLock().unlock();
}
};
thread1.start();
Thread.sleep(2000);
DeadlockDetector detector = new DeadlockDetector();
detector.addDependencies(DeadlockDetector.collectAllDependencies("here"));
LinkedList<Dependency> deadlocks = detector.findDeadlock();
System.out.println("deadlocks=" + deadlocks);
assertEquals(4, detector.findDeadlock().size());
}
use of java.util.concurrent.locks.ReentrantReadWriteLock in project geode by apache.
the class ParallelGatewaySenderQueueJUnitTest method createParallelGatewaySenderQueue.
@Before
public void createParallelGatewaySenderQueue() {
cache = mock(GemFireCacheImpl.class);
sender = mock(AbstractGatewaySender.class);
CancelCriterion cancelCriterion = mock(CancelCriterion.class);
when(sender.getCancelCriterion()).thenReturn(cancelCriterion);
when(sender.getCache()).thenReturn(cache);
when(sender.getMaximumQueueMemory()).thenReturn(100);
when(sender.getLifeCycleLock()).thenReturn(new ReentrantReadWriteLock());
metaRegionFactory = mock(MetaRegionFactory.class);
queue = new ParallelGatewaySenderQueue(sender, Collections.emptySet(), 0, 1, metaRegionFactory);
}
use of java.util.concurrent.locks.ReentrantReadWriteLock in project geode by apache.
the class BucketRegionJUnitTest method setInternalRegionArguments.
@Override
protected void setInternalRegionArguments(InternalRegionArguments ira) {
// PR specific
PartitionedRegion pr = mock(PartitionedRegion.class);
BucketAdvisor ba = mock(BucketAdvisor.class);
ReadWriteLock primaryMoveLock = new ReentrantReadWriteLock();
Lock activeWriteLock = primaryMoveLock.readLock();
when(ba.getActiveWriteLock()).thenReturn(activeWriteLock);
when(ba.isPrimary()).thenReturn(true);
ira.setPartitionedRegion(pr).setPartitionedRegionBucketRedundancy(1).setBucketAdvisor(ba);
}
use of java.util.concurrent.locks.ReentrantReadWriteLock in project ignite by apache.
the class GridOffheapSnapTreeSelfTest method testMemoryMultithreaded.
/**
* Test for memory leaks.
*
* @throws Exception If failed.
*/
@SuppressWarnings("TypeMayBeWeakened")
public void testMemoryMultithreaded() throws Exception {
final TestPointerFactory f = new TestPointerFactory(1000);
final GridUnsafeMemory mem = new GridUnsafeMemory(25000);
final GridUnsafeGuard guard = new GridUnsafeGuard();
final GridOffHeapSnapTreeMap<TestPointer, TestPointer> m = new GridOffHeapSnapTreeMap<>(f, f, mem, guard);
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
for (int j = 1; j < 20; j++) {
final int max = Math.min(1000, j * 20);
multithreaded(new Runnable() {
@SuppressWarnings({ "LockAcquiredButNotSafelyReleased", "StatementWithEmptyBody", "UnusedDeclaration", "unchecked" })
@Override
public void run() {
Random rnd = new Random();
for (int i = 0; i < 100000; i++) {
int x = 1 + rnd.nextInt(max);
TestPointer fx = f.createPointer(x);
boolean put = rnd.nextInt(2) != 0;
lock.readLock().lock();
guard.begin();
try {
if (put)
m.put(fx, fx);
else
m.remove(fx);
} finally {
lock.readLock().unlock();
guard.end();
}
if (i % 100 == 0) {
lock.writeLock().lock();
GridOffHeapSnapTreeMap<TestPointer, TestPointer> m2;
try {
m.validate();
m2 = m.clone();
assert m2.equals(m);
} finally {
lock.writeLock().unlock();
}
m2.validate();
for (GridOffHeapSmartPointer p : m2.values()) assertTrue(((TestPointer) p).refs.get() >= 2);
m2.close();
}
}
}
}, 29);
m.validate();
assertEquals(m.size(), m.nodes(true));
assertFalse(mem.allocatedSize() == 0);
X.println(String.valueOf(mem.allocatedSize()));
int refs = 0;
for (TestPointer ptr : f.ptrs) refs += ptr.refs.get();
assertEquals(m.size() * 2 + (m.nodes(false) - m.size()), refs);
if (j % 2 == 0)
continue;
guard.begin();
try {
for (int i = 1; i <= max; i++) m.remove(f.createPointer(i));
} finally {
guard.end();
}
assertEquals(0, m.size());
assertTrue(m.isEmpty());
assertEquals(0, m.nodes(false));
for (TestPointer ptr : f.ptrs) {
refs = ptr.refs.get();
assertEquals(0, refs);
}
}
m.close();
assertEquals(0, mem.allocatedSize());
}
use of java.util.concurrent.locks.ReentrantReadWriteLock in project ignite by apache.
the class QueueSizeCounterMultiThreadedTest method testQueueSizeCounter.
/**
* @throws Exception If failed.
*/
@SuppressWarnings({ "LockAcquiredButNotSafelyReleased" })
public void testQueueSizeCounter() throws Exception {
final ConcurrentLinkedQueue<Integer> q = new ConcurrentLinkedQueue<>();
final AtomicInteger sizeCnt = new AtomicInteger();
final AtomicBoolean done = new AtomicBoolean();
final AtomicBoolean guard = new AtomicBoolean();
final ReadWriteLock lock = new ReentrantReadWriteLock();
IgniteInternalFuture fut1 = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {
@SuppressWarnings({ "BusyWait" })
@Nullable
@Override
public Object call() throws Exception {
int cleanUps = 0;
while (!done.get()) {
lock.readLock().lock();
try {
q.add(1);
sizeCnt.incrementAndGet();
} finally {
lock.readLock().unlock();
}
if (sizeCnt.get() > 100 && guard.compareAndSet(false, true)) {
lock.writeLock().lock();
try {
for (Integer i = q.poll(); i != null; i = q.poll()) sizeCnt.decrementAndGet();
cleanUps++;
assert sizeCnt.get() == 0 : "Invalid count [cnt=" + sizeCnt.get() + ", size=" + q.size() + ", entries=" + q + ']';
} finally {
lock.writeLock().unlock();
guard.set(false);
}
}
}
X.println("Cleanups count (per thread): " + cleanUps);
return null;
}
}, 100, "test-thread");
Thread.sleep(3 * 60 * 1000);
done.set(true);
fut1.get();
}
Aggregations