|
25 | 25 | *
|
26 | 26 | * @see <a href="http://msdn.microsoft.com/en-us/library/system.reactive.disposables.multipleassignmentdisposable">Rx.Net equivalent MultipleAssignmentDisposable</a>
|
27 | 27 | */
|
28 |
| -public class MultipleAssignmentSubscription implements Subscription { |
29 |
| - private AtomicReference<Subscription> reference = new AtomicReference<Subscription>(); |
30 |
| - /** Sentinel for the unsubscribed state. */ |
31 |
| - private static final Subscription UNSUBSCRIBED_SENTINEL = new Subscription() { |
32 |
| - @Override |
33 |
| - public void unsubscribe() { |
| 28 | +public final class MultipleAssignmentSubscription implements Subscription { |
| 29 | + |
| 30 | + private final AtomicReference<State> state = new AtomicReference<State>(new State(false, Subscriptions.empty())); |
| 31 | + |
| 32 | + private static final class State { |
| 33 | + final boolean isUnsubscribed; |
| 34 | + final Subscription subscription; |
| 35 | + |
| 36 | + State(boolean u, Subscription s) { |
| 37 | + this.isUnsubscribed = u; |
| 38 | + this.subscription = s; |
| 39 | + } |
| 40 | + |
| 41 | + State unsubscribe() { |
| 42 | + return new State(true, subscription); |
34 | 43 | }
|
35 |
| - }; |
| 44 | + |
| 45 | + State set(Subscription s) { |
| 46 | + return new State(isUnsubscribed, s); |
| 47 | + } |
| 48 | + |
| 49 | + } |
36 | 50 |
|
37 | 51 | public boolean isUnsubscribed() {
|
38 |
| - return reference.get() == UNSUBSCRIBED_SENTINEL; |
| 52 | + return state.get().isUnsubscribed; |
39 | 53 | }
|
40 | 54 |
|
41 | 55 | @Override
|
42 | 56 | public void unsubscribe() {
|
43 |
| - Subscription s = reference.getAndSet(UNSUBSCRIBED_SENTINEL); |
44 |
| - if (s != null) { |
45 |
| - s.unsubscribe(); |
46 |
| - } |
| 57 | + State oldState; |
| 58 | + State newState; |
| 59 | + do { |
| 60 | + oldState = state.get(); |
| 61 | + if (oldState.isUnsubscribed) { |
| 62 | + return; |
| 63 | + } else { |
| 64 | + newState = oldState.unsubscribe(); |
| 65 | + } |
| 66 | + } while (!state.compareAndSet(oldState, newState)); |
| 67 | + oldState.subscription.unsubscribe(); |
47 | 68 | }
|
48 | 69 |
|
| 70 | + @Deprecated |
49 | 71 | public void setSubscription(Subscription s) {
|
| 72 | + set(s); |
| 73 | + } |
| 74 | + |
| 75 | + public void set(Subscription s) { |
| 76 | + if (s == null) { |
| 77 | + throw new IllegalArgumentException("Subscription can not be null"); |
| 78 | + } |
| 79 | + State oldState; |
| 80 | + State newState; |
50 | 81 | do {
|
51 |
| - Subscription r = reference.get(); |
52 |
| - if (r == UNSUBSCRIBED_SENTINEL) { |
| 82 | + oldState = state.get(); |
| 83 | + if (oldState.isUnsubscribed) { |
53 | 84 | s.unsubscribe();
|
54 | 85 | return;
|
| 86 | + } else { |
| 87 | + newState = oldState.set(s); |
55 | 88 | }
|
56 |
| - if (reference.compareAndSet(r, s)) { |
57 |
| - break; |
58 |
| - } |
59 |
| - } while (true); |
| 89 | + } while (!state.compareAndSet(oldState, newState)); |
60 | 90 | }
|
61 | 91 |
|
| 92 | + @Deprecated |
62 | 93 | public Subscription getSubscription() {
|
63 |
| - Subscription s = reference.get(); |
64 |
| - return s != UNSUBSCRIBED_SENTINEL ? s : Subscriptions.empty(); |
| 94 | + return get(); |
| 95 | + } |
| 96 | + |
| 97 | + public Subscription get() { |
| 98 | + return state.get().subscription; |
65 | 99 | }
|
66 | 100 |
|
67 | 101 | }
|
0 commit comments