| 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.disposables.Disposable; |
| 20 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 21 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 22 |
|
| 23 |
/** |
| 24 |
* Wrapper for a regular task that gets immediately rescheduled when the task completed. |
| 25 |
*/ |
| 26 |
final class InstantPeriodicTask implements Callable<Void>, Disposable { |
| 27 |
|
| 28 |
final Runnable task; |
| 29 |
|
| 30 |
final AtomicReference<Future<?>> rest; |
| 31 |
|
| 32 |
final AtomicReference<Future<?>> first; |
| 33 |
|
| 34 |
final ExecutorService executor; |
| 35 |
|
| 36 |
Thread runner; |
| 37 |
|
| 38 |
static final FutureTask<Void> CANCELLED = new FutureTask<>(Functions.EMPTY_RUNNABLE, null); |
| 39 |
|
| 40 |
InstantPeriodicTask(Runnable task, ExecutorService executor) { |
| 41 |
super(); |
| 42 |
this.task = task; |
| 43 |
this.first = new AtomicReference<>(); |
| 44 |
this.rest = new AtomicReference<>(); |
| 45 |
this.executor = executor; |
| 46 |
} |
| 47 |
|
| 48 |
@Override |
| 49 |
public Void call() { |
| 50 |
runner = Thread.currentThread(); |
| 51 |
try { |
| 52 |
task.run(); |
| 53 |
runner = null; |
| 54 |
setRest(executor.submit(this)); |
| 55 |
} catch (Throwable ex) { |
| 56 |
// Exceptions.throwIfFatal(ex); nowhere to go |
| 57 |
runner = null; |
| 58 |
RxJavaPlugins.onError(ex); |
| 59 |
throw ex; |
| 60 |
} |
| 61 |
return null; |
| 62 |
} |
| 63 |
|
| 64 |
@Override |
| 65 |
public void dispose() { |
| 66 |
Future<?> current = first.getAndSet(CANCELLED); |
| 67 |
if (current != null && current != CANCELLED) { |
| 68 |
current.cancel(runner != Thread.currentThread()); |
| 69 |
} |
| 70 |
current = rest.getAndSet(CANCELLED); |
| 71 |
if (current != null && current != CANCELLED) { |
| 72 |
current.cancel(runner != Thread.currentThread()); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
@Override |
| 77 |
public boolean isDisposed() { |
| 78 |
return first.get() == CANCELLED; |
| 79 |
} |
| 80 |
|
| 81 |
void setFirst(Future<?> f) { |
| 82 |
for (;;) { |
| 83 |
Future<?> current = first.get(); |
| 84 |
if (current == CANCELLED) { |
| 85 |
f.cancel(runner != Thread.currentThread()); |
| 86 |
return; |
| 87 |
} |
| 88 |
if (first.compareAndSet(current, f)) { |
| 89 |
return; |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
void setRest(Future<?> f) { |
| 95 |
for (;;) { |
| 96 |
Future<?> current = rest.get(); |
| 97 |
if (current == CANCELLED) { |
| 98 |
f.cancel(runner != Thread.currentThread()); |
| 99 |
return; |
| 100 |
} |
| 101 |
if (rest.compareAndSet(current, f)) { |
| 102 |
return; |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
} |