|
16 | 16 | package rx.subscriptions; |
17 | 17 |
|
18 | 18 | import java.util.ArrayList; |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.Collection; |
| 21 | +import java.util.HashSet; |
19 | 22 | import java.util.List; |
20 | | -import java.util.concurrent.atomic.AtomicReference; |
| 23 | +import java.util.Set; |
21 | 24 |
|
22 | 25 | import rx.Subscription; |
23 | 26 | import rx.exceptions.CompositeException; |
|
30 | 33 | */ |
31 | 34 | public final class CompositeSubscription implements Subscription { |
32 | 35 |
|
33 | | - private final AtomicReference<State> state = new AtomicReference<State>(); |
34 | | - |
35 | | - /** Empty initial state. */ |
36 | | - private static final State CLEAR_STATE; |
37 | | - /** Unsubscribed empty state. */ |
38 | | - private static final State CLEAR_STATE_UNSUBSCRIBED; |
39 | | - static { |
40 | | - Subscription[] s0 = new Subscription[0]; |
41 | | - CLEAR_STATE = new State(false, s0); |
42 | | - CLEAR_STATE_UNSUBSCRIBED = new State(true, s0); |
43 | | - } |
44 | | - |
45 | | - private static final class State { |
46 | | - final boolean isUnsubscribed; |
47 | | - final Subscription[] subscriptions; |
48 | | - |
49 | | - State(boolean u, Subscription[] s) { |
50 | | - this.isUnsubscribed = u; |
51 | | - this.subscriptions = s; |
52 | | - } |
53 | | - |
54 | | - State unsubscribe() { |
55 | | - return CLEAR_STATE_UNSUBSCRIBED; |
56 | | - } |
57 | | - |
58 | | - State add(Subscription s) { |
59 | | - int idx = subscriptions.length; |
60 | | - Subscription[] newSubscriptions = new Subscription[idx + 1]; |
61 | | - System.arraycopy(subscriptions, 0, newSubscriptions, 0, idx); |
62 | | - newSubscriptions[idx] = s; |
63 | | - return new State(isUnsubscribed, newSubscriptions); |
64 | | - } |
65 | | - |
66 | | - State remove(Subscription s) { |
67 | | - if ((subscriptions.length == 1 && subscriptions[0].equals(s)) || subscriptions.length == 0) { |
68 | | - return clear(); |
69 | | - } |
70 | | - Subscription[] newSubscriptions = new Subscription[subscriptions.length - 1]; |
71 | | - int idx = 0; |
72 | | - for (Subscription _s : subscriptions) { |
73 | | - if (!_s.equals(s)) { |
74 | | - // was not in this composite |
75 | | - if (idx == newSubscriptions.length) { |
76 | | - return this; |
77 | | - } |
78 | | - newSubscriptions[idx] = _s; |
79 | | - idx++; |
80 | | - } |
81 | | - } |
82 | | - if (idx == 0) { |
83 | | - return clear(); |
84 | | - } |
85 | | - // subscription appeared more than once |
86 | | - if (idx < newSubscriptions.length) { |
87 | | - Subscription[] newSub2 = new Subscription[idx]; |
88 | | - System.arraycopy(newSubscriptions, 0, newSub2, 0, idx); |
89 | | - return new State(isUnsubscribed, newSub2); |
90 | | - } |
91 | | - return new State(isUnsubscribed, newSubscriptions); |
92 | | - } |
93 | | - |
94 | | - State clear() { |
95 | | - return isUnsubscribed ? CLEAR_STATE_UNSUBSCRIBED : CLEAR_STATE; |
96 | | - } |
97 | | - } |
| 36 | + private Set<Subscription> subscriptions; |
| 37 | + private boolean unsubscribed = false; |
98 | 38 |
|
99 | 39 | public CompositeSubscription() { |
100 | | - state.set(CLEAR_STATE); |
101 | 40 | } |
102 | 41 |
|
103 | 42 | public CompositeSubscription(final Subscription... subscriptions) { |
104 | | - state.set(new State(false, subscriptions)); |
| 43 | + this.subscriptions = new HashSet<Subscription>(Arrays.asList(subscriptions)); |
105 | 44 | } |
106 | 45 |
|
107 | 46 | @Override |
108 | | - public boolean isUnsubscribed() { |
109 | | - return state.get().isUnsubscribed; |
| 47 | + public synchronized boolean isUnsubscribed() { |
| 48 | + return unsubscribed; |
110 | 49 | } |
111 | 50 |
|
112 | 51 | public void add(final Subscription s) { |
113 | | - State oldState; |
114 | | - State newState; |
115 | | - do { |
116 | | - oldState = state.get(); |
117 | | - if (oldState.isUnsubscribed) { |
118 | | - s.unsubscribe(); |
119 | | - return; |
| 52 | + Subscription unsubscribe = null; |
| 53 | + synchronized (this) { |
| 54 | + if (unsubscribed) { |
| 55 | + unsubscribe = s; |
120 | 56 | } else { |
121 | | - newState = oldState.add(s); |
| 57 | + if (subscriptions == null) { |
| 58 | + subscriptions = new HashSet<Subscription>(4); |
| 59 | + } |
| 60 | + subscriptions.add(s); |
122 | 61 | } |
123 | | - } while (!state.compareAndSet(oldState, newState)); |
| 62 | + } |
| 63 | + if (unsubscribe != null) { |
| 64 | + // call after leaving the synchronized block so we're not holding a lock while executing this |
| 65 | + unsubscribe.unsubscribe(); |
| 66 | + } |
124 | 67 | } |
125 | 68 |
|
126 | 69 | public void remove(final Subscription s) { |
127 | | - State oldState; |
128 | | - State newState; |
129 | | - do { |
130 | | - oldState = state.get(); |
131 | | - if (oldState.isUnsubscribed) { |
| 70 | + boolean unsubscribe = false; |
| 71 | + synchronized (this) { |
| 72 | + if (unsubscribed || subscriptions == null) { |
132 | 73 | return; |
133 | | - } else { |
134 | | - newState = oldState.remove(s); |
135 | 74 | } |
136 | | - } while (!state.compareAndSet(oldState, newState)); |
137 | | - // if we removed successfully we then need to call unsubscribe on it |
138 | | - s.unsubscribe(); |
| 75 | + unsubscribe = subscriptions.remove(s); |
| 76 | + } |
| 77 | + if (unsubscribe) { |
| 78 | + // if we removed successfully we then need to call unsubscribe on it (outside of the lock) |
| 79 | + s.unsubscribe(); |
| 80 | + } |
139 | 81 | } |
140 | 82 |
|
141 | 83 | public void clear() { |
142 | | - State oldState; |
143 | | - State newState; |
144 | | - do { |
145 | | - oldState = state.get(); |
146 | | - if (oldState.isUnsubscribed) { |
| 84 | + Collection<Subscription> unsubscribe = null; |
| 85 | + synchronized (this) { |
| 86 | + if (unsubscribed || subscriptions == null) { |
147 | 87 | return; |
148 | 88 | } else { |
149 | | - newState = oldState.clear(); |
| 89 | + unsubscribe = subscriptions; |
| 90 | + subscriptions = null; |
150 | 91 | } |
151 | | - } while (!state.compareAndSet(oldState, newState)); |
152 | | - // if we cleared successfully we then need to call unsubscribe on all previous |
153 | | - unsubscribeFromAll(oldState.subscriptions); |
| 92 | + } |
| 93 | + unsubscribeFromAll(unsubscribe); |
154 | 94 | } |
155 | 95 |
|
156 | 96 | @Override |
157 | 97 | public void unsubscribe() { |
158 | | - State oldState; |
159 | | - State newState; |
160 | | - do { |
161 | | - oldState = state.get(); |
162 | | - if (oldState.isUnsubscribed) { |
| 98 | + synchronized (this) { |
| 99 | + if (unsubscribed) { |
163 | 100 | return; |
164 | | - } else { |
165 | | - newState = oldState.unsubscribe(); |
166 | 101 | } |
167 | | - } while (!state.compareAndSet(oldState, newState)); |
168 | | - unsubscribeFromAll(oldState.subscriptions); |
| 102 | + unsubscribed = true; |
| 103 | + } |
| 104 | + // we will only get here once |
| 105 | + unsubscribeFromAll(subscriptions); |
169 | 106 | } |
170 | 107 |
|
171 | | - private static void unsubscribeFromAll(Subscription[] subscriptions) { |
172 | | - final List<Throwable> es = new ArrayList<Throwable>(); |
| 108 | + private static void unsubscribeFromAll(Collection<Subscription> subscriptions) { |
| 109 | + if (subscriptions == null) { |
| 110 | + return; |
| 111 | + } |
| 112 | + List<Throwable> es = null; |
173 | 113 | for (Subscription s : subscriptions) { |
174 | 114 | try { |
175 | 115 | s.unsubscribe(); |
176 | 116 | } catch (Throwable e) { |
| 117 | + if (es == null) { |
| 118 | + es = new ArrayList<Throwable>(); |
| 119 | + } |
177 | 120 | es.add(e); |
178 | 121 | } |
179 | 122 | } |
180 | | - if (!es.isEmpty()) { |
| 123 | + if (es != null) { |
181 | 124 | if (es.size() == 1) { |
182 | 125 | Throwable t = es.get(0); |
183 | 126 | if (t instanceof RuntimeException) { |
|
0 commit comments