| 1 |
/* |
| 2 |
* Copyright (c) 2016-present, RxJava Contributors. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 |
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See |
| 11 |
* the License for the specific language governing permissions and limitations under the License. |
| 12 |
*/ |
| 13 |
|
| 14 |
package io.reactivex.rxjava3.internal.schedulers; |
| 15 |
|
| 16 |
import java.util.concurrent.*; |
| 17 |
import java.util.concurrent.atomic.AtomicReference; |
| 18 |
|
| 19 |
import io.reactivex.rxjava3.annotations.NonNull; |
| 20 |
import io.reactivex.rxjava3.core.Scheduler; |
| 21 |
import io.reactivex.rxjava3.disposables.*; |
| 22 |
import io.reactivex.rxjava3.internal.disposables.*; |
| 23 |
import io.reactivex.rxjava3.internal.functions.ObjectHelper; |
| 24 |
|
| 25 |
/** |
| 26 |
* Holds a fixed pool of worker threads and assigns them |
| 27 |
* to requested Scheduler.Workers in a round-robin fashion. |
| 28 |
*/ |
| 29 |
public final class ComputationScheduler extends Scheduler implements SchedulerMultiWorkerSupport { |
| 30 |
/** This will indicate no pool is active. */ |
| 31 |
static final FixedSchedulerPool NONE; |
| 32 |
/** Manages a fixed number of workers. */ |
| 33 |
private static final String THREAD_NAME_PREFIX = "RxComputationThreadPool"; |
| 34 |
static final RxThreadFactory THREAD_FACTORY; |
| 35 |
/** |
| 36 |
* Key to setting the maximum number of computation scheduler threads. |
| 37 |
* Zero or less is interpreted as use available. Capped by available. |
| 38 |
*/ |
| 39 |
static final String KEY_MAX_THREADS = "rx3.computation-threads"; |
| 40 |
/** The maximum number of computation scheduler threads. */ |
| 41 |
static final int MAX_THREADS; |
| 42 |
|
| 43 |
static final PoolWorker SHUTDOWN_WORKER; |
| 44 |
|
| 45 |
final ThreadFactory threadFactory; |
| 46 |
final AtomicReference<FixedSchedulerPool> pool; |
| 47 |
/** The name of the system property for setting the thread priority for this Scheduler. */ |
| 48 |
private static final String KEY_COMPUTATION_PRIORITY = "rx3.computation-priority"; |
| 49 |
|
| 50 |
static { |
| 51 |
MAX_THREADS = cap(Runtime.getRuntime().availableProcessors(), Integer.getInteger(KEY_MAX_THREADS, 0)); |
| 52 |
|
| 53 |
SHUTDOWN_WORKER = new PoolWorker(new RxThreadFactory("RxComputationShutdown")); |
| 54 |
SHUTDOWN_WORKER.dispose(); |
| 55 |
|
| 56 |
int priority = Math.max(Thread.MIN_PRIORITY, Math.min(Thread.MAX_PRIORITY, |
| 57 |
Integer.getInteger(KEY_COMPUTATION_PRIORITY, Thread.NORM_PRIORITY))); |
| 58 |
|
| 59 |
THREAD_FACTORY = new RxThreadFactory(THREAD_NAME_PREFIX, priority, true); |
| 60 |
|
| 61 |
NONE = new FixedSchedulerPool(0, THREAD_FACTORY); |
| 62 |
NONE.shutdown(); |
| 63 |
} |
| 64 |
|
| 65 |
static int cap(int cpuCount, int paramThreads) { |
| 66 |
return paramThreads <= 0 || paramThreads > cpuCount ? cpuCount : paramThreads; |
| 67 |
} |
| 68 |
|
| 69 |
static final class FixedSchedulerPool implements SchedulerMultiWorkerSupport { |
| 70 |
final int cores; |
| 71 |
|
| 72 |
final PoolWorker[] eventLoops; |
| 73 |
long n; |
| 74 |
|
| 75 |
FixedSchedulerPool(int maxThreads, ThreadFactory threadFactory) { |
| 76 |
// initialize event loops |
| 77 |
this.cores = maxThreads; |
| 78 |
this.eventLoops = new PoolWorker[maxThreads]; |
| 79 |
for (int i = 0; i < maxThreads; i++) { |
| 80 |
this.eventLoops[i] = new PoolWorker(threadFactory); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
public PoolWorker getEventLoop() { |
| 85 |
int c = cores; |
| 86 |
if (c == 0) { |
| 87 |
return SHUTDOWN_WORKER; |
| 88 |
} |
| 89 |
// simple round robin, improvements to come |
| 90 |
return eventLoops[(int)(n++ % c)]; |
| 91 |
} |
| 92 |
|
| 93 |
public void shutdown() { |
| 94 |
for (PoolWorker w : eventLoops) { |
| 95 |
w.dispose(); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
@Override |
| 100 |
public void createWorkers(int number, WorkerCallback callback) { |
| 101 |
int c = cores; |
| 102 |
if (c == 0) { |
| 103 |
for (int i = 0; i < number; i++) { |
| 104 |
callback.onWorker(i, SHUTDOWN_WORKER); |
| 105 |
} |
| 106 |
} else { |
| 107 |
int index = (int)n % c; |
| 108 |
for (int i = 0; i < number; i++) { |
| 109 |
callback.onWorker(i, new EventLoopWorker(eventLoops[index])); |
| 110 |
if (++index == c) { |
| 111 |
index = 0; |
| 112 |
} |
| 113 |
} |
| 114 |
n = index; |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Create a scheduler with pool size equal to the available processor |
| 121 |
* count and using least-recent worker selection policy. |
| 122 |
*/ |
| 123 |
public ComputationScheduler() { |
| 124 |
this(THREAD_FACTORY); |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Create a scheduler with pool size equal to the available processor |
| 129 |
* count and using least-recent worker selection policy. |
| 130 |
* |
| 131 |
* @param threadFactory thread factory to use for creating worker threads. Note that this takes precedence over any |
| 132 |
* system properties for configuring new thread creation. Cannot be null. |
| 133 |
*/ |
| 134 |
public ComputationScheduler(ThreadFactory threadFactory) { |
| 135 |
this.threadFactory = threadFactory; |
| 136 |
this.pool = new AtomicReference<>(NONE); |
| 137 |
start(); |
| 138 |
} |
| 139 |
|
| 140 |
@NonNull |
| 141 |
@Override |
| 142 |
public Worker createWorker() { |
| 143 |
return new EventLoopWorker(pool.get().getEventLoop()); |
| 144 |
} |
| 145 |
|
| 146 |
@Override |
| 147 |
public void createWorkers(int number, WorkerCallback callback) { |
| 148 |
ObjectHelper.verifyPositive(number, "number > 0 required"); |
| 149 |
pool.get().createWorkers(number, callback); |
| 150 |
} |
| 151 |
|
| 152 |
@NonNull |
| 153 |
@Override |
| 154 |
public Disposable scheduleDirect(@NonNull Runnable run, long delay, TimeUnit unit) { |
| 155 |
PoolWorker w = pool.get().getEventLoop(); |
| 156 |
return w.scheduleDirect(run, delay, unit); |
| 157 |
} |
| 158 |
|
| 159 |
@NonNull |
| 160 |
@Override |
| 161 |
public Disposable schedulePeriodicallyDirect(@NonNull Runnable run, long initialDelay, long period, TimeUnit unit) { |
| 162 |
PoolWorker w = pool.get().getEventLoop(); |
| 163 |
return w.schedulePeriodicallyDirect(run, initialDelay, period, unit); |
| 164 |
} |
| 165 |
|
| 166 |
@Override |
| 167 |
public void start() { |
| 168 |
FixedSchedulerPool update = new FixedSchedulerPool(MAX_THREADS, threadFactory); |
| 169 |
if (!pool.compareAndSet(NONE, update)) { |
| 170 |
update.shutdown(); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
@Override |
| 175 |
public void shutdown() { |
| 176 |
FixedSchedulerPool curr = pool.getAndSet(NONE); |
| 177 |
if (curr != NONE) { |
| 178 |
curr.shutdown(); |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
static final class EventLoopWorker extends Scheduler.Worker { |
| 183 |
private final ListCompositeDisposable serial; |
| 184 |
private final CompositeDisposable timed; |
| 185 |
private final ListCompositeDisposable both; |
| 186 |
private final PoolWorker poolWorker; |
| 187 |
|
| 188 |
volatile boolean disposed; |
| 189 |
|
| 190 |
EventLoopWorker(PoolWorker poolWorker) { |
| 191 |
this.poolWorker = poolWorker; |
| 192 |
this.serial = new ListCompositeDisposable(); |
| 193 |
this.timed = new CompositeDisposable(); |
| 194 |
this.both = new ListCompositeDisposable(); |
| 195 |
this.both.add(serial); |
| 196 |
this.both.add(timed); |
| 197 |
} |
| 198 |
|
| 199 |
@Override |
| 200 |
public void dispose() { |
| 201 |
if (!disposed) { |
| 202 |
disposed = true; |
| 203 |
both.dispose(); |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
@Override |
| 208 |
public boolean isDisposed() { |
| 209 |
return disposed; |
| 210 |
} |
| 211 |
|
| 212 |
@NonNull |
| 213 |
@Override |
| 214 |
public Disposable schedule(@NonNull Runnable action) { |
| 215 |
if (disposed) { |
| 216 |
return EmptyDisposable.INSTANCE; |
| 217 |
} |
| 218 |
|
| 219 |
return poolWorker.scheduleActual(action, 0, TimeUnit.MILLISECONDS, serial); |
| 220 |
} |
| 221 |
|
| 222 |
@NonNull |
| 223 |
@Override |
| 224 |
public Disposable schedule(@NonNull Runnable action, long delayTime, @NonNull TimeUnit unit) { |
| 225 |
if (disposed) { |
| 226 |
return EmptyDisposable.INSTANCE; |
| 227 |
} |
| 228 |
|
| 229 |
return poolWorker.scheduleActual(action, delayTime, unit, timed); |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
static final class PoolWorker extends NewThreadWorker { |
| 234 |
PoolWorker(ThreadFactory threadFactory) { |
| 235 |
super(threadFactory); |
| 236 |
} |
| 237 |
} |
| 238 |
} |