Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

initial code

  • Loading branch information...
commit 08aa0340fbfc4e2835446b85c83eecccd8287601 0 parents
@vladdu authored
6 .classpath
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="src" path="src"/>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
+ <classpathentry kind="output" path="bin"/>
+</classpath>
17 .project
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>je</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ </natures>
+</projectDescription>
12 .settings/org.eclipse.jdt.core.prefs
@@ -0,0 +1,12 @@
+#Fri Jan 15 10:10:46 CET 2010
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
+org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
+org.eclipse.jdt.core.compiler.compliance=1.6
+org.eclipse.jdt.core.compiler.debug.lineNumber=generate
+org.eclipse.jdt.core.compiler.debug.localVariable=generate
+org.eclipse.jdt.core.compiler.debug.sourceFile=generate
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.source=1.6
3  src/Continuation.java
@@ -0,0 +1,3 @@
+public interface Continuation {
+ public Continuation run();
+}
10 src/Fiber.java
@@ -0,0 +1,10 @@
+public abstract class Fiber {
+ public Continuation continuation;
+
+ public void start(Scheduler scheduler) {
+ continuation = run();
+ }
+
+ public abstract Continuation run();
+
+}
4 src/Func.java
@@ -0,0 +1,4 @@
+public interface Func<T1, T2> {
+ T2 exec(T1 arg);
+
+}
18 src/MailBox.java
@@ -0,0 +1,18 @@
+import java.util.ArrayDeque;
+import java.util.Queue;
+
+public class MailBox<T> {
+ private final Queue<T> queue = new ArrayDeque<T>();
+
+ public void addMessage(T msg) {
+ queue.add(msg);
+ }
+
+ public T tryGetNextMessage() {
+ try {
+ return queue.remove();
+ } catch (Exception e) {
+ return null;
+ }
+ }
+}
17 src/Main.java
@@ -0,0 +1,17 @@
+public class Main {
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args) {
+ Scheduler scheduler = new Scheduler();
+ PingPong ping = new PingPong("ping");
+ PingPong pong = new PingPong("pong");
+ ping.friend = pong;
+ pong.friend = ping;
+
+ scheduler.spawn(ping, pong);
+ scheduler.schedule();
+ }
+
+}
21 src/PingPong.java
@@ -0,0 +1,21 @@
+class PingPong extends Process<String> {
+ public Process<String> friend;
+ private final String name;
+
+ public PingPong(String name) {
+ this.name = name;
+ }
+
+ @Override
+ public Continuation run() {
+ friend.send(name);
+ return receive(new Func<String, Continuation>() {
+
+ @Override
+ public Continuation exec(String n) {
+ System.out.println(name + " received message from " + n);
+ return run();
+ }
+ });
+ }
+}
29 src/Process.java
@@ -0,0 +1,29 @@
+public abstract class Process<T> extends Fiber {
+ private final MailBox<T> mailBox;
+
+ protected Process() {
+ mailBox = new MailBox<T>();
+ }
+
+ public void send(T msg) {
+ mailBox.addMessage(msg);
+ }
+
+ protected Continuation receive(final Func<T, Continuation> continuation) {
+ final T message;
+ message = mailBox.tryGetNextMessage();
+ if (message != null) {
+ return new Continuation() {
+ public Continuation run() {
+ return continuation.exec(message);
+ }
+ };
+ } else {
+ return new Continuation() {
+ public Continuation run() {
+ return receive(continuation);
+ }
+ };
+ }
+ }
+}
35 src/Scheduler.java
@@ -0,0 +1,35 @@
+import java.util.ArrayList;
+import java.util.List;
+
+public class Scheduler {
+ private List<Fiber> fibers;
+
+ public Scheduler() {
+ fibers = new ArrayList<Fiber>();
+ }
+
+ public void schedule() {
+ while (fibers.size() > 0) {
+ List<Fiber> fibs = new ArrayList<Fiber>();
+
+ for (Fiber f : fibers) {
+ // System.out.println("-- run " + p);
+ Continuation run = f.continuation.run();
+ // System.out.println("-- got " + run);
+ if (run != null) {
+ f.continuation = run;
+ fibs.add(f);
+ }
+ }
+ fibers = fibs;
+ }
+ }
+
+ public void spawn(Fiber... procs) {
+ for (Fiber process : procs) {
+ // System.out.println("-- schedule " + process);
+ fibers.add(process);
+ process.start(this);
+ }
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.