package com.oracle.truffle.api.dsl.test;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.ReentrantLock;
import org.junit.Assert;
import org.junit.Test;
import com.oracle.truffle.api.CallTarget;
import com.oracle.truffle.api.dsl.NodeChild;
import com.oracle.truffle.api.dsl.NodeChildren;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.dsl.TypeSystemReference;
import com.oracle.truffle.api.dsl.test.SynchronizationTestFactory.NotifyNodeFactory;
import com.oracle.truffle.api.dsl.test.SynchronizationTestFactory.WaitNodeFactory;
import com.oracle.truffle.api.dsl.test.TypeSystemTest.ValueNode;
import com.oracle.truffle.api.dsl.test.examples.ExampleTypes;
public class SynchronizationTest {
@TypeSystemReference(ExampleTypes.class)
@NodeChildren({@NodeChild("monitor"), @NodeChild("latch")})
abstract static class WaitNode extends ValueNode {
public abstract Object executeEvaluated(Object monitor, CountDownLatch latch);
@SuppressWarnings("deprecation")
@Specialization
Object doWait(Object monitor, CountDownLatch latch) {
Assert.assertEquals(false, Thread.holdsLock(getLock()));
final boolean holdsLock = ((ReentrantLock) getLock()).isHeldByCurrentThread();
Assert.assertEquals(false, holdsLock);
synchronized (monitor) {
latch.countDown();
try {
monitor.wait();
} catch (InterruptedException e) {
throw new Error(e);
}
}
return monitor;
}
}
@TypeSystemReference(ExampleTypes.class)
@NodeChildren({@NodeChild("monitor")})
abstract static class NotifyNode extends ValueNode {
public abstract Object executeEvaluated(Object monitor);
@SuppressWarnings("deprecation")
@Specialization
Object doNotify(Object monitor) {
Assert.assertEquals(false, Thread.holdsLock(getLock()));
final boolean holdsLock = ((ReentrantLock) getLock()).isHeldByCurrentThread();
Assert.assertEquals(false, holdsLock);
synchronized (monitor) {
monitor.notify();
}
return monitor;
}
}
static class IfNode extends ValueNode {
@Child WaitNode waitNode = WaitNodeFactory.create(null, null);
@Child NotifyNode notifyNode = NotifyNodeFactory.create(null);
}
@Test
public void testFirstExecutionDoesNotHoldLock() throws InterruptedException {
final Object monitor = new Object();
final CountDownLatch latch = new CountDownLatch(1);
final IfNode ifNode = new IfNode();
@SuppressWarnings("unused")
final CallTarget callTarget = TestHelper.createCallTarget(ifNode);
Thread waitThread = new Thread(new Runnable() {
@Override
public void run() {
ifNode.waitNode.executeEvaluated(monitor, latch);
}
});
waitThread.start();
Thread notifyThread = new Thread(new Runnable() {
@Override
public void run() {
ifNode.notifyNode.executeEvaluated(monitor);
}
});
latch.await();
notifyThread.start();
waitThread.join();
notifyThread.join();
}
}