Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1.x: fix from(Iterable) error handling of Iterable/Iterator #3862

Merged
merged 3 commits into from
Apr 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 94 additions & 36 deletions src/main/java/rx/internal/operators/OnSubscribeFromIterable.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import rx.*;
import rx.Observable.OnSubscribe;
import rx.exceptions.Exceptions;

/**
* Converts an {@code Iterable} sequence into an {@code Observable}.
Expand All @@ -42,11 +43,25 @@ public OnSubscribeFromIterable(Iterable<? extends T> iterable) {

@Override
public void call(final Subscriber<? super T> o) {
final Iterator<? extends T> it = is.iterator();
if (!it.hasNext() && !o.isUnsubscribed())
o.onCompleted();
else
o.setProducer(new IterableProducer<T>(o, it));
final Iterator<? extends T> it;
boolean b;

try {
it = is.iterator();

b = it.hasNext();
} catch (Throwable ex) {
Exceptions.throwOrReport(ex, o);
return;
}

if (!o.isUnsubscribed()) {
if (!b) {
o.onCompleted();
} else {
o.setProducer(new IterableProducer<T>(o, it));
}
}
}

private static final class IterableProducer<T> extends AtomicLong implements Producer {
Expand Down Expand Up @@ -81,55 +96,98 @@ void slowpath(long n) {
final Iterator<? extends T> it = this.it;

long r = n;
while (true) {
/*
* This complicated logic is done to avoid touching the
* volatile `requested` value during the loop itself. If
* it is touched during the loop the performance is
* impacted significantly.
*/
long numToEmit = r;
while (true) {
long e = 0;

for (;;) {
while (e != r) {
if (o.isUnsubscribed()) {
return;
} else if (it.hasNext()) {
if (--numToEmit >= 0) {
o.onNext(it.next());
} else
break;
} else if (!o.isUnsubscribed()) {
o.onCompleted();
}

T value;

try {
value = it.next();
} catch (Throwable ex) {
Exceptions.throwOrReport(ex, o);
return;
} else {
// is unsubscribed
}

o.onNext(value);

if (o.isUnsubscribed()) {
return;
}

boolean b;

try {
b = it.hasNext();
} catch (Throwable ex) {
Exceptions.throwOrReport(ex, o);
return;
}

if (!b) {
if (!o.isUnsubscribed()) {
o.onCompleted();
}
return;
}

e++;
}
r = addAndGet(-r);
if (r == 0L) {
// we're done emitting the number requested so
// return
return;

r = get();
if (e == r) {
r = BackpressureUtils.produced(this, e);
if (r == 0L) {
break;
}
e = 0L;
}

}

}

void fastpath() {
// fast-path without backpressure
final Subscriber<? super T> o = this.o;
final Iterator<? extends T> it = this.it;

while (true) {
for (;;) {
if (o.isUnsubscribed()) {
return;
} else if (it.hasNext()) {
o.onNext(it.next());
} else if (!o.isUnsubscribed()) {
o.onCompleted();
}

T value;

try {
value = it.next();
} catch (Throwable ex) {
Exceptions.throwOrReport(ex, o);
return;
}

o.onNext(value);

if (o.isUnsubscribed()) {
return;
} else {
// is unsubscribed
}

boolean b;

try {
b = it.hasNext();
} catch (Throwable ex) {
Exceptions.throwOrReport(ex, o);
return;
}

if (!b) {
if (!o.isUnsubscribed()) {
o.onCompleted();
}
return;
}
}
Expand Down
Loading