File tree Expand file tree Collapse file tree 3 files changed +45
-8
lines changed
main/java/rx/internal/util Expand file tree Collapse file tree 3 files changed +45
-8
lines changed Original file line number Diff line number Diff line change @@ -54,7 +54,7 @@ public synchronized boolean isUnsubscribed() {
54
54
* @param s
55
55
* the {@link Subscription} to add
56
56
*/
57
- public void add (final Subscription s ) {
57
+ public synchronized void add (final Subscription s ) {
58
58
if (unsubscribed ) {
59
59
s .unsubscribe ();
60
60
} else {
@@ -71,13 +71,19 @@ public void add(final Subscription s) {
71
71
*/
72
72
@ Override
73
73
public void unsubscribe () {
74
- if (unsubscribed ) {
75
- return ;
74
+ Collection <Subscription > toUnsubscribe = null ;
75
+ synchronized (this ) {
76
+ if (unsubscribed ) {
77
+ return ;
78
+ }
79
+ unsubscribed = true ;
80
+ toUnsubscribe = subscriptions ;
81
+ subscriptions = null ;
82
+ }
83
+
84
+ if (toUnsubscribe != null ) {
85
+ unsubscribeFromAll (toUnsubscribe );
76
86
}
77
- unsubscribed = true ;
78
- // we will only get here once
79
- unsubscribeFromAll (subscriptions );
80
- subscriptions = null ;
81
87
}
82
88
83
89
private static void unsubscribeFromAll (Collection <Subscription > subscriptions ) {
Original file line number Diff line number Diff line change @@ -39,6 +39,16 @@ public void observableConsumption(Input input) throws InterruptedException {
39
39
public void observableViaRange (Input input ) throws InterruptedException {
40
40
input .observable .subscribe (input .observer );
41
41
}
42
+
43
+ @ Benchmark
44
+ public void observableConsumptionUnsafe (Input input ) throws InterruptedException {
45
+ input .firehose .unsafeSubscribe (input .newSubscriber ());
46
+ }
47
+
48
+ @ Benchmark
49
+ public void observableViaRangeUnsafe (Input input ) throws InterruptedException {
50
+ input .observable .unsafeSubscribe (input .newSubscriber ());
51
+ }
42
52
43
53
@ Benchmark
44
54
public void iterableViaForLoopConsumption (Input input ) throws InterruptedException {
Original file line number Diff line number Diff line change @@ -36,7 +36,7 @@ public abstract class InputWithIncrementingInteger {
36
36
public Observer <Integer > observer ;
37
37
38
38
public abstract int getSize ();
39
-
39
+
40
40
@ Setup
41
41
public void setup (final Blackhole bh ) {
42
42
this .bh = bh ;
@@ -106,4 +106,25 @@ public LatchedObserver<Integer> newLatchedObserver() {
106
106
return new LatchedObserver <Integer >(bh );
107
107
}
108
108
109
+ public Subscriber <Integer > newSubscriber () {
110
+ return new Subscriber <Integer >() {
111
+
112
+ @ Override
113
+ public void onCompleted () {
114
+
115
+ }
116
+
117
+ @ Override
118
+ public void onError (Throwable e ) {
119
+
120
+ }
121
+
122
+ @ Override
123
+ public void onNext (Integer t ) {
124
+ bh .consume (t );
125
+ }
126
+
127
+ };
128
+ }
129
+
109
130
}
You can’t perform that action at this time.
0 commit comments