From 357f983875a8f89351cec2b69312ef2f1f91543e Mon Sep 17 00:00:00 2001 From: voidstarstar Date: Tue, 14 Jun 2016 13:09:15 -0400 Subject: [PATCH 1/4] Allow fibers to have a wider range of priorities --- .../co/paralleluniverse/fibers/Fiber.java | 19 +++++++- .../co/paralleluniverse/strands/Strand.java | 45 ++++++++++++------- .../co/paralleluniverse/fibers/FiberTest.java | 6 +-- 3 files changed, 50 insertions(+), 20 deletions(-) diff --git a/quasar-core/src/main/java/co/paralleluniverse/fibers/Fiber.java b/quasar-core/src/main/java/co/paralleluniverse/fibers/Fiber.java index 42f646e282..55630341db 100644 --- a/quasar-core/src/main/java/co/paralleluniverse/fibers/Fiber.java +++ b/quasar-core/src/main/java/co/paralleluniverse/fibers/Fiber.java @@ -140,7 +140,7 @@ private static long nextFiberId() { // private int preemptionCredits; private transient Thread runningThread; private final SuspendableCallable target; - private byte priority; + private int priority; private transient ClassLoader contextClassLoader; private transient AccessControlContext inheritedAccessControlContext; // These are typed as Object because they store JRE-internal ThreadLocalMap objects, which is a package private @@ -230,6 +230,21 @@ public Fiber(String name, int stackSize, SuspendableCallable target) { this(name, defaultScheduler(), stackSize, target); } + /** + * The minimum priority that a fiber can have. + */ + public final static int MIN_PRIORITY = Integer.MIN_VALUE; + + /** + * The default priority that is assigned to a fiber. + */ + public final static int NORM_PRIORITY = 0; + + /** + * The maximum priority that a fiber can have. + */ + public final static int MAX_PRIORITY = Integer.MAX_VALUE; + private static FiberScheduler defaultScheduler() { final Fiber parent = currentFiber(); if (parent == null) @@ -310,7 +325,7 @@ public final Fiber setName(String name) { public Fiber setPriority(int newPriority) { if (newPriority > MAX_PRIORITY || newPriority < MIN_PRIORITY) throw new IllegalArgumentException(); - this.priority = (byte) newPriority; + this.priority = newPriority; return this; } diff --git a/quasar-core/src/main/java/co/paralleluniverse/strands/Strand.java b/quasar-core/src/main/java/co/paralleluniverse/strands/Strand.java index 491118a089..d65b2e30c0 100644 --- a/quasar-core/src/main/java/co/paralleluniverse/strands/Strand.java +++ b/quasar-core/src/main/java/co/paralleluniverse/strands/Strand.java @@ -55,21 +55,6 @@ public static Strand of(Fiber fiber) { return fiber; } - /** - * The minimum priority that a strand can have. - */ - public final static int MIN_PRIORITY = 1; - - /** - * The default priority that is assigned to a strand. - */ - public final static int NORM_PRIORITY = 5; - - /** - * The maximum priority that a strand can have. - */ - public final static int MAX_PRIORITY = 10; - /** * A strand's running state */ @@ -351,6 +336,36 @@ public static boolean interrupted() { return Thread.interrupted(); } + /** + * The minimum priority that the current strand can have. + */ + public static int MIN_PRIORITY() { + if (isCurrentFiber()) + return Fiber.MIN_PRIORITY; + else + return Thread.MIN_PRIORITY; + } + + /** + * The default priority that is assigned to the current strand. + */ + public static int NORM_PRIORITY() { + if (isCurrentFiber()) + return Fiber.NORM_PRIORITY; + else + return Thread.NORM_PRIORITY; + } + + /** + * The maximum priority that the current strand can have. + */ + public static int MAX_PRIORITY() { + if (isCurrentFiber()) + return Fiber.MAX_PRIORITY; + else + return Thread.MAX_PRIORITY; + } + /** * Awaits the termination of a given strand. * This method blocks until this strand terminates. diff --git a/quasar-core/src/test/java/co/paralleluniverse/fibers/FiberTest.java b/quasar-core/src/test/java/co/paralleluniverse/fibers/FiberTest.java index 161c4f58f6..b45c584727 100644 --- a/quasar-core/src/test/java/co/paralleluniverse/fibers/FiberTest.java +++ b/quasar-core/src/test/java/co/paralleluniverse/fibers/FiberTest.java @@ -114,19 +114,19 @@ public void run() throws SuspendExecution { } }); - assertThat(fiber.getPriority(), is(Strand.NORM_PRIORITY)); + assertThat(fiber.getPriority(), is(Fiber.NORM_PRIORITY)); fiber.setPriority(3); assertThat(fiber.getPriority(), is(3)); try { - fiber.setPriority(Strand.MAX_PRIORITY + 1); + fiber.setPriority(Fiber.MAX_PRIORITY + 1); fail(); } catch (IllegalArgumentException e) { } try { - fiber.setPriority(Strand.MIN_PRIORITY - 1); + fiber.setPriority(Fiber.MIN_PRIORITY - 1); fail(); } catch (IllegalArgumentException e) { } From a602880c53c427df19556db3c5cfb2c4819d133b Mon Sep 17 00:00:00 2001 From: voidstarstar Date: Tue, 14 Jun 2016 14:43:23 -0400 Subject: [PATCH 2/4] Fix priority test for Integer overflow/underflow --- .../src/test/java/co/paralleluniverse/fibers/FiberTest.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/quasar-core/src/test/java/co/paralleluniverse/fibers/FiberTest.java b/quasar-core/src/test/java/co/paralleluniverse/fibers/FiberTest.java index b45c584727..4edb4b007f 100644 --- a/quasar-core/src/test/java/co/paralleluniverse/fibers/FiberTest.java +++ b/quasar-core/src/test/java/co/paralleluniverse/fibers/FiberTest.java @@ -121,13 +121,15 @@ public void run() throws SuspendExecution { try { fiber.setPriority(Fiber.MAX_PRIORITY + 1); - fail(); + if (Fiber.MAX_PRIORITY + 1 != Integer.MIN_VALUE) // Ignore overflow + fail(); } catch (IllegalArgumentException e) { } try { fiber.setPriority(Fiber.MIN_PRIORITY - 1); - fail(); + if (Fiber.MIN_PRIORITY - 1 != Integer.MAX_VALUE) // Ignore underflow + fail(); } catch (IllegalArgumentException e) { } } From 0c4c914757cef2e1a9e8f0175a6b92735880c58e Mon Sep 17 00:00:00 2001 From: voidstarstar Date: Wed, 15 Jun 2016 07:33:59 -0400 Subject: [PATCH 3/4] Fix bug where priority was cast to a byte --- quasar-core/src/main/java/co/paralleluniverse/fibers/Fiber.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/quasar-core/src/main/java/co/paralleluniverse/fibers/Fiber.java b/quasar-core/src/main/java/co/paralleluniverse/fibers/Fiber.java index 55630341db..bd553de469 100644 --- a/quasar-core/src/main/java/co/paralleluniverse/fibers/Fiber.java +++ b/quasar-core/src/main/java/co/paralleluniverse/fibers/Fiber.java @@ -176,7 +176,7 @@ public Fiber(String name, FiberScheduler scheduler, int stackSize, SuspendableCa this.task = scheduler != null ? scheduler.newFiberTask(this) : new FiberForkJoinTask(this); this.initialStackSize = stackSize; this.stack = new Stack(this, stackSize > 0 ? stackSize : DEFAULT_STACK_SIZE); - this.priority = (byte)NORM_PRIORITY; + this.priority = NORM_PRIORITY; if (Debug.isDebug()) record(1, "Fiber", "", "Creating fiber name: %s, scheduler: %s, parent: %s, target: %s, task: %s, stackSize: %s", name, scheduler, parent, target, task, stackSize); From 9245fdca96f30ae67940d4345bc203687e13a3b1 Mon Sep 17 00:00:00 2001 From: voidstarstar Date: Wed, 15 Jun 2016 08:43:26 -0400 Subject: [PATCH 4/4] Remove the static priority methods from Strand --- .../co/paralleluniverse/strands/Strand.java | 30 ------------------- 1 file changed, 30 deletions(-) diff --git a/quasar-core/src/main/java/co/paralleluniverse/strands/Strand.java b/quasar-core/src/main/java/co/paralleluniverse/strands/Strand.java index d65b2e30c0..967450e698 100644 --- a/quasar-core/src/main/java/co/paralleluniverse/strands/Strand.java +++ b/quasar-core/src/main/java/co/paralleluniverse/strands/Strand.java @@ -336,36 +336,6 @@ public static boolean interrupted() { return Thread.interrupted(); } - /** - * The minimum priority that the current strand can have. - */ - public static int MIN_PRIORITY() { - if (isCurrentFiber()) - return Fiber.MIN_PRIORITY; - else - return Thread.MIN_PRIORITY; - } - - /** - * The default priority that is assigned to the current strand. - */ - public static int NORM_PRIORITY() { - if (isCurrentFiber()) - return Fiber.NORM_PRIORITY; - else - return Thread.NORM_PRIORITY; - } - - /** - * The maximum priority that the current strand can have. - */ - public static int MAX_PRIORITY() { - if (isCurrentFiber()) - return Fiber.MAX_PRIORITY; - else - return Thread.MAX_PRIORITY; - } - /** * Awaits the termination of a given strand. * This method blocks until this strand terminates.