Mercurial > projects > dwt2
diff java/src/java/util/Timer.d @ 21:9b96950f2c3c
the 'java' tree compiles on both D1-Tango and D2-Phobos
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Thu, 19 Mar 2009 20:38:55 +0100 |
parents | 52184e4b815c |
children |
line wrap: on
line diff
--- a/java/src/java/util/Timer.d Wed Mar 18 12:10:17 2009 +0100 +++ b/java/src/java/util/Timer.d Thu Mar 19 20:38:55 2009 +0100 @@ -1,19 +1,24 @@ module java.util.Timer; +import java.lang.all; import java.util.TimerTask; -// import tango.util.container.CircularList; import java.lang.Thread; -import tango.core.sync.Mutex; -import tango.core.sync.Condition; -// import tango.time.Time; -// import tango.time.Clock; -import tango.text.convert.Format; -import java.lang.all; + +version(Tango){ + import tango.core.sync.Mutex; + import tango.core.sync.Condition; + import tango.text.convert.Format; +} else { // Phobos +} + class Timer { private static final class TaskQueue { - private Mutex mutex; - private Condition cond; + version(Tango){ + private Mutex mutex; + private Condition cond; + } else { // Phobos + } private static const int DEFAULT_SIZE = 32; @@ -21,11 +26,15 @@ private TimerTask heap[]; private int elements; public this() { - mutex = new Mutex(); - cond = new Condition( mutex ); - heap = new TimerTask[DEFAULT_SIZE]; - elements = 0; - nullOnEmpty = false; + version(Tango){ + mutex = new Mutex(); + cond = new Condition( mutex ); + heap = new TimerTask[DEFAULT_SIZE]; + elements = 0; + nullOnEmpty = false; + } else { // Phobos + implMissing(__FILE__,__LINE__); + } } private void add(TimerTask task) { @@ -50,23 +59,27 @@ } public void enqueue(TimerTask task) { - synchronized( mutex ){ - if (heap is null) { - throw new IllegalStateException("cannot enqueue when stop() has been called on queue"); - } + version(Tango){ + synchronized( mutex ){ + if (heap is null) { + throw new IllegalStateException("cannot enqueue when stop() has been called on queue"); + } - heap[0] = task; - add(task); - int child = elements; - int parent = child / 2; - while (heap[parent].scheduled > task.scheduled) { - heap[child] = heap[parent]; - child = parent; - parent = child / 2; + heap[0] = task; + add(task); + int child = elements; + int parent = child / 2; + while (heap[parent].scheduled > task.scheduled) { + heap[child] = heap[parent]; + child = parent; + parent = child / 2; + } + heap[child] = task; + heap[0] = null; + cond.notify(); } - heap[child] = task; - heap[0] = null; - cond.notify(); + } else { // Phobos + implMissing(__FILE__,__LINE__); } } @@ -80,79 +93,92 @@ } public TimerTask serve() { - synchronized( mutex ){ - TimerTask task = null; - while (task is null) { - task = top(); + version(Tango){ + synchronized( mutex ){ + TimerTask task = null; + while (task is null) { + task = top(); - if ((heap is null) || (task is null && nullOnEmpty)) { - return null; - } + if ((heap is null) || (task is null && nullOnEmpty)) { + return null; + } - if (task !is null) { - // The time to wait until the task should be served - long time = task.scheduled - System.currentTimeMillis(); - if (time > 0) { - // This task should not yet be served - // So wait until this task is ready + if (task !is null) { + // The time to wait until the task should be served + long time = task.scheduled - System.currentTimeMillis(); + if (time > 0) { + // This task should not yet be served + // So wait until this task is ready + // or something else happens to the queue + task = null; // set to null to make sure we call top() + try { + cond.wait(time); + } + catch (InterruptedException _) { + } + } + } + else { + // wait until a task is added // or something else happens to the queue - task = null; // set to null to make sure we call top() try { - cond.wait(time); + cond.wait(); } catch (InterruptedException _) { } } } - else { - // wait until a task is added - // or something else happens to the queue - try { - cond.wait(); - } - catch (InterruptedException _) { - } - } - } + + TimerTask lastTask = heap[elements]; + remove(); - TimerTask lastTask = heap[elements]; - remove(); + int parent = 1; + int child = 2; + heap[1] = lastTask; + while (child <= elements) { + if (child < elements) { + if (heap[child].scheduled > heap[child + 1].scheduled) { + child++; + } + } - int parent = 1; - int child = 2; - heap[1] = lastTask; - while (child <= elements) { - if (child < elements) { - if (heap[child].scheduled > heap[child + 1].scheduled) { - child++; - } + if (lastTask.scheduled <= heap[child].scheduled) + break; + + heap[parent] = heap[child]; + parent = child; + child = parent * 2; } - if (lastTask.scheduled <= heap[child].scheduled) - break; - - heap[parent] = heap[child]; - parent = child; - child = parent * 2; + heap[parent] = lastTask; + return task; } - - heap[parent] = lastTask; - return task; + } else { // Phobos + implMissing(__FILE__,__LINE__); + return null; } } public void setNullOnEmpty(bool nullOnEmpty) { - synchronized( mutex ){ - this.nullOnEmpty = nullOnEmpty; - cond.notify(); + version(Tango){ + synchronized( mutex ){ + this.nullOnEmpty = nullOnEmpty; + cond.notify(); + } + } else { // Phobos + implMissing(__FILE__,__LINE__); } } public void stop() { - synchronized( mutex ){ - this.heap = null; - this.elements = 0; - cond.notify(); + version(Tango){ + synchronized( mutex ){ + this.heap = null; + this.elements = 0; + cond.notify(); + } + } else { // Phobos + implMissing(__FILE__,__LINE__); } } @@ -176,11 +202,11 @@ try { task.run(); } -// catch (ThreadDeath death) { -// // If an exception escapes, the Timer becomes invalid. -// queue.stop(); -// throw death; -// } + // catch (ThreadDeath death) { + // // If an exception escapes, the Timer becomes invalid. + // queue.stop(); + // throw death; + // } catch (Exception t) { queue.stop(); } @@ -270,16 +296,16 @@ } } -// public void schedule(TimerTask task, Date date) { -// long time = date.getTime(); -// schedule(task, time, -1, false); -// } + // public void schedule(TimerTask task, Date date) { + // long time = date.getTime(); + // schedule(task, time, -1, false); + // } -// public void schedule(TimerTask task, Date date, long period) { -// positivePeriod(period); -// long time = date.getTime(); -// schedule(task, time, period, false); -// } + // public void schedule(TimerTask task, Date date, long period) { + // positivePeriod(period); + // long time = date.getTime(); + // schedule(task, time, period, false); + // } public void schedule(TimerTask task, long delay) { positiveDelay(delay); @@ -294,11 +320,11 @@ schedule(task, time, period, false); } -// public void scheduleAtFixedRate(TimerTask task, Date date, long period) { -// positivePeriod(period); -// long time = date.getTime(); -// schedule(task, time, period, true); -// } + // public void scheduleAtFixedRate(TimerTask task, Date date, long period) { + // positivePeriod(period); + // long time = date.getTime(); + // schedule(task, time, period, true); + // } public void scheduleAtFixedRate(TimerTask task, long delay, long period) { positiveDelay(delay);