| 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.*; |
| 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.Functions; |
| 24 |
import io.reactivex.rxjava3.internal.queue.MpscLinkedQueue; |
| 25 |
import io.reactivex.rxjava3.internal.schedulers.ExecutorScheduler.ExecutorWorker.*; |
| 26 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 27 |
import io.reactivex.rxjava3.schedulers.*; |
| 28 |
|
| 29 |
/** |
| 30 |
* Wraps an Executor and provides the Scheduler API over it. |
| 31 |
*/ |
| 32 |
public final class ExecutorScheduler extends Scheduler { |
| 33 |
|
| 34 |
final boolean interruptibleWorker; |
| 35 |
|
| 36 |
final boolean fair; |
| 37 |
|
| 38 |
@NonNull |
| 39 |
final Executor executor; |
| 40 |
|
| 41 |
static final class SingleHolder { |
| 42 |
static final Scheduler HELPER = Schedulers.single(); |
| 43 |
} |
| 44 |
|
| 45 |
public ExecutorScheduler(@NonNull Executor executor, boolean interruptibleWorker, boolean fair) { |
| 46 |
this.executor = executor; |
| 47 |
this.interruptibleWorker = interruptibleWorker; |
| 48 |
this.fair = fair; |
| 49 |
} |
| 50 |
|
| 51 |
@NonNull |
| 52 |
@Override |
| 53 |
public Worker createWorker() { |
| 54 |
return new ExecutorWorker(executor, interruptibleWorker, fair); |
| 55 |
} |
| 56 |
|
| 57 |
@NonNull |
| 58 |
@Override |
| 59 |
public Disposable scheduleDirect(@NonNull Runnable run) { |
| 60 |
Runnable decoratedRun = RxJavaPlugins.onSchedule(run); |
| 61 |
try { |
| 62 |
if (executor instanceof ExecutorService) { |
| 63 |
ScheduledDirectTask task = new ScheduledDirectTask(decoratedRun, interruptibleWorker); |
| 64 |
Future<?> f = ((ExecutorService)executor).submit(task); |
| 65 |
task.setFuture(f); |
| 66 |
return task; |
| 67 |
} |
| 68 |
|
| 69 |
if (interruptibleWorker) { |
| 70 |
InterruptibleRunnable interruptibleTask = new InterruptibleRunnable(decoratedRun, null); |
| 71 |
executor.execute(interruptibleTask); |
| 72 |
return interruptibleTask; |
| 73 |
} else { |
| 74 |
BooleanRunnable br = new BooleanRunnable(decoratedRun); |
| 75 |
executor.execute(br); |
| 76 |
return br; |
| 77 |
} |
| 78 |
} catch (RejectedExecutionException ex) { |
| 79 |
RxJavaPlugins.onError(ex); |
| 80 |
return EmptyDisposable.INSTANCE; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
@NonNull |
| 85 |
@Override |
| 86 |
public Disposable scheduleDirect(@NonNull Runnable run, final long delay, final TimeUnit unit) { |
| 87 |
final Runnable decoratedRun = RxJavaPlugins.onSchedule(run); |
| 88 |
if (executor instanceof ScheduledExecutorService) { |
| 89 |
try { |
| 90 |
ScheduledDirectTask task = new ScheduledDirectTask(decoratedRun, interruptibleWorker); |
| 91 |
Future<?> f = ((ScheduledExecutorService)executor).schedule(task, delay, unit); |
| 92 |
task.setFuture(f); |
| 93 |
return task; |
| 94 |
} catch (RejectedExecutionException ex) { |
| 95 |
RxJavaPlugins.onError(ex); |
| 96 |
return EmptyDisposable.INSTANCE; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
final DelayedRunnable dr = new DelayedRunnable(decoratedRun); |
| 101 |
|
| 102 |
Disposable delayed = SingleHolder.HELPER.scheduleDirect(new DelayedDispose(dr), delay, unit); |
| 103 |
|
| 104 |
dr.timed.replace(delayed); |
| 105 |
|
| 106 |
return dr; |
| 107 |
} |
| 108 |
|
| 109 |
@NonNull |
| 110 |
@Override |
| 111 |
public Disposable schedulePeriodicallyDirect(@NonNull Runnable run, long initialDelay, long period, TimeUnit unit) { |
| 112 |
if (executor instanceof ScheduledExecutorService) { |
| 113 |
Runnable decoratedRun = RxJavaPlugins.onSchedule(run); |
| 114 |
try { |
| 115 |
ScheduledDirectPeriodicTask task = new ScheduledDirectPeriodicTask(decoratedRun, interruptibleWorker); |
| 116 |
Future<?> f = ((ScheduledExecutorService)executor).scheduleAtFixedRate(task, initialDelay, period, unit); |
| 117 |
task.setFuture(f); |
| 118 |
return task; |
| 119 |
} catch (RejectedExecutionException ex) { |
| 120 |
RxJavaPlugins.onError(ex); |
| 121 |
return EmptyDisposable.INSTANCE; |
| 122 |
} |
| 123 |
} |
| 124 |
return super.schedulePeriodicallyDirect(run, initialDelay, period, unit); |
| 125 |
} |
| 126 |
/* public: test support. */ |
| 127 |
public static final class ExecutorWorker extends Scheduler.Worker implements Runnable { |
| 128 |
|
| 129 |
final boolean interruptibleWorker; |
| 130 |
|
| 131 |
final boolean fair; |
| 132 |
|
| 133 |
final Executor executor; |
| 134 |
|
| 135 |
final MpscLinkedQueue<Runnable> queue; |
| 136 |
|
| 137 |
volatile boolean disposed; |
| 138 |
|
| 139 |
final AtomicInteger wip = new AtomicInteger(); |
| 140 |
|
| 141 |
final CompositeDisposable tasks = new CompositeDisposable(); |
| 142 |
|
| 143 |
public ExecutorWorker(Executor executor, boolean interruptibleWorker, boolean fair) { |
| 144 |
this.executor = executor; |
| 145 |
this.queue = new MpscLinkedQueue<>(); |
| 146 |
this.interruptibleWorker = interruptibleWorker; |
| 147 |
this.fair = fair; |
| 148 |
} |
| 149 |
|
| 150 |
@NonNull |
| 151 |
@Override |
| 152 |
public Disposable schedule(@NonNull Runnable run) { |
| 153 |
if (disposed) { |
| 154 |
return EmptyDisposable.INSTANCE; |
| 155 |
} |
| 156 |
|
| 157 |
Runnable decoratedRun = RxJavaPlugins.onSchedule(run); |
| 158 |
|
| 159 |
Runnable task; |
| 160 |
Disposable disposable; |
| 161 |
|
| 162 |
if (interruptibleWorker) { |
| 163 |
InterruptibleRunnable interruptibleTask = new InterruptibleRunnable(decoratedRun, tasks); |
| 164 |
tasks.add(interruptibleTask); |
| 165 |
|
| 166 |
task = interruptibleTask; |
| 167 |
disposable = interruptibleTask; |
| 168 |
} else { |
| 169 |
BooleanRunnable runnableTask = new BooleanRunnable(decoratedRun); |
| 170 |
|
| 171 |
task = runnableTask; |
| 172 |
disposable = runnableTask; |
| 173 |
} |
| 174 |
|
| 175 |
queue.offer(task); |
| 176 |
|
| 177 |
if (wip.getAndIncrement() == 0) { |
| 178 |
try { |
| 179 |
executor.execute(this); |
| 180 |
} catch (RejectedExecutionException ex) { |
| 181 |
disposed = true; |
| 182 |
queue.clear(); |
| 183 |
RxJavaPlugins.onError(ex); |
| 184 |
return EmptyDisposable.INSTANCE; |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
return disposable; |
| 189 |
} |
| 190 |
|
| 191 |
@NonNull |
| 192 |
@Override |
| 193 |
public Disposable schedule(@NonNull Runnable run, long delay, @NonNull TimeUnit unit) { |
| 194 |
if (delay <= 0) { |
| 195 |
return schedule(run); |
| 196 |
} |
| 197 |
if (disposed) { |
| 198 |
return EmptyDisposable.INSTANCE; |
| 199 |
} |
| 200 |
|
| 201 |
SequentialDisposable first = new SequentialDisposable(); |
| 202 |
|
| 203 |
final SequentialDisposable mar = new SequentialDisposable(first); |
| 204 |
|
| 205 |
final Runnable decoratedRun = RxJavaPlugins.onSchedule(run); |
| 206 |
|
| 207 |
ScheduledRunnable sr = new ScheduledRunnable(new SequentialDispose(mar, decoratedRun), tasks); |
| 208 |
tasks.add(sr); |
| 209 |
|
| 210 |
if (executor instanceof ScheduledExecutorService) { |
| 211 |
try { |
| 212 |
Future<?> f = ((ScheduledExecutorService)executor).schedule((Callable<Object>)sr, delay, unit); |
| 213 |
sr.setFuture(f); |
| 214 |
} catch (RejectedExecutionException ex) { |
| 215 |
disposed = true; |
| 216 |
RxJavaPlugins.onError(ex); |
| 217 |
return EmptyDisposable.INSTANCE; |
| 218 |
} |
| 219 |
} else { |
| 220 |
final Disposable d = SingleHolder.HELPER.scheduleDirect(sr, delay, unit); |
| 221 |
sr.setFuture(new DisposeOnCancel(d)); |
| 222 |
} |
| 223 |
|
| 224 |
first.replace(sr); |
| 225 |
|
| 226 |
return mar; |
| 227 |
} |
| 228 |
|
| 229 |
@Override |
| 230 |
public void dispose() { |
| 231 |
if (!disposed) { |
| 232 |
disposed = true; |
| 233 |
tasks.dispose(); |
| 234 |
if (wip.getAndIncrement() == 0) { |
| 235 |
queue.clear(); |
| 236 |
} |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
@Override |
| 241 |
public boolean isDisposed() { |
| 242 |
return disposed; |
| 243 |
} |
| 244 |
|
| 245 |
@Override |
| 246 |
public void run() { |
| 247 |
if (fair) { |
| 248 |
runFair(); |
| 249 |
} else { |
| 250 |
runEager(); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
void runFair() { |
| 255 |
final MpscLinkedQueue<Runnable> q = queue; |
| 256 |
if (disposed) { |
| 257 |
q.clear(); |
| 258 |
return; |
| 259 |
} |
| 260 |
|
| 261 |
Runnable run = q.poll(); |
| 262 |
run.run(); // never null because of offer + increment happens first |
| 263 |
|
| 264 |
if (disposed) { |
| 265 |
q.clear(); |
| 266 |
return; |
| 267 |
} |
| 268 |
|
| 269 |
if (wip.decrementAndGet() != 0) { |
| 270 |
executor.execute(this); |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
void runEager() { |
| 275 |
int missed = 1; |
| 276 |
final MpscLinkedQueue<Runnable> q = queue; |
| 277 |
for (;;) { |
| 278 |
|
| 279 |
if (disposed) { |
| 280 |
q.clear(); |
| 281 |
return; |
| 282 |
} |
| 283 |
|
| 284 |
for (;;) { |
| 285 |
Runnable run = q.poll(); |
| 286 |
if (run == null) { |
| 287 |
break; |
| 288 |
} |
| 289 |
run.run(); |
| 290 |
|
| 291 |
if (disposed) { |
| 292 |
q.clear(); |
| 293 |
return; |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
if (disposed) { |
| 298 |
q.clear(); |
| 299 |
return; |
| 300 |
} |
| 301 |
|
| 302 |
missed = wip.addAndGet(-missed); |
| 303 |
if (missed == 0) { |
| 304 |
break; |
| 305 |
} |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
static final class BooleanRunnable extends AtomicBoolean implements Runnable, Disposable { |
| 310 |
|
| 311 |
private static final long serialVersionUID = -2421395018820541164L; |
| 312 |
|
| 313 |
final Runnable actual; |
| 314 |
BooleanRunnable(Runnable actual) { |
| 315 |
this.actual = actual; |
| 316 |
} |
| 317 |
|
| 318 |
@Override |
| 319 |
public void run() { |
| 320 |
if (get()) { |
| 321 |
return; |
| 322 |
} |
| 323 |
try { |
| 324 |
actual.run(); |
| 325 |
} catch (Throwable ex) { |
| 326 |
// Exceptions.throwIfFatal(ex); nowhere to go |
| 327 |
RxJavaPlugins.onError(ex); |
| 328 |
throw ex; |
| 329 |
} finally { |
| 330 |
lazySet(true); |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
@Override |
| 335 |
public void dispose() { |
| 336 |
lazySet(true); |
| 337 |
} |
| 338 |
|
| 339 |
@Override |
| 340 |
public boolean isDisposed() { |
| 341 |
return get(); |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
final class SequentialDispose implements Runnable { |
| 346 |
private final SequentialDisposable mar; |
| 347 |
private final Runnable decoratedRun; |
| 348 |
|
| 349 |
SequentialDispose(SequentialDisposable mar, Runnable decoratedRun) { |
| 350 |
this.mar = mar; |
| 351 |
this.decoratedRun = decoratedRun; |
| 352 |
} |
| 353 |
|
| 354 |
@Override |
| 355 |
public void run() { |
| 356 |
mar.replace(schedule(decoratedRun)); |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
/** |
| 361 |
* Wrapper for a {@link Runnable} with additional logic for handling interruption on |
| 362 |
* a shared thread, similar to how Java Executors do it. |
| 363 |
*/ |
| 364 |
static final class InterruptibleRunnable extends AtomicInteger implements Runnable, Disposable { |
| 365 |
|
| 366 |
private static final long serialVersionUID = -3603436687413320876L; |
| 367 |
|
| 368 |
final Runnable run; |
| 369 |
|
| 370 |
final DisposableContainer tasks; |
| 371 |
|
| 372 |
volatile Thread thread; |
| 373 |
|
| 374 |
static final int READY = 0; |
| 375 |
|
| 376 |
static final int RUNNING = 1; |
| 377 |
|
| 378 |
static final int FINISHED = 2; |
| 379 |
|
| 380 |
static final int INTERRUPTING = 3; |
| 381 |
|
| 382 |
static final int INTERRUPTED = 4; |
| 383 |
|
| 384 |
InterruptibleRunnable(Runnable run, DisposableContainer tasks) { |
| 385 |
this.run = run; |
| 386 |
this.tasks = tasks; |
| 387 |
} |
| 388 |
|
| 389 |
@Override |
| 390 |
public void run() { |
| 391 |
if (get() == READY) { |
| 392 |
thread = Thread.currentThread(); |
| 393 |
if (compareAndSet(READY, RUNNING)) { |
| 394 |
try { |
| 395 |
try { |
| 396 |
run.run(); |
| 397 |
} catch (Throwable ex) { |
| 398 |
// Exceptions.throwIfFatal(ex); nowhere to go |
| 399 |
RxJavaPlugins.onError(ex); |
| 400 |
throw ex; |
| 401 |
} |
| 402 |
} finally { |
| 403 |
thread = null; |
| 404 |
if (compareAndSet(RUNNING, FINISHED)) { |
| 405 |
cleanup(); |
| 406 |
} else { |
| 407 |
while (get() == INTERRUPTING) { |
| 408 |
Thread.yield(); |
| 409 |
} |
| 410 |
Thread.interrupted(); |
| 411 |
} |
| 412 |
} |
| 413 |
} else { |
| 414 |
thread = null; |
| 415 |
} |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
@Override |
| 420 |
public void dispose() { |
| 421 |
for (;;) { |
| 422 |
int state = get(); |
| 423 |
if (state >= FINISHED) { |
| 424 |
break; |
| 425 |
} else if (state == READY) { |
| 426 |
if (compareAndSet(READY, INTERRUPTED)) { |
| 427 |
cleanup(); |
| 428 |
break; |
| 429 |
} |
| 430 |
} else { |
| 431 |
if (compareAndSet(RUNNING, INTERRUPTING)) { |
| 432 |
Thread t = thread; |
| 433 |
if (t != null) { |
| 434 |
t.interrupt(); |
| 435 |
thread = null; |
| 436 |
} |
| 437 |
set(INTERRUPTED); |
| 438 |
cleanup(); |
| 439 |
break; |
| 440 |
} |
| 441 |
} |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
void cleanup() { |
| 446 |
if (tasks != null) { |
| 447 |
tasks.delete(this); |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
@Override |
| 452 |
public boolean isDisposed() { |
| 453 |
return get() >= FINISHED; |
| 454 |
} |
| 455 |
} |
| 456 |
} |
| 457 |
|
| 458 |
static final class DelayedRunnable extends AtomicReference<Runnable> |
| 459 |
implements Runnable, Disposable, SchedulerRunnableIntrospection { |
| 460 |
|
| 461 |
private static final long serialVersionUID = -4101336210206799084L; |
| 462 |
|
| 463 |
final SequentialDisposable timed; |
| 464 |
|
| 465 |
final SequentialDisposable direct; |
| 466 |
|
| 467 |
DelayedRunnable(Runnable run) { |
| 468 |
super(run); |
| 469 |
this.timed = new SequentialDisposable(); |
| 470 |
this.direct = new SequentialDisposable(); |
| 471 |
} |
| 472 |
|
| 473 |
@Override |
| 474 |
public void run() { |
| 475 |
Runnable r = get(); |
| 476 |
if (r != null) { |
| 477 |
try { |
| 478 |
try { |
| 479 |
r.run(); |
| 480 |
} finally { |
| 481 |
lazySet(null); |
| 482 |
timed.lazySet(DisposableHelper.DISPOSED); |
| 483 |
direct.lazySet(DisposableHelper.DISPOSED); |
| 484 |
} |
| 485 |
} catch (Throwable ex) { |
| 486 |
// Exceptions.throwIfFatal(ex); nowhere to go |
| 487 |
RxJavaPlugins.onError(ex); |
| 488 |
throw ex; |
| 489 |
} |
| 490 |
} |
| 491 |
} |
| 492 |
|
| 493 |
@Override |
| 494 |
public boolean isDisposed() { |
| 495 |
return get() == null; |
| 496 |
} |
| 497 |
|
| 498 |
@Override |
| 499 |
public void dispose() { |
| 500 |
if (getAndSet(null) != null) { |
| 501 |
timed.dispose(); |
| 502 |
direct.dispose(); |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
@Override |
| 507 |
public Runnable getWrappedRunnable() { |
| 508 |
Runnable r = get(); |
| 509 |
return r != null ? r : Functions.EMPTY_RUNNABLE; |
| 510 |
} |
| 511 |
} |
| 512 |
|
| 513 |
final class DelayedDispose implements Runnable { |
| 514 |
private final DelayedRunnable dr; |
| 515 |
|
| 516 |
DelayedDispose(DelayedRunnable dr) { |
| 517 |
this.dr = dr; |
| 518 |
} |
| 519 |
|
| 520 |
@Override |
| 521 |
public void run() { |
| 522 |
dr.direct.replace(scheduleDirect(dr)); |
| 523 |
} |
| 524 |
} |
| 525 |
} |