Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/schedulers/AbstractDirectTask.java

raw
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.schedulers.SchedulerRunnableIntrospection;
22
23 /**
24 * Base functionality for direct tasks that manage a runnable and cancellation/completion.
25 * @since 2.0.8
26 */
27 abstract class AbstractDirectTask
28 extends AtomicReference<Future<?>>
29 implements Disposable, SchedulerRunnableIntrospection {
30
31 private static final long serialVersionUID = 1811839108042568751L;
32
33 protected final Runnable runnable;
34
35 protected final boolean interruptOnCancel;
36
37 protected Thread runner;
38
39 protected static final FutureTask<Void> FINISHED = new FutureTask<>(Functions.EMPTY_RUNNABLE, null);
40
41 protected static final FutureTask<Void> DISPOSED = new FutureTask<>(Functions.EMPTY_RUNNABLE, null);
42
43 AbstractDirectTask(Runnable runnable, boolean interruptOnCancel) {
44 this.runnable = runnable;
45 this.interruptOnCancel = interruptOnCancel;
46 }
47
48 @Override
49 public final void dispose() {
50 Future<?> f = get();
51 if (f != FINISHED && f != DISPOSED) {
52 if (compareAndSet(f, DISPOSED)) {
53 if (f != null) {
54 cancelFuture(f);
55 }
56 }
57 }
58 }
59
60 @Override
61 public final boolean isDisposed() {
62 Future<?> f = get();
63 return f == FINISHED || f == DISPOSED;
64 }
65
66 public final void setFuture(Future<?> future) {
67 for (;;) {
68 Future<?> f = get();
69 if (f == FINISHED) {
70 break;
71 }
72 if (f == DISPOSED) {
73 cancelFuture(future);
74 break;
75 }
76 if (compareAndSet(f, future)) {
77 break;
78 }
79 }
80 }
81
82 private void cancelFuture(Future<?> future) {
83 if (runner == Thread.currentThread()) {
84 future.cancel(false);
85 } else {
86 future.cancel(interruptOnCancel);
87 }
88 }
89
90 @Override
91 public Runnable getWrappedRunnable() {
92 return runnable;
93 }
94
95 @Override
96 public String toString() {
97 String status;
98 Future<?> f = get();
99 if (f == FINISHED) {
100 status = "Finished";
101 } else if (f == DISPOSED) {
102 status = "Disposed";
103 } else {
104 Thread r = runner;
105 if (r != null) {
106 status = "Running on " + runner;
107 } else {
108 status = "Waiting";
109 }
110 }
111
112 return getClass().getSimpleName() + "[" + status + "]";
113 }
114 }