| 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.AtomicReferenceArray; |
| 18 |
|
| 19 |
import io.reactivex.rxjava3.disposables.*; |
| 20 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 21 |
|
| 22 |
public final class ScheduledRunnable extends AtomicReferenceArray<Object> |
| 23 |
implements Runnable, Callable<Object>, Disposable { |
| 24 |
|
| 25 |
private static final long serialVersionUID = -6120223772001106981L; |
| 26 |
final Runnable actual; |
| 27 |
|
| 28 |
/** Indicates that the parent tracking this task has been notified about its completion. */ |
| 29 |
static final Object PARENT_DISPOSED = new Object(); |
| 30 |
/** Indicates the dispose() was called from within the run/call method. */ |
| 31 |
static final Object SYNC_DISPOSED = new Object(); |
| 32 |
/** Indicates the dispose() was called from another thread. */ |
| 33 |
static final Object ASYNC_DISPOSED = new Object(); |
| 34 |
|
| 35 |
static final Object DONE = new Object(); |
| 36 |
|
| 37 |
static final int PARENT_INDEX = 0; |
| 38 |
static final int FUTURE_INDEX = 1; |
| 39 |
static final int THREAD_INDEX = 2; |
| 40 |
|
| 41 |
/** |
| 42 |
* Creates a ScheduledRunnable by wrapping the given action and setting |
| 43 |
* up the optional parent. |
| 44 |
* @param actual the runnable to wrap, not-null (not verified) |
| 45 |
* @param parent the parent tracking container or null if none |
| 46 |
*/ |
| 47 |
public ScheduledRunnable(Runnable actual, DisposableContainer parent) { |
| 48 |
super(3); |
| 49 |
this.actual = actual; |
| 50 |
this.lazySet(0, parent); |
| 51 |
} |
| 52 |
|
| 53 |
@Override |
| 54 |
public Object call() { |
| 55 |
// Being Callable saves an allocation in ThreadPoolExecutor |
| 56 |
run(); |
| 57 |
return null; |
| 58 |
} |
| 59 |
|
| 60 |
@Override |
| 61 |
public void run() { |
| 62 |
lazySet(THREAD_INDEX, Thread.currentThread()); |
| 63 |
try { |
| 64 |
try { |
| 65 |
actual.run(); |
| 66 |
} catch (Throwable e) { |
| 67 |
// Exceptions.throwIfFatal(e); nowhere to go |
| 68 |
RxJavaPlugins.onError(e); |
| 69 |
throw e; |
| 70 |
} |
| 71 |
} finally { |
| 72 |
Object o = get(PARENT_INDEX); |
| 73 |
if (o != PARENT_DISPOSED && compareAndSet(PARENT_INDEX, o, DONE) && o != null) { |
| 74 |
((DisposableContainer)o).delete(this); |
| 75 |
} |
| 76 |
|
| 77 |
for (;;) { |
| 78 |
o = get(FUTURE_INDEX); |
| 79 |
if (o == SYNC_DISPOSED || o == ASYNC_DISPOSED || compareAndSet(FUTURE_INDEX, o, DONE)) { |
| 80 |
break; |
| 81 |
} |
| 82 |
} |
| 83 |
lazySet(THREAD_INDEX, null); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
public void setFuture(Future<?> f) { |
| 88 |
for (;;) { |
| 89 |
Object o = get(FUTURE_INDEX); |
| 90 |
if (o == DONE) { |
| 91 |
return; |
| 92 |
} |
| 93 |
if (o == SYNC_DISPOSED) { |
| 94 |
f.cancel(false); |
| 95 |
return; |
| 96 |
} |
| 97 |
if (o == ASYNC_DISPOSED) { |
| 98 |
f.cancel(true); |
| 99 |
return; |
| 100 |
} |
| 101 |
if (compareAndSet(FUTURE_INDEX, o, f)) { |
| 102 |
return; |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
@Override |
| 108 |
public void dispose() { |
| 109 |
for (;;) { |
| 110 |
Object o = get(FUTURE_INDEX); |
| 111 |
if (o == DONE || o == SYNC_DISPOSED || o == ASYNC_DISPOSED) { |
| 112 |
break; |
| 113 |
} |
| 114 |
boolean async = get(THREAD_INDEX) != Thread.currentThread(); |
| 115 |
if (compareAndSet(FUTURE_INDEX, o, async ? ASYNC_DISPOSED : SYNC_DISPOSED)) { |
| 116 |
if (o != null) { |
| 117 |
((Future<?>)o).cancel(async); |
| 118 |
} |
| 119 |
break; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
for (;;) { |
| 124 |
Object o = get(PARENT_INDEX); |
| 125 |
if (o == DONE || o == PARENT_DISPOSED || o == null) { |
| 126 |
return; |
| 127 |
} |
| 128 |
if (compareAndSet(PARENT_INDEX, o, PARENT_DISPOSED)) { |
| 129 |
((DisposableContainer)o).delete(this); |
| 130 |
return; |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
@Override |
| 136 |
public boolean isDisposed() { |
| 137 |
Object o = get(PARENT_INDEX); |
| 138 |
return o == PARENT_DISPOSED || o == DONE; |
| 139 |
} |
| 140 |
|
| 141 |
@Override |
| 142 |
public String toString() { |
| 143 |
String state; |
| 144 |
Object o = get(FUTURE_INDEX); |
| 145 |
if (o == DONE) { |
| 146 |
state = "Finished"; |
| 147 |
} else if (o == SYNC_DISPOSED) { |
| 148 |
state = "Disposed(Sync)"; |
| 149 |
} else if (o == ASYNC_DISPOSED) { |
| 150 |
state = "Disposed(Async)"; |
| 151 |
} else { |
| 152 |
o = get(THREAD_INDEX); |
| 153 |
if (o == null) { |
| 154 |
state = "Waiting"; |
| 155 |
} else { |
| 156 |
state = "Running on " + o; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
return getClass().getSimpleName() + "[" + state + "]"; |
| 161 |
} |
| 162 |
} |