Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Partially implement nqp::timer.
Missing cancellation-related bits so far.
  • Loading branch information
jnthn committed Apr 18, 2014
1 parent d958b6b commit 3b3ccda
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/vm/jvm/runtime/org/perl6/nqp/runtime/GlobalContext.java
Expand Up @@ -5,6 +5,7 @@
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Timer;
import java.util.WeakHashMap;

import org.perl6.nqp.sixmodel.CodePairContainerConfigurer;
Expand Down Expand Up @@ -122,6 +123,11 @@ public class GlobalContext {
*/
public ThreadContext mainThread;

/**
* Timer object, used by nqp::timer.
*/
public Timer timer;

/**
* Active HLL configuration (maps HLL name to the configuration).
*/
Expand Down Expand Up @@ -231,6 +237,7 @@ public GlobalContext()
allThreads = new WeakHashMap< >();

mainThread = getCurrentThreadContext();
timer = new Timer(true);
KnowHOWBootstrapper.bootstrap(mainThread);
bootInterop = new BootJavaInterop(this);

Expand Down
29 changes: 29 additions & 0 deletions src/vm/jvm/runtime/org/perl6/nqp/runtime/Ops.java
Expand Up @@ -31,6 +31,7 @@
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.TimerTask;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.net.InetAddress;
Expand Down Expand Up @@ -4388,6 +4389,34 @@ public static SixModelObject queuepoll(SixModelObject queue, ThreadContext tc) {
throw ExceptionHandling.dieInternal(tc, "queuepoll requires an operand with REPR ConcBlockingQueue");
}

/* Asynchronousy operations. */

private static class AddToQueueTimerTask extends TimerTask {
private LinkedBlockingQueue<SixModelObject> queue;
private SixModelObject schedulee;

public AddToQueueTimerTask(LinkedBlockingQueue<SixModelObject> queue, SixModelObject schedulee) {
this.queue = queue;
this.schedulee = schedulee;
}

public void run() {
queue.add(schedulee);
}
}
public static SixModelObject timer(SixModelObject queue, SixModelObject schedulee,
long timeout, long repeat, SixModelObject handle_type, ThreadContext tc) {
if (!(queue instanceof ConcBlockingQueueInstance))
throw ExceptionHandling.dieInternal(tc, "timer's first argument should have REPR ConcBlockingQueue");
AddToQueueTimerTask tt = new AddToQueueTimerTask(((ConcBlockingQueueInstance)queue).queue, schedulee);
if (repeat > 0)
tc.gc.timer.scheduleAtFixedRate(tt, timeout, repeat);
else
tc.gc.timer.schedule(tt, timeout);
/* XXX TODO: cancellation handle. */
return null;
}

/* Exception related. */
public static void die_s_c(String msg, ThreadContext tc) {
// Construct exception object.
Expand Down

0 comments on commit 3b3ccda

Please sign in to comment.