Skip to content

Commit df4589c

Browse files
committed
Provide condition variable abstraction on JVM.
1 parent d1dbdf3 commit df4589c

File tree

5 files changed

+87
-0
lines changed

5 files changed

+87
-0
lines changed

src/vm/jvm/QAST/Compiler.nqp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2459,6 +2459,10 @@ QAST::OperationsJAST.map_classlib_core_op('threadyield', $TYPE_OPS, 'threadyield
24592459
QAST::OperationsJAST.map_classlib_core_op('currentthread', $TYPE_OPS, 'currentthread', [], $RT_OBJ, :tc);
24602460
QAST::OperationsJAST.map_classlib_core_op('lock', $TYPE_OPS, 'lock', [$RT_OBJ], $RT_OBJ, :tc);
24612461
QAST::OperationsJAST.map_classlib_core_op('unlock', $TYPE_OPS, 'unlock', [$RT_OBJ], $RT_OBJ, :tc);
2462+
QAST::OperationsJAST.map_classlib_core_op('getlockcondvar', $TYPE_OPS, 'getlockcondvar', [$RT_OBJ, $RT_OBJ], $RT_OBJ, :tc);
2463+
QAST::OperationsJAST.map_classlib_core_op('condwait', $TYPE_OPS, 'condwait', [$RT_OBJ], $RT_OBJ, :tc);
2464+
QAST::OperationsJAST.map_classlib_core_op('condsignalone', $TYPE_OPS, 'condsignalone', [$RT_OBJ], $RT_OBJ, :tc);
2465+
QAST::OperationsJAST.map_classlib_core_op('condsignalall', $TYPE_OPS, 'condsignalall', [$RT_OBJ], $RT_OBJ, :tc);
24622466
QAST::OperationsJAST.map_classlib_core_op('semacquire', $TYPE_OPS, 'semacquire', [$RT_OBJ], $RT_OBJ, :tc);
24632467
QAST::OperationsJAST.map_classlib_core_op('semtryacquire', $TYPE_OPS, 'semtryacquire', [$RT_OBJ], $RT_INT, :tc);
24642468
QAST::OperationsJAST.map_classlib_core_op('semrelease', $TYPE_OPS, 'semrelease', [$RT_OBJ], $RT_OBJ, :tc);

src/vm/jvm/runtime/org/perl6/nqp/runtime/Ops.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@
9191
import org.perl6.nqp.sixmodel.reprs.ReentrantMutexInstance;
9292
import org.perl6.nqp.sixmodel.reprs.SemaphoreInstance;
9393
import org.perl6.nqp.sixmodel.reprs.ConcBlockingQueueInstance;
94+
import org.perl6.nqp.sixmodel.reprs.ConditionVariable;
95+
import org.perl6.nqp.sixmodel.reprs.ConditionVariableInstance;
9496

9597
/**
9698
* Contains complex operations that are more involved that the simple ops that the
@@ -4266,6 +4268,41 @@ public static SixModelObject unlock(SixModelObject lock, ThreadContext tc) {
42664268
return lock;
42674269
}
42684270

4271+
public static SixModelObject getlockcondvar(SixModelObject lock, SixModelObject type, ThreadContext tc) {
4272+
if (!(lock instanceof ReentrantMutexInstance))
4273+
throw ExceptionHandling.dieInternal(tc, "getlockcondvar requires an operand with REPR ReentrantMutex");
4274+
if (!(type.st.REPR instanceof ConditionVariable))
4275+
throw ExceptionHandling.dieInternal(tc, "getlockcondvar requires a result type with REPR ConditionVariable");
4276+
ConditionVariableInstance result = new ConditionVariableInstance();
4277+
result.st = type.st;
4278+
result.condvar = ((ReentrantMutexInstance)lock).lock.newCondition();
4279+
return result;
4280+
}
4281+
4282+
public static SixModelObject condwait(SixModelObject cv, ThreadContext tc) throws InterruptedException {
4283+
if (cv instanceof ConditionVariableInstance)
4284+
((ConditionVariableInstance)cv).condvar.await();
4285+
else
4286+
throw ExceptionHandling.dieInternal(tc, "condwait requires an operand with REPR ConditionVariable");
4287+
return cv;
4288+
}
4289+
4290+
public static SixModelObject condsignalone(SixModelObject cv, ThreadContext tc) {
4291+
if (cv instanceof ConditionVariableInstance)
4292+
((ConditionVariableInstance)cv).condvar.signal();
4293+
else
4294+
throw ExceptionHandling.dieInternal(tc, "condsignalone requires an operand with REPR ConditionVariable");
4295+
return cv;
4296+
}
4297+
4298+
public static SixModelObject condsignalall(SixModelObject cv, ThreadContext tc) {
4299+
if (cv instanceof ConditionVariableInstance)
4300+
((ConditionVariableInstance)cv).condvar.signalAll();
4301+
else
4302+
throw ExceptionHandling.dieInternal(tc, "condsignalall requires an operand with REPR ConditionVariable");
4303+
return cv;
4304+
}
4305+
42694306
public static SixModelObject semacquire(SixModelObject sem, ThreadContext tc) {
42704307
try {
42714308
if (sem instanceof SemaphoreInstance)

src/vm/jvm/runtime/org/perl6/nqp/sixmodel/REPRRegistry.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.perl6.nqp.sixmodel.reprs.ReentrantMutex;
3535
import org.perl6.nqp.sixmodel.reprs.Semaphore;
3636
import org.perl6.nqp.sixmodel.reprs.ConcBlockingQueue;
37+
import org.perl6.nqp.sixmodel.reprs.ConditionVariable;
3738

3839
public class REPRRegistry {
3940
private static HashMap<String, Integer> reprIdMap = new HashMap<String, Integer>();
@@ -92,5 +93,6 @@ private static void addREPR(String name, REPR REPR) {
9293
addREPR("ReentrantMutex", new ReentrantMutex());
9394
addREPR("Semaphore", new Semaphore());
9495
addREPR("ConcBlockingQueue", new ConcBlockingQueue());
96+
addREPR("ConditionVariable", new ConditionVariable());
9597
}
9698
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.perl6.nqp.sixmodel.reprs;
2+
3+
import java.util.concurrent.locks.ReentrantLock;
4+
5+
import org.perl6.nqp.runtime.ExceptionHandling;
6+
import org.perl6.nqp.runtime.ThreadContext;
7+
import org.perl6.nqp.sixmodel.REPR;
8+
import org.perl6.nqp.sixmodel.STable;
9+
import org.perl6.nqp.sixmodel.SerializationReader;
10+
import org.perl6.nqp.sixmodel.SixModelObject;
11+
import org.perl6.nqp.sixmodel.TypeObject;
12+
13+
public class ConditionVariable extends REPR {
14+
public SixModelObject type_object_for(ThreadContext tc, SixModelObject HOW) {
15+
STable st = new STable(this, HOW);
16+
SixModelObject obj = new TypeObject();
17+
obj.st = st;
18+
st.WHAT = obj;
19+
return st.WHAT;
20+
}
21+
22+
public SixModelObject allocate(ThreadContext tc, STable st) {
23+
throw ExceptionHandling.dieInternal(tc,
24+
"Cannot create a condition variable directly");
25+
}
26+
27+
public SixModelObject deserialize_stub(ThreadContext tc, STable st) {
28+
throw ExceptionHandling.dieInternal(tc, "Cannot deserialize a lock");
29+
}
30+
31+
public void deserialize_finish(ThreadContext tc, STable st,
32+
SerializationReader reader, SixModelObject obj) {
33+
throw ExceptionHandling.dieInternal(tc, "Cannot deserialize a lock");
34+
}
35+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.perl6.nqp.sixmodel.reprs;
2+
3+
import java.util.concurrent.locks.Condition;
4+
5+
import org.perl6.nqp.sixmodel.SixModelObject;
6+
7+
public class ConditionVariableInstance extends SixModelObject {
8+
public Condition condvar;
9+
}

0 commit comments

Comments
 (0)