|
15 | 15 | */
|
16 | 16 | package rx.subscriptions;
|
17 | 17 |
|
| 18 | +import static java.util.Arrays.asList; |
| 19 | +import static java.util.Collections.unmodifiableSet; |
| 20 | + |
18 | 21 | import java.util.ArrayList;
|
19 | 22 | import java.util.Collection;
|
20 |
| -import java.util.List; |
21 |
| -import java.util.concurrent.ConcurrentHashMap; |
22 |
| -import java.util.concurrent.atomic.AtomicBoolean; |
| 23 | +import java.util.HashSet; |
| 24 | +import java.util.Set; |
| 25 | +import java.util.concurrent.atomic.AtomicReference; |
23 | 26 |
|
24 | 27 | import rx.Subscription;
|
25 | 28 | import rx.util.CompositeException;
|
26 | 29 |
|
27 | 30 | /**
|
28 |
| - * Subscription that represents a group of Subscriptions that are unsubscribed together. |
| 31 | + * Subscription that represents a group of Subscriptions that are unsubscribed |
| 32 | + * together. |
29 | 33 | *
|
30 |
| - * @see <a href="http://msdn.microsoft.com/en-us/library/system.reactive.disposables.compositedisposable(v=vs.103).aspx">Rx.Net equivalent CompositeDisposable</a> |
| 34 | + * @see <a |
| 35 | + * href="http://msdn.microsoft.com/en-us/library/system.reactive.disposables.compositedisposable(v=vs.103).aspx">Rx.Net |
| 36 | + * equivalent CompositeDisposable</a> |
31 | 37 | */
|
32 | 38 | public class CompositeSubscription implements Subscription {
|
| 39 | + private static final Set<Subscription> MUTATE_STATE = unmodifiableSet(new HashSet<Subscription>()); |
| 40 | + private static final Set<Subscription> UNSUBSCRIBED_STATE = unmodifiableSet(new HashSet<Subscription>()); |
| 41 | + |
| 42 | + private final AtomicReference<Set<Subscription>> reference = new AtomicReference<Set<Subscription>>(); |
33 | 43 |
|
34 |
| - /* |
35 |
| - * The reason 'synchronized' is used on 'add' and 'unsubscribe' is because AtomicBoolean/ConcurrentLinkedQueue are both being modified so it needs to be done atomically. |
36 |
| - * |
37 |
| - * TODO evaluate whether use of synchronized is a performance issue here and if it's worth using an atomic state machine or other non-locking approach |
38 |
| - */ |
39 |
| - private AtomicBoolean unsubscribed = new AtomicBoolean(false); |
40 |
| - private final ConcurrentHashMap<Subscription, Boolean> subscriptions = new ConcurrentHashMap<Subscription, Boolean>(); |
41 |
| - |
42 |
| - public CompositeSubscription(List<Subscription> subscriptions) { |
43 |
| - for (Subscription s : subscriptions) { |
44 |
| - this.subscriptions.put(s, Boolean.TRUE); |
45 |
| - } |
| 44 | + public CompositeSubscription(final Subscription... subscriptions) { |
| 45 | + reference.set(new HashSet<Subscription>(asList(subscriptions))); |
46 | 46 | }
|
47 | 47 |
|
48 |
| - public CompositeSubscription(Subscription... subscriptions) { |
49 |
| - for (Subscription s : subscriptions) { |
50 |
| - this.subscriptions.put(s, Boolean.TRUE); |
51 |
| - } |
| 48 | + public boolean isUnsubscribed() { |
| 49 | + return reference.get() == UNSUBSCRIBED_STATE; |
52 | 50 | }
|
53 | 51 |
|
54 |
| - /** |
55 |
| - * Remove and unsubscribe all subscriptions but do not unsubscribe the outer CompositeSubscription. |
56 |
| - */ |
57 |
| - public void clear() { |
58 |
| - Collection<Throwable> es = null; |
59 |
| - for (Subscription s : subscriptions.keySet()) { |
60 |
| - try { |
| 52 | + public void add(final Subscription s) { |
| 53 | + do { |
| 54 | + final Set<Subscription> existing = reference.get(); |
| 55 | + if (existing == UNSUBSCRIBED_STATE) { |
61 | 56 | s.unsubscribe();
|
62 |
| - this.subscriptions.remove(s); |
63 |
| - } catch (Throwable e) { |
64 |
| - if (es == null) { |
65 |
| - es = new ArrayList<Throwable>(); |
66 |
| - } |
67 |
| - es.add(e); |
| 57 | + break; |
68 | 58 | }
|
69 |
| - } |
70 |
| - if (es != null) { |
71 |
| - throw new CompositeException("Failed to unsubscribe to 1 or more subscriptions.", es); |
72 |
| - } |
73 |
| - } |
74 | 59 |
|
75 |
| - /** |
76 |
| - * Remove the {@link Subscription} and unsubscribe it. |
77 |
| - * |
78 |
| - * @param s |
79 |
| - */ |
80 |
| - public void remove(Subscription s) { |
81 |
| - this.subscriptions.remove(s); |
82 |
| - // also unsubscribe from it: http://msdn.microsoft.com/en-us/library/system.reactive.disposables.compositedisposable.remove(v=vs.103).aspx |
83 |
| - s.unsubscribe(); |
| 60 | + if (reference.compareAndSet(existing, MUTATE_STATE)) { |
| 61 | + existing.add(s); |
| 62 | + reference.set(existing); |
| 63 | + break; |
| 64 | + } |
| 65 | + } while (true); |
84 | 66 | }
|
85 | 67 |
|
86 |
| - public boolean isUnsubscribed() { |
87 |
| - return unsubscribed.get(); |
| 68 | + public void remove(final Subscription s) { |
| 69 | + do { |
| 70 | + final Set<Subscription> subscriptions = reference.get(); |
| 71 | + if (subscriptions == UNSUBSCRIBED_STATE) { |
| 72 | + s.unsubscribe(); |
| 73 | + break; |
| 74 | + } |
| 75 | + |
| 76 | + if (reference.compareAndSet(subscriptions, MUTATE_STATE)) { |
| 77 | + // also unsubscribe from it: |
| 78 | + // http://msdn.microsoft.com/en-us/library/system.reactive.disposables.compositedisposable.remove(v=vs.103).aspx |
| 79 | + subscriptions.remove(s); |
| 80 | + reference.set(subscriptions); |
| 81 | + s.unsubscribe(); |
| 82 | + break; |
| 83 | + } |
| 84 | + } while (true); |
88 | 85 | }
|
89 | 86 |
|
90 |
| - public synchronized void add(Subscription s) { |
91 |
| - if (unsubscribed.get()) { |
92 |
| - s.unsubscribe(); |
93 |
| - } else { |
94 |
| - subscriptions.put(s, Boolean.TRUE); |
95 |
| - } |
| 87 | + public void clear() { |
| 88 | + do { |
| 89 | + final Set<Subscription> subscriptions = reference.get(); |
| 90 | + if (subscriptions == UNSUBSCRIBED_STATE) { |
| 91 | + break; |
| 92 | + } |
| 93 | + |
| 94 | + if (reference.compareAndSet(subscriptions, MUTATE_STATE)) { |
| 95 | + final Set<Subscription> copy = new HashSet<Subscription>( |
| 96 | + subscriptions); |
| 97 | + subscriptions.clear(); |
| 98 | + reference.set(subscriptions); |
| 99 | + |
| 100 | + for (final Subscription subscription : copy) { |
| 101 | + subscription.unsubscribe(); |
| 102 | + } |
| 103 | + break; |
| 104 | + } |
| 105 | + } while (true); |
96 | 106 | }
|
97 | 107 |
|
98 | 108 | @Override
|
99 |
| - public synchronized void unsubscribe() { |
100 |
| - if (unsubscribed.compareAndSet(false, true)) { |
101 |
| - Collection<Throwable> es = null; |
102 |
| - for (Subscription s : subscriptions.keySet()) { |
103 |
| - try { |
104 |
| - s.unsubscribe(); |
105 |
| - } catch (Throwable e) { |
106 |
| - if (es == null) { |
107 |
| - es = new ArrayList<Throwable>(); |
| 109 | + public void unsubscribe() { |
| 110 | + do { |
| 111 | + final Set<Subscription> subscriptions = reference.get(); |
| 112 | + if (subscriptions == UNSUBSCRIBED_STATE) { |
| 113 | + break; |
| 114 | + } |
| 115 | + |
| 116 | + if (subscriptions == MUTATE_STATE) { |
| 117 | + continue; |
| 118 | + } |
| 119 | + |
| 120 | + if (reference.compareAndSet(subscriptions, UNSUBSCRIBED_STATE)) { |
| 121 | + final Collection<Throwable> es = new ArrayList<Throwable>(); |
| 122 | + for (final Subscription s : subscriptions) { |
| 123 | + try { |
| 124 | + s.unsubscribe(); |
| 125 | + } catch (final Throwable e) { |
| 126 | + es.add(e); |
108 | 127 | }
|
109 |
| - es.add(e); |
110 | 128 | }
|
| 129 | + if (es.isEmpty()) { |
| 130 | + break; |
| 131 | + } |
| 132 | + throw new CompositeException( |
| 133 | + "Failed to unsubscribe to 1 or more subscriptions.", es); |
111 | 134 | }
|
112 |
| - if (es != null) { |
113 |
| - throw new CompositeException("Failed to unsubscribe to 1 or more subscriptions.", es); |
114 |
| - } |
115 |
| - } |
| 135 | + } while (true); |
116 | 136 | }
|
117 | 137 | }
|
0 commit comments