-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #568 from jloisel/master
Use lock free strategy for several Subscription implementations
- Loading branch information
Showing
4 changed files
with
455 additions
and
218 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 62 additions & 70 deletions
132
rxjava-core/src/main/java/rx/subscriptions/SerialSubscription.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,62 @@ | ||
/** | ||
* Copyright 2013 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package rx.subscriptions; | ||
|
||
import rx.Subscription; | ||
|
||
/** | ||
* Represents a subscription whose underlying subscription can be swapped for another subscription | ||
* which causes the previous underlying subscription to be unsubscribed. | ||
* | ||
* @see <a href="http://msdn.microsoft.com/en-us/library/system.reactive.disposables.serialdisposable(v=vs.103).aspx">Rx.Net equivalent SerialDisposable</a> | ||
*/ | ||
public class SerialSubscription implements Subscription { | ||
private boolean unsubscribed; | ||
private Subscription subscription; | ||
private final Object gate = new Object(); | ||
|
||
@Override | ||
public void unsubscribe() { | ||
Subscription toUnsubscribe = null; | ||
synchronized (gate) { | ||
if (!unsubscribed) { | ||
if (subscription != null) { | ||
toUnsubscribe = subscription; | ||
subscription = null; | ||
} | ||
unsubscribed = true; | ||
} | ||
} | ||
if (toUnsubscribe != null) { | ||
toUnsubscribe.unsubscribe(); | ||
} | ||
} | ||
|
||
public Subscription getSubscription() { | ||
synchronized (gate) { | ||
return subscription; | ||
} | ||
} | ||
|
||
public void setSubscription(Subscription subscription) { | ||
Subscription toUnsubscribe = null; | ||
synchronized (gate) { | ||
if (!unsubscribed) { | ||
if (this.subscription != null) { | ||
toUnsubscribe = this.subscription; | ||
} | ||
this.subscription = subscription; | ||
} else { | ||
toUnsubscribe = subscription; | ||
} | ||
} | ||
if (toUnsubscribe != null) { | ||
toUnsubscribe.unsubscribe(); | ||
} | ||
} | ||
} | ||
/** | ||
* Copyright 2013 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package rx.subscriptions; | ||
|
||
import static rx.subscriptions.Subscriptions.empty; | ||
|
||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
import rx.Subscription; | ||
|
||
/** | ||
* Represents a subscription whose underlying subscription can be swapped for another subscription | ||
* which causes the previous underlying subscription to be unsubscribed. | ||
* | ||
* @see <a href="http://msdn.microsoft.com/en-us/library/system.reactive.disposables.serialdisposable(v=vs.103).aspx">Rx.Net equivalent SerialDisposable</a> | ||
*/ | ||
public class SerialSubscription implements Subscription { | ||
private final AtomicReference<Subscription> reference = new AtomicReference<Subscription>(empty()); | ||
|
||
private static final Subscription UNSUBSCRIBED = new Subscription() { | ||
@Override | ||
public void unsubscribe() { | ||
} | ||
}; | ||
|
||
@Override | ||
public void unsubscribe() { | ||
setSubscription(UNSUBSCRIBED); | ||
} | ||
|
||
public void setSubscription(final Subscription subscription) { | ||
do { | ||
final Subscription current = reference.get(); | ||
if (current == UNSUBSCRIBED) { | ||
subscription.unsubscribe(); | ||
break; | ||
} | ||
if (reference.compareAndSet(current, subscription)) { | ||
current.unsubscribe(); | ||
break; | ||
} | ||
} while (true); | ||
} | ||
|
||
public Subscription getSubscription() { | ||
final Subscription subscription = reference.get(); | ||
return subscription == UNSUBSCRIBED ? null : subscription; | ||
} | ||
} |
Oops, something went wrong.