1 |
/* |
2 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
* Expert Group and released to the public domain, as explained at |
4 |
* http://creativecommons.org/licenses/publicdomain |
5 |
*/ |
6 |
|
7 |
package jsr166y.forkjoin; |
8 |
import java.util.concurrent.atomic.*; |
9 |
|
10 |
/** |
11 |
* Maintains lifecycle control for pool and workers. |
12 |
* Opportunistically subclasses AtomicInteger. Don't directly use the |
13 |
* AtomicInteger methods though. |
14 |
*/ |
15 |
final class RunState extends AtomicInteger { |
16 |
// Order among values matters |
17 |
static final int RUNNING = 0; |
18 |
static final int SHUTDOWN = 1; |
19 |
static final int STOPPING = 2; |
20 |
static final int TERMINATED = 4; |
21 |
|
22 |
boolean isRunning() { return get() == RUNNING; } |
23 |
boolean isShutdown() { return get() == SHUTDOWN; } |
24 |
boolean isStopping() { return get() == STOPPING; } |
25 |
boolean isTerminated() { return get() == TERMINATED; } |
26 |
boolean isAtLeastShutdown() { return get() >= SHUTDOWN; } |
27 |
boolean isAtLeastStopping() { return get() >= STOPPING; } |
28 |
boolean transitionToShutdown() { return transitionTo(SHUTDOWN); } |
29 |
boolean transitionToStopping() { return transitionTo(STOPPING); } |
30 |
boolean transitionToTerminated() { return transitionTo(TERMINATED); } |
31 |
|
32 |
/** |
33 |
* Transition to at least the given state. Return true if not |
34 |
* already at least given state. |
35 |
*/ |
36 |
private boolean transitionTo(int state) { |
37 |
for (;;) { |
38 |
int s = get(); |
39 |
if (s >= state) |
40 |
return false; |
41 |
if (compareAndSet(s, state)) |
42 |
return true; |
43 |
} |
44 |
} |
45 |
} |