138 lines
4.4 KiB
Java
138 lines
4.4 KiB
Java
package io.reactivex.internal.schedulers;
|
|
|
|
import io.reactivex.Scheduler;
|
|
import io.reactivex.disposables.CompositeDisposable;
|
|
import io.reactivex.disposables.Disposable;
|
|
import io.reactivex.internal.disposables.EmptyDisposable;
|
|
import io.reactivex.internal.disposables.ListCompositeDisposable;
|
|
import java.util.concurrent.ThreadFactory;
|
|
import java.util.concurrent.TimeUnit;
|
|
import java.util.concurrent.atomic.AtomicReference;
|
|
|
|
/* loaded from: classes2.dex */
|
|
public final class ComputationScheduler extends Scheduler implements SchedulerMultiWorkerSupport {
|
|
static final FixedSchedulerPool d;
|
|
static final RxThreadFactory e;
|
|
static final int f = a(Runtime.getRuntime().availableProcessors(), Integer.getInteger("rx2.computation-threads", 0).intValue());
|
|
static final PoolWorker g = new PoolWorker(new RxThreadFactory("RxComputationShutdown"));
|
|
final ThreadFactory b;
|
|
final AtomicReference<FixedSchedulerPool> c;
|
|
|
|
static final class FixedSchedulerPool implements SchedulerMultiWorkerSupport {
|
|
final int a;
|
|
final PoolWorker[] b;
|
|
long c;
|
|
|
|
FixedSchedulerPool(int i, ThreadFactory threadFactory) {
|
|
this.a = i;
|
|
this.b = new PoolWorker[i];
|
|
for (int i2 = 0; i2 < i; i2++) {
|
|
this.b[i2] = new PoolWorker(threadFactory);
|
|
}
|
|
}
|
|
|
|
public PoolWorker a() {
|
|
int i = this.a;
|
|
if (i == 0) {
|
|
return ComputationScheduler.g;
|
|
}
|
|
PoolWorker[] poolWorkerArr = this.b;
|
|
long j = this.c;
|
|
this.c = 1 + j;
|
|
return poolWorkerArr[(int) (j % i)];
|
|
}
|
|
|
|
public void b() {
|
|
for (PoolWorker poolWorker : this.b) {
|
|
poolWorker.dispose();
|
|
}
|
|
}
|
|
}
|
|
|
|
static final class PoolWorker extends NewThreadWorker {
|
|
PoolWorker(ThreadFactory threadFactory) {
|
|
super(threadFactory);
|
|
}
|
|
}
|
|
|
|
static {
|
|
g.dispose();
|
|
e = new RxThreadFactory("RxComputationThreadPool", Math.max(1, Math.min(10, Integer.getInteger("rx2.computation-priority", 5).intValue())), true);
|
|
d = new FixedSchedulerPool(0, e);
|
|
d.b();
|
|
}
|
|
|
|
public ComputationScheduler() {
|
|
this(e);
|
|
}
|
|
|
|
static int a(int i, int i2) {
|
|
return (i2 <= 0 || i2 > i) ? i : i2;
|
|
}
|
|
|
|
@Override // io.reactivex.Scheduler
|
|
public Scheduler.Worker a() {
|
|
return new EventLoopWorker(this.c.get().a());
|
|
}
|
|
|
|
public void b() {
|
|
FixedSchedulerPool fixedSchedulerPool = new FixedSchedulerPool(f, this.b);
|
|
if (this.c.compareAndSet(d, fixedSchedulerPool)) {
|
|
return;
|
|
}
|
|
fixedSchedulerPool.b();
|
|
}
|
|
|
|
public ComputationScheduler(ThreadFactory threadFactory) {
|
|
this.b = threadFactory;
|
|
this.c = new AtomicReference<>(d);
|
|
b();
|
|
}
|
|
|
|
@Override // io.reactivex.Scheduler
|
|
public Disposable a(Runnable runnable, long j, TimeUnit timeUnit) {
|
|
return this.c.get().a().b(runnable, j, timeUnit);
|
|
}
|
|
|
|
static final class EventLoopWorker extends Scheduler.Worker {
|
|
private final ListCompositeDisposable a = new ListCompositeDisposable();
|
|
private final CompositeDisposable b = new CompositeDisposable();
|
|
private final ListCompositeDisposable c = new ListCompositeDisposable();
|
|
private final PoolWorker d;
|
|
volatile boolean e;
|
|
|
|
EventLoopWorker(PoolWorker poolWorker) {
|
|
this.d = poolWorker;
|
|
this.c.b(this.a);
|
|
this.c.b(this.b);
|
|
}
|
|
|
|
@Override // io.reactivex.Scheduler.Worker
|
|
public Disposable a(Runnable runnable) {
|
|
return this.e ? EmptyDisposable.INSTANCE : this.d.a(runnable, 0L, TimeUnit.MILLISECONDS, this.a);
|
|
}
|
|
|
|
@Override // io.reactivex.disposables.Disposable
|
|
public void dispose() {
|
|
if (this.e) {
|
|
return;
|
|
}
|
|
this.e = true;
|
|
this.c.dispose();
|
|
}
|
|
|
|
@Override // io.reactivex.Scheduler.Worker
|
|
public Disposable a(Runnable runnable, long j, TimeUnit timeUnit) {
|
|
if (this.e) {
|
|
return EmptyDisposable.INSTANCE;
|
|
}
|
|
return this.d.a(runnable, j, timeUnit, this.b);
|
|
}
|
|
}
|
|
|
|
@Override // io.reactivex.Scheduler
|
|
public Disposable a(Runnable runnable, long j, long j2, TimeUnit timeUnit) {
|
|
return this.c.get().a().b(runnable, j, j2, timeUnit);
|
|
}
|
|
}
|