| 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.AtomicInteger; |
| 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.EmptyDisposable; |
| 23 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 24 |
|
| 25 |
/** |
| 26 |
* Schedules work on the current thread but does not execute immediately. Work is put in a queue and executed |
| 27 |
* after the current unit of work is completed. |
| 28 |
*/ |
| 29 |
public final class TrampolineScheduler extends Scheduler { |
| 30 |
private static final TrampolineScheduler INSTANCE = new TrampolineScheduler(); |
| 31 |
|
| 32 |
public static TrampolineScheduler instance() { |
| 33 |
return INSTANCE; |
| 34 |
} |
| 35 |
|
| 36 |
@NonNull |
| 37 |
@Override |
| 38 |
public Worker createWorker() { |
| 39 |
return new TrampolineWorker(); |
| 40 |
} |
| 41 |
|
| 42 |
/* package accessible for unit tests */TrampolineScheduler() { |
| 43 |
} |
| 44 |
|
| 45 |
@NonNull |
| 46 |
@Override |
| 47 |
public Disposable scheduleDirect(@NonNull Runnable run) { |
| 48 |
RxJavaPlugins.onSchedule(run).run(); |
| 49 |
return EmptyDisposable.INSTANCE; |
| 50 |
} |
| 51 |
|
| 52 |
@NonNull |
| 53 |
@Override |
| 54 |
public Disposable scheduleDirect(@NonNull Runnable run, long delay, TimeUnit unit) { |
| 55 |
try { |
| 56 |
unit.sleep(delay); |
| 57 |
RxJavaPlugins.onSchedule(run).run(); |
| 58 |
} catch (InterruptedException ex) { |
| 59 |
Thread.currentThread().interrupt(); |
| 60 |
RxJavaPlugins.onError(ex); |
| 61 |
} |
| 62 |
return EmptyDisposable.INSTANCE; |
| 63 |
} |
| 64 |
|
| 65 |
static final class TrampolineWorker extends Scheduler.Worker implements Disposable { |
| 66 |
final PriorityBlockingQueue<TimedRunnable> queue = new PriorityBlockingQueue<>(); |
| 67 |
|
| 68 |
private final AtomicInteger wip = new AtomicInteger(); |
| 69 |
|
| 70 |
final AtomicInteger counter = new AtomicInteger(); |
| 71 |
|
| 72 |
volatile boolean disposed; |
| 73 |
|
| 74 |
@NonNull |
| 75 |
@Override |
| 76 |
public Disposable schedule(@NonNull Runnable action) { |
| 77 |
return enqueue(action, now(TimeUnit.MILLISECONDS)); |
| 78 |
} |
| 79 |
|
| 80 |
@NonNull |
| 81 |
@Override |
| 82 |
public Disposable schedule(@NonNull Runnable action, long delayTime, @NonNull TimeUnit unit) { |
| 83 |
long execTime = now(TimeUnit.MILLISECONDS) + unit.toMillis(delayTime); |
| 84 |
|
| 85 |
return enqueue(new SleepingRunnable(action, this, execTime), execTime); |
| 86 |
} |
| 87 |
|
| 88 |
Disposable enqueue(Runnable action, long execTime) { |
| 89 |
if (disposed) { |
| 90 |
return EmptyDisposable.INSTANCE; |
| 91 |
} |
| 92 |
final TimedRunnable timedRunnable = new TimedRunnable(action, execTime, counter.incrementAndGet()); |
| 93 |
queue.add(timedRunnable); |
| 94 |
|
| 95 |
if (wip.getAndIncrement() == 0) { |
| 96 |
int missed = 1; |
| 97 |
for (;;) { |
| 98 |
for (;;) { |
| 99 |
if (disposed) { |
| 100 |
queue.clear(); |
| 101 |
return EmptyDisposable.INSTANCE; |
| 102 |
} |
| 103 |
final TimedRunnable polled = queue.poll(); |
| 104 |
if (polled == null) { |
| 105 |
break; |
| 106 |
} |
| 107 |
if (!polled.disposed) { |
| 108 |
polled.run.run(); |
| 109 |
} |
| 110 |
} |
| 111 |
missed = wip.addAndGet(-missed); |
| 112 |
if (missed == 0) { |
| 113 |
break; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
return EmptyDisposable.INSTANCE; |
| 118 |
} else { |
| 119 |
// queue wasn't empty, a parent is already processing so we just add to the end of the queue |
| 120 |
return Disposable.fromRunnable(new AppendToQueueTask(timedRunnable)); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
@Override |
| 125 |
public void dispose() { |
| 126 |
disposed = true; |
| 127 |
} |
| 128 |
|
| 129 |
@Override |
| 130 |
public boolean isDisposed() { |
| 131 |
return disposed; |
| 132 |
} |
| 133 |
|
| 134 |
final class AppendToQueueTask implements Runnable { |
| 135 |
final TimedRunnable timedRunnable; |
| 136 |
|
| 137 |
AppendToQueueTask(TimedRunnable timedRunnable) { |
| 138 |
this.timedRunnable = timedRunnable; |
| 139 |
} |
| 140 |
|
| 141 |
@Override |
| 142 |
public void run() { |
| 143 |
timedRunnable.disposed = true; |
| 144 |
queue.remove(timedRunnable); |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
static final class TimedRunnable implements Comparable<TimedRunnable> { |
| 150 |
final Runnable run; |
| 151 |
final long execTime; |
| 152 |
final int count; // In case if time between enqueueing took less than 1ms |
| 153 |
|
| 154 |
volatile boolean disposed; |
| 155 |
|
| 156 |
TimedRunnable(Runnable run, Long execTime, int count) { |
| 157 |
this.run = run; |
| 158 |
this.execTime = execTime; |
| 159 |
this.count = count; |
| 160 |
} |
| 161 |
|
| 162 |
@Override |
| 163 |
public int compareTo(TimedRunnable that) { |
| 164 |
int result = Long.compare(execTime, that.execTime); |
| 165 |
if (result == 0) { |
| 166 |
return Integer.compare(count, that.count); |
| 167 |
} |
| 168 |
return result; |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
static final class SleepingRunnable implements Runnable { |
| 173 |
private final Runnable run; |
| 174 |
private final TrampolineWorker worker; |
| 175 |
private final long execTime; |
| 176 |
|
| 177 |
SleepingRunnable(Runnable run, TrampolineWorker worker, long execTime) { |
| 178 |
this.run = run; |
| 179 |
this.worker = worker; |
| 180 |
this.execTime = execTime; |
| 181 |
} |
| 182 |
|
| 183 |
@Override |
| 184 |
public void run() { |
| 185 |
if (!worker.disposed) { |
| 186 |
long t = worker.now(TimeUnit.MILLISECONDS); |
| 187 |
if (execTime > t) { |
| 188 |
long delay = execTime - t; |
| 189 |
try { |
| 190 |
Thread.sleep(delay); |
| 191 |
} catch (InterruptedException e) { |
| 192 |
Thread.currentThread().interrupt(); |
| 193 |
RxJavaPlugins.onError(e); |
| 194 |
return; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
if (!worker.disposed) { |
| 199 |
run.run(); |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
} |