package io.reactivex.internal.schedulers;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicReference;
import io.reactivex.Scheduler;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.*;
import io.reactivex.internal.disposables.*;
import io.reactivex.internal.functions.ObjectHelper;
public final class ComputationScheduler extends Scheduler implements SchedulerMultiWorkerSupport {
static final FixedSchedulerPool NONE;
private static final String THREAD_NAME_PREFIX = "RxComputationThreadPool";
static final RxThreadFactory THREAD_FACTORY;
static final String KEY_MAX_THREADS = "rx2.computation-threads";
static final int MAX_THREADS;
static final PoolWorker SHUTDOWN_WORKER;
final ThreadFactory threadFactory;
final AtomicReference<FixedSchedulerPool> pool;
private static final String KEY_COMPUTATION_PRIORITY = "rx2.computation-priority";
static {
MAX_THREADS = cap(Runtime.getRuntime().availableProcessors(), Integer.getInteger(KEY_MAX_THREADS, 0));
SHUTDOWN_WORKER = new PoolWorker(new RxThreadFactory("RxComputationShutdown"));
SHUTDOWN_WORKER.dispose();
int priority = Math.max(Thread.MIN_PRIORITY, Math.min(Thread.MAX_PRIORITY,
Integer.getInteger(KEY_COMPUTATION_PRIORITY, Thread.NORM_PRIORITY)));
THREAD_FACTORY = new RxThreadFactory(THREAD_NAME_PREFIX, priority, true);
NONE = new FixedSchedulerPool(0, THREAD_FACTORY);
NONE.shutdown();
}
static int cap(int cpuCount, int paramThreads) {
return paramThreads <= 0 || paramThreads > cpuCount ? cpuCount : paramThreads;
}
static final class FixedSchedulerPool implements SchedulerMultiWorkerSupport {
final int cores;
final PoolWorker[] eventLoops;
long n;
FixedSchedulerPool(int maxThreads, ThreadFactory threadFactory) {
this.cores = maxThreads;
this.eventLoops = new PoolWorker[maxThreads];
for (int i = 0; i < maxThreads; i++) {
this.eventLoops[i] = new PoolWorker(threadFactory);
}
}
public PoolWorker getEventLoop() {
int c = cores;
if (c == 0) {
return SHUTDOWN_WORKER;
}
return eventLoops[(int)(n++ % c)];
}
public void shutdown() {
for (PoolWorker w : eventLoops) {
w.dispose();
}
}
@Override
public void createWorkers(int number, WorkerCallback callback) {
int c = cores;
if (c == 0) {
for (int i = 0; i < number; i++) {
callback.onWorker(i, SHUTDOWN_WORKER);
}
} else {
int index = (int)n % c;
for (int i = 0; i < number; i++) {
callback.onWorker(i, new EventLoopWorker(eventLoops[index]));
if (++index == c) {
index = 0;
}
}
n = index;
}
}
}
public ComputationScheduler() {
this(THREAD_FACTORY);
}
public ComputationScheduler(ThreadFactory threadFactory) {
this.threadFactory = threadFactory;
this.pool = new AtomicReference<FixedSchedulerPool>(NONE);
start();
}
@NonNull
@Override
public Worker createWorker() {
return new EventLoopWorker(pool.get().getEventLoop());
}
@Override
public void createWorkers(int number, WorkerCallback callback) {
ObjectHelper.verifyPositive(number, "number > 0 required");
pool.get().createWorkers(number, callback);
}
@NonNull
@Override
public Disposable scheduleDirect(@NonNull Runnable run, long delay, TimeUnit unit) {
PoolWorker w = pool.get().getEventLoop();
return w.scheduleDirect(run, delay, unit);
}
@NonNull
@Override
public Disposable schedulePeriodicallyDirect(@NonNull Runnable run, long initialDelay, long period, TimeUnit unit) {
PoolWorker w = pool.get().getEventLoop();
return w.schedulePeriodicallyDirect(run, initialDelay, period, unit);
}
@Override
public void start() {
FixedSchedulerPool update = new FixedSchedulerPool(MAX_THREADS, threadFactory);
if (!pool.compareAndSet(NONE, update)) {
update.shutdown();
}
}
@Override
public void shutdown() {
for (;;) {
FixedSchedulerPool curr = pool.get();
if (curr == NONE) {
return;
}
if (pool.compareAndSet(curr, NONE)) {
curr.shutdown();
return;
}
}
}
static final class EventLoopWorker extends Scheduler.Worker {
private final ListCompositeDisposable serial;
private final CompositeDisposable timed;
private final ListCompositeDisposable both;
private final PoolWorker poolWorker;
volatile boolean disposed;
EventLoopWorker(PoolWorker poolWorker) {
this.poolWorker = poolWorker;
this.serial = new ListCompositeDisposable();
this.timed = new CompositeDisposable();
this.both = new ListCompositeDisposable();
this.both.add(serial);
this.both.add(timed);
}
@Override
public void dispose() {
if (!disposed) {
disposed = true;
both.dispose();
}
}
@Override
public boolean isDisposed() {
return disposed;
}
@NonNull
@Override
public Disposable schedule(@NonNull Runnable action) {
if (disposed) {
return EmptyDisposable.INSTANCE;
}
return poolWorker.scheduleActual(action, 0, TimeUnit.MILLISECONDS, serial);
}
@NonNull
@Override
public Disposable schedule(@NonNull Runnable action, long delayTime, @NonNull TimeUnit unit) {
if (disposed) {
return EmptyDisposable.INSTANCE;
}
return poolWorker.scheduleActual(action, delayTime, unit, timed);
}
}
static final class PoolWorker extends NewThreadWorker {
PoolWorker(ThreadFactory threadFactory) {
super(threadFactory);
}
}
}