forked from rsocket/rsocket-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9f0b516
commit 7b1e789
Showing
8 changed files
with
503 additions
and
83 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
37 changes: 37 additions & 0 deletions
37
src/main/java/io/reactivesocket/internal/BooleanDisposable.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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package io.reactivesocket.internal; | ||
|
||
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; | ||
|
||
import io.reactivesocket.observable.Disposable; | ||
|
||
public final class BooleanDisposable implements Disposable { | ||
volatile Runnable run; | ||
|
||
static final AtomicReferenceFieldUpdater<BooleanDisposable, Runnable> RUN = | ||
AtomicReferenceFieldUpdater.newUpdater(BooleanDisposable.class, Runnable.class, "run"); | ||
|
||
static final Runnable DISPOSED = () -> { }; | ||
|
||
public BooleanDisposable() { | ||
this(() -> { }); | ||
} | ||
|
||
public BooleanDisposable(Runnable run) { | ||
RUN.lazySet(this, run); | ||
} | ||
|
||
@Override | ||
public void dispose() { | ||
Runnable r = run; | ||
if (r != DISPOSED) { | ||
r = RUN.getAndSet(this, DISPOSED); | ||
if (r != DISPOSED) { | ||
r.run(); | ||
} | ||
} | ||
} | ||
|
||
public boolean isDisposed() { | ||
return run == DISPOSED; | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
src/main/java/io/reactivesocket/internal/CompositeCompletable.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 |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package io.reactivesocket.internal; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import io.reactivesocket.Completable; | ||
|
||
/** | ||
* A Completable container that can hold onto multiple other Completables. | ||
*/ | ||
public final class CompositeCompletable implements Completable { | ||
|
||
// protected by synchronized | ||
private boolean completed = false; | ||
private Throwable error = null; | ||
final Set<Completable> resources = new HashSet<>(); | ||
|
||
public CompositeCompletable() { | ||
|
||
} | ||
|
||
public void add(Completable d) { | ||
boolean terminal = false; | ||
synchronized (this) { | ||
if (error != null || completed) { | ||
terminal = true; | ||
} else { | ||
resources.add(d); | ||
} | ||
} | ||
if (terminal) { | ||
if (error != null) { | ||
d.error(error); | ||
} else { | ||
d.success(); | ||
} | ||
} | ||
} | ||
|
||
public void remove(Completable d) { | ||
synchronized (this) { | ||
resources.remove(d); | ||
} | ||
} | ||
|
||
public void clear() { | ||
synchronized (this) { | ||
resources.clear(); | ||
} | ||
} | ||
|
||
@Override | ||
public void success() { | ||
Completable[] cs = null; | ||
synchronized (this) { | ||
if (error == null) { | ||
completed = true; | ||
cs = resources.toArray(new Completable[] {}); | ||
resources.clear(); | ||
} | ||
} | ||
if (cs != null) { | ||
for (Completable c : cs) { | ||
c.success(); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void error(Throwable e) { | ||
Completable[] cs = null; | ||
synchronized (this) { | ||
if (error == null && !completed) { | ||
error = e; | ||
cs = resources.toArray(new Completable[] {}); | ||
resources.clear(); | ||
} | ||
} | ||
if (cs != null) { | ||
for (Completable c : cs) { | ||
c.error(e); | ||
} | ||
} | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/main/java/io/reactivesocket/internal/CompositeDisposable.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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package io.reactivesocket.internal; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import io.reactivesocket.Completable; | ||
import io.reactivesocket.observable.Disposable; | ||
|
||
/** | ||
* A Disposable container that can hold onto multiple other Disposables. | ||
*/ | ||
public final class CompositeDisposable implements Disposable { | ||
|
||
// protected by synchronized | ||
private boolean disposed = false; | ||
final Set<Disposable> resources = new HashSet<>(); | ||
|
||
public CompositeDisposable() { | ||
|
||
} | ||
|
||
public void add(Disposable d) { | ||
boolean isDisposed = false; | ||
synchronized (this) { | ||
if (disposed) { | ||
isDisposed = true; | ||
} else { | ||
resources.add(d); | ||
} | ||
} | ||
if (isDisposed) { | ||
d.dispose(); | ||
} | ||
} | ||
|
||
public void remove(Completable d) { | ||
synchronized (this) { | ||
resources.remove(d); | ||
} | ||
} | ||
|
||
public void clear() { | ||
synchronized (this) { | ||
resources.clear(); | ||
} | ||
} | ||
|
||
@Override | ||
public void dispose() { | ||
Disposable[] cs = null; | ||
synchronized (this) { | ||
disposed = true; | ||
cs = resources.toArray(new Disposable[] {}); | ||
resources.clear(); | ||
} | ||
for (Disposable d : cs) { | ||
d.dispose(); | ||
} | ||
} | ||
|
||
} |
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
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
Oops, something went wrong.