diff --git a/classpath/java/util/concurrent/CancellationException.java b/classpath/java/util/concurrent/CancellationException.java new file mode 100644 index 0000000000..936031275a --- /dev/null +++ b/classpath/java/util/concurrent/CancellationException.java @@ -0,0 +1,23 @@ +/* Copyright (c) 2008-2013, Avian Contributors + + Permission to use, copy, modify, and/or distribute this software + for any purpose with or without fee is hereby granted, provided + that the above copyright notice and this permission notice appear + in all copies. + + There is NO WARRANTY for this software. See license.txt for + details. */ + +package java.util.concurrent; + +public class CancellationException extends IllegalStateException { + private static final long serialVersionUID = -9202173006928992231L; + + public CancellationException() { + super(); + } + + public CancellationException(String message) { + super(message); + } +} diff --git a/classpath/java/util/concurrent/ConcurrentLinkedQueue.java b/classpath/java/util/concurrent/ConcurrentLinkedQueue.java index 251adadbcc..888fc80685 100644 --- a/classpath/java/util/concurrent/ConcurrentLinkedQueue.java +++ b/classpath/java/util/concurrent/ConcurrentLinkedQueue.java @@ -22,7 +22,7 @@ public class ConcurrentLinkedQueue { } } - private volatile Node head = new Node(null, null); + private volatile Node head = new Node(null, null); private volatile Node tail = head; public void clear() { @@ -31,7 +31,7 @@ public class ConcurrentLinkedQueue { } public boolean add(T value) { - Node n = new Node(value, null); + Node n = new Node(value, null); while (true) { Node t = tail; Node next = tail.next; diff --git a/classpath/java/util/concurrent/Delayed.java b/classpath/java/util/concurrent/Delayed.java new file mode 100644 index 0000000000..256f6669fd --- /dev/null +++ b/classpath/java/util/concurrent/Delayed.java @@ -0,0 +1,15 @@ +/* Copyright (c) 2008-2013, Avian Contributors + + Permission to use, copy, modify, and/or distribute this software + for any purpose with or without fee is hereby granted, provided + that the above copyright notice and this permission notice appear + in all copies. + + There is NO WARRANTY for this software. See license.txt for + details. */ + +package java.util.concurrent; + +public interface Delayed { + public long getDelay(TimeUnit unit); +} diff --git a/classpath/java/util/concurrent/ExecutionException.java b/classpath/java/util/concurrent/ExecutionException.java new file mode 100644 index 0000000000..ac19c43581 --- /dev/null +++ b/classpath/java/util/concurrent/ExecutionException.java @@ -0,0 +1,31 @@ +/* Copyright (c) 2008-2013, Avian Contributors + + Permission to use, copy, modify, and/or distribute this software + for any purpose with or without fee is hereby granted, provided + that the above copyright notice and this permission notice appear + in all copies. + + There is NO WARRANTY for this software. See license.txt for + details. */ + +package java.util.concurrent; + +public class ExecutionException extends Exception { + private static final long serialVersionUID = 7830266012832686185L; + + protected ExecutionException() { + super(); + } + + protected ExecutionException(String message) { + super(message); + } + + public ExecutionException(String message, Throwable cause) { + super(message, cause); + } + + public ExecutionException(Throwable cause) { + super(cause); + } +} diff --git a/classpath/java/util/concurrent/Executor.java b/classpath/java/util/concurrent/Executor.java new file mode 100644 index 0000000000..26192086a2 --- /dev/null +++ b/classpath/java/util/concurrent/Executor.java @@ -0,0 +1,15 @@ +/* Copyright (c) 2008-2013, Avian Contributors + + Permission to use, copy, modify, and/or distribute this software + for any purpose with or without fee is hereby granted, provided + that the above copyright notice and this permission notice appear + in all copies. + + There is NO WARRANTY for this software. See license.txt for + details. */ + +package java.util.concurrent; + +public interface Executor { + public void execute(Runnable task); +} diff --git a/classpath/java/util/concurrent/ExecutorService.java b/classpath/java/util/concurrent/ExecutorService.java new file mode 100644 index 0000000000..53238db01b --- /dev/null +++ b/classpath/java/util/concurrent/ExecutorService.java @@ -0,0 +1,39 @@ +/* Copyright (c) 2008-2013, Avian Contributors + + Permission to use, copy, modify, and/or distribute this software + for any purpose with or without fee is hereby granted, provided + that the above copyright notice and this permission notice appear + in all copies. + + There is NO WARRANTY for this software. See license.txt for + details. */ + +package java.util.concurrent; +import java.util.List; +import java.util.Collection; + +public interface ExecutorService extends Executor { + public void shutdown(); + + public boolean isShutdown(); + + public boolean isTerminated(); + + public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException; + + public Future submit(Callable task); + + public Future submit(Runnable task, T result); + + public Future submit(Runnable task); + + public List> invokeAll(Collection> tasks) throws InterruptedException; + + public List> invokeAll(Collection> tasks, + long timeout, TimeUnit unit) throws InterruptedException; + + public T invokeAny(Collection> tasks) throws InterruptedException, ExecutionException; + + public T invokeAny(Collection> tasks, + long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException; +} diff --git a/classpath/java/util/concurrent/Future.java b/classpath/java/util/concurrent/Future.java new file mode 100644 index 0000000000..04a44db790 --- /dev/null +++ b/classpath/java/util/concurrent/Future.java @@ -0,0 +1,25 @@ +/* Copyright (c) 2008-2013, Avian Contributors + + Permission to use, copy, modify, and/or distribute this software + for any purpose with or without fee is hereby granted, provided + that the above copyright notice and this permission notice appear + in all copies. + + There is NO WARRANTY for this software. See license.txt for + details. */ + +package java.util.concurrent; + +public interface Future { + public boolean cancel(boolean mayInterruptIfRunning); + + public boolean isCancelled(); + + public boolean isDone(); + + public V get() throws InterruptedException, ExecutionException; + + public V get(long timeout, TimeUnit unit) throws InterruptedException, + ExecutionException, + TimeoutException; +} diff --git a/classpath/java/util/concurrent/RejectedExecutionException.java b/classpath/java/util/concurrent/RejectedExecutionException.java new file mode 100644 index 0000000000..dfb9f12133 --- /dev/null +++ b/classpath/java/util/concurrent/RejectedExecutionException.java @@ -0,0 +1,31 @@ +/* Copyright (c) 2008-2013, Avian Contributors + + Permission to use, copy, modify, and/or distribute this software + for any purpose with or without fee is hereby granted, provided + that the above copyright notice and this permission notice appear + in all copies. + + There is NO WARRANTY for this software. See license.txt for + details. */ + +package java.util.concurrent; + +public class RejectedExecutionException extends RuntimeException { + private static final long serialVersionUID = -375805702767069545L; + + public RejectedExecutionException() { + super(); + } + + public RejectedExecutionException(String message) { + super(message); + } + + public RejectedExecutionException(String message, Throwable cause) { + super(message, cause); + } + + public RejectedExecutionException(Throwable cause) { + super(cause); + } +} diff --git a/classpath/java/util/concurrent/ScheduledFuture.java b/classpath/java/util/concurrent/ScheduledFuture.java new file mode 100644 index 0000000000..225bfd830f --- /dev/null +++ b/classpath/java/util/concurrent/ScheduledFuture.java @@ -0,0 +1,15 @@ +/* Copyright (c) 2008-2013, Avian Contributors + + Permission to use, copy, modify, and/or distribute this software + for any purpose with or without fee is hereby granted, provided + that the above copyright notice and this permission notice appear + in all copies. + + There is NO WARRANTY for this software. See license.txt for + details. */ + +package java.util.concurrent; + +public interface ScheduledFuture extends Delayed, Future { + +} diff --git a/classpath/java/util/concurrent/TimeUnit.java b/classpath/java/util/concurrent/TimeUnit.java new file mode 100644 index 0000000000..fa378a4885 --- /dev/null +++ b/classpath/java/util/concurrent/TimeUnit.java @@ -0,0 +1,416 @@ +/* Copyright (c) 2008-2013, Avian Contributors + + Permission to use, copy, modify, and/or distribute this software + for any purpose with or without fee is hereby granted, provided + that the above copyright notice and this permission notice appear + in all copies. + + There is NO WARRANTY for this software. See license.txt for + details. */ + +package java.util.concurrent; + +public enum TimeUnit { + NANOSECONDS { + @Override + public long toNanos(long d) { + return d; + } + + @Override + public long toMicros(long d) { + return d / NANOSECONDS_PER_MICROSECOND; + } + + @Override + public long toMillis(long d) { + return d / NANOSECONDS_PER_MILLISECOND; + } + + @Override + public long toSeconds(long d) { + return d / NANOSECONDS_PER_SECOND; + } + + @Override + public long toMinutes(long d) { + return d / NANOSECONDS_PER_MINUET; + } + + @Override + public long toHours(long d) { + return d / NANOSECONDS_PER_HOUR; + } + + @Override + public long toDays(long d) { + return d / NANOSECONDS_PER_DAY; + } + + @Override + public long convert(long d, TimeUnit u) { + return u.toNanos(d); + } + + @Override + int excessNanos(long d, long m) { + return (int) (d - (m * NANOSECONDS_PER_MILLISECOND)); + } + }, + MICROSECONDS { + @Override + public long toNanos(long d) { + return scale(d, NANOSECONDS_PER_MICROSECOND); + } + + @Override + public long toMicros(long d) { + return d; + } + + @Override + public long toMillis(long d) { + return d / MICROSECONDS_PER_MILLISECOND; + } + + @Override + public long toSeconds(long d) { + return d / MICROSECONDS_PER_SECOND; + } + + @Override + public long toMinutes(long d) { + return d / MICROSECONDS_PER_MINUET; + } + + @Override + public long toHours(long d) { + return d / MICROSECONDS_PER_HOUR; + } + + @Override + public long toDays(long d) { + return d / MICROSECONDS_PER_DAY; + } + + @Override + public long convert(long d, TimeUnit u) { + return u.toMicros(d); + } + + @Override + int excessNanos(long d, long m) { + return (int) ((d * NANOSECONDS_PER_MICROSECOND) - (m * NANOSECONDS_PER_MILLISECOND)); + } + }, + MILLISECONDS { + @Override + public long toNanos(long d) { + return scale(d, NANOSECONDS_PER_MILLISECOND); + } + + @Override + public long toMicros(long d) { + return scale(d, MICROSECONDS_PER_MILLISECOND); + } + + @Override + public long toMillis(long d) { + return d; + } + + @Override + public long toSeconds(long d) { + return d / MILLISECONDS_PER_SECOND; + } + + @Override + public long toMinutes(long d) { + return d / MILLISECONDS_PER_MINUET; + } + + @Override + public long toHours(long d) { + return d / MILLISECONDS_PER_HOUR; + } + + @Override + public long toDays(long d) { + return d / MILLISECONDS_PER_DAY; + } + + @Override + public long convert(long d, TimeUnit u) { + return u.toMillis(d); + } + + @Override + int excessNanos(long d, long m) { + return 0; + } + }, + SECONDS { + @Override + public long toNanos(long d) { + return scale(d, NANOSECONDS_PER_SECOND); + } + + @Override + public long toMicros(long d) { + return scale(d, MICROSECONDS_PER_SECOND); + } + + @Override + public long toMillis(long d) { + return scale(d, MILLISECONDS_PER_SECOND); + } + + @Override + public long toSeconds(long d) { + return d; + } + + @Override + public long toMinutes(long d) { + return d / SECONDS_PER_MINUET; + } + + @Override + public long toHours(long d) { + return d / SECONDS_PER_HOUR; + } + + @Override + public long toDays(long d) { + return d / SECONDS_PER_DAY; + } + + @Override + public long convert(long d, TimeUnit u) { + return u.toSeconds(d); + } + + @Override + int excessNanos(long d, long m) { + return 0; + } + }, + MINUTES { + @Override + public long toNanos(long d) { + return scale(d, NANOSECONDS_PER_MINUET); + } + + @Override + public long toMicros(long d) { + return scale(d, MICROSECONDS_PER_MINUET); + } + + @Override + public long toMillis(long d) { + return scale(d, MILLISECONDS_PER_MINUET); + } + + @Override + public long toSeconds(long d) { + return scale(d, SECONDS_PER_MINUET); + } + + @Override + public long toMinutes(long d) { + return d; + } + + @Override + public long toHours(long d) { + return d / MINUETS_PER_HOUR; + } + + @Override + public long toDays(long d) { + return d / MINUETS_PER_DAY; + } + + @Override + public long convert(long d, TimeUnit u) { + return u.toMinutes(d); + } + + @Override + int excessNanos(long d, long m) { + return 0; + } + }, + HOURS { + @Override + public long toNanos(long d) { + return scale(d, NANOSECONDS_PER_HOUR); + } + + @Override + public long toMicros(long d) { + return scale(d, MICROSECONDS_PER_HOUR); + } + + @Override + public long toMillis(long d) { + return scale(d, MILLISECONDS_PER_HOUR); + } + + @Override + public long toSeconds(long d) { + return scale(d, SECONDS_PER_HOUR); + } + + @Override + public long toMinutes(long d) { + return scale(d, MINUETS_PER_HOUR); + } + + @Override + public long toHours(long d) { + return d; + } + + @Override + public long toDays(long d) { + return d / HOURS_PER_DAY; + } + + @Override + public long convert(long d, TimeUnit u) { + return u.toHours(d); + } + + @Override + int excessNanos(long d, long m) { + return 0; + } + }, + DAYS { + @Override + public long toNanos(long d) { + return scale(d, NANOSECONDS_PER_DAY); + } + + @Override + public long toMicros(long d) { + return scale(d, MICROSECONDS_PER_DAY); + } + + @Override + public long toMillis(long d) { + return scale(d, MILLISECONDS_PER_DAY); + } + + @Override + public long toSeconds(long d) { + return scale(d, SECONDS_PER_DAY); + } + + @Override + public long toMinutes(long d) { + return scale(d, MINUETS_PER_DAY); + } + + @Override + public long toHours(long d) { + return scale(d, HOURS_PER_DAY); + } + + @Override + public long toDays(long d) { + return d; + } + + @Override + public long convert(long d, TimeUnit u) { + return u.toDays(d); + } + + @Override + int excessNanos(long d, long m) { + return 0; + } + }; + + private static final long NANOSECONDS_PER_MICROSECOND = 1000L; + private static final long MICROSECONDS_PER_MILLISECOND = 1000L; + private static final long MILLISECONDS_PER_SECOND = 1000L; + private static final long SECONDS_PER_MINUET = 60; + private static final long MINUETS_PER_HOUR = 60; + private static final long HOURS_PER_DAY = 24; + + private static final long NANOSECONDS_PER_MILLISECOND = NANOSECONDS_PER_MICROSECOND * MICROSECONDS_PER_MILLISECOND; + private static final long NANOSECONDS_PER_SECOND = NANOSECONDS_PER_MILLISECOND * MILLISECONDS_PER_SECOND; + private static final long NANOSECONDS_PER_MINUET = NANOSECONDS_PER_SECOND * SECONDS_PER_MINUET; + private static final long NANOSECONDS_PER_HOUR = NANOSECONDS_PER_MINUET * MINUETS_PER_HOUR; + private static final long NANOSECONDS_PER_DAY = NANOSECONDS_PER_HOUR * HOURS_PER_DAY; + + private static final long MICROSECONDS_PER_SECOND = MICROSECONDS_PER_MILLISECOND * MILLISECONDS_PER_SECOND; + private static final long MICROSECONDS_PER_MINUET = MICROSECONDS_PER_SECOND * SECONDS_PER_MINUET; + private static final long MICROSECONDS_PER_HOUR = MICROSECONDS_PER_MINUET * MINUETS_PER_HOUR; + private static final long MICROSECONDS_PER_DAY = MICROSECONDS_PER_HOUR * HOURS_PER_DAY; + + private static final long MILLISECONDS_PER_MINUET = MILLISECONDS_PER_SECOND * SECONDS_PER_MINUET; + private static final long MILLISECONDS_PER_HOUR = MILLISECONDS_PER_MINUET * MINUETS_PER_HOUR; + private static final long MILLISECONDS_PER_DAY = MILLISECONDS_PER_HOUR * HOURS_PER_DAY; + + private static final long SECONDS_PER_HOUR = SECONDS_PER_MINUET * MINUETS_PER_HOUR; + private static final long SECONDS_PER_DAY = SECONDS_PER_HOUR * HOURS_PER_DAY; + + private static final long MINUETS_PER_DAY = MINUETS_PER_HOUR * HOURS_PER_DAY; + + private static long scale(long value, long conversion) { + long result = value * conversion; + if (value > 0 && result < value) { + return Long.MAX_VALUE; + } else if (value < 0 && result > value) { + return Long.MIN_VALUE; + } else { + return result; + } + } + + public abstract long convert(long sourceDuration, TimeUnit sourceUnit); + + public abstract long toNanos(long duration); + + public abstract long toMicros(long duration); + + public abstract long toMillis(long duration); + + public abstract long toSeconds(long duration); + + public abstract long toMinutes(long duration); + + public abstract long toHours(long duration); + + public abstract long toDays(long duration); + + abstract int excessNanos(long d, long m); + + public void timedWait(Object obj, long timeout) throws InterruptedException { + if (timeout > 0) { + long ms = toMillis(timeout); + int ns = excessNanos(timeout, ms); + obj.wait(ms, ns); + } + } + + public void timedJoin(Thread thread, long timeout) throws InterruptedException { + if (timeout > 0) { + long ms = toMillis(timeout); + int ns = excessNanos(timeout, ms); + thread.join(ms, ns); + } + } + + public void sleep(long timeout) throws InterruptedException { + if (timeout > 0) { + long ms = toMillis(timeout); + int ns = excessNanos(timeout, ms); + Thread.sleep(ms, ns); + } + } +} diff --git a/classpath/java/util/concurrent/TimeoutException.java b/classpath/java/util/concurrent/TimeoutException.java new file mode 100644 index 0000000000..929ede7927 --- /dev/null +++ b/classpath/java/util/concurrent/TimeoutException.java @@ -0,0 +1,23 @@ +/* Copyright (c) 2008-2013, Avian Contributors + + Permission to use, copy, modify, and/or distribute this software + for any purpose with or without fee is hereby granted, provided + that the above copyright notice and this permission notice appear + in all copies. + + There is NO WARRANTY for this software. See license.txt for + details. */ + +package java.util.concurrent; + +public class TimeoutException extends Exception { + private static final long serialVersionUID = 1900926677490660714L; + + public TimeoutException() { + super(); + } + + public TimeoutException(String message) { + super(message); + } +}