package org.mozartoz.truffle.nodes.builtins;

import static org.mozartoz.truffle.nodes.builtins.Builtin.ALL;

import org.mozartoz.truffle.nodes.OzNode;
import org.mozartoz.truffle.runtime.OzThread;

import com.oracle.truffle.api.dsl.GenerateNodeFactory;
import com.oracle.truffle.api.dsl.NodeChild;
import com.oracle.truffle.api.dsl.Specialization;

public abstract class TimeBuiltins {

	@Builtin(deref = ALL)
	@GenerateNodeFactory
	@NodeChild("delay")
	public static abstract class AlarmNode extends OzNode {

		@Specialization
		Object alarm(long delay) {
			long end = now() + delay;
			while (now() < end) {
				sleep();
				OzThread.getCurrent().yield();
			}
			return unit;
		}

		private long now() {
			return System.currentTimeMillis();
		}

		private void sleep() {
			try {
				Thread.sleep(1);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}

	}

	@GenerateNodeFactory
	public static abstract class GetReferenceTimeNode extends OzNode {

		@Specialization
		Object getReferenceTime() {
			return unimplemented();
		}

	}

	@GenerateNodeFactory
	public static abstract class GetMonotonicTimeNode extends OzNode {

		@Specialization
		Object getMonotonicTime() {
			return unimplemented();
		}

	}

}

@@ -1160,30 +1160,28 @@
 alization%0A%09%09
-Object
+long
  getMonotoni
@@ -1196,37 +1196,39 @@
 %7B%0A%09%09%09return 
-unimplemented
+System.nanoTime
 ();%0A%09%09%7D%0A%0A%09%7D%0A

Implement Boot_Time.getMonotonicTime