101 lines
2.9 KiB
Java
101 lines
2.9 KiB
Java
package androidx.lifecycle;
|
|
|
|
import android.annotation.SuppressLint;
|
|
import androidx.annotation.AnyThread;
|
|
import androidx.annotation.MainThread;
|
|
import d0.z.d.m;
|
|
import java.util.ArrayDeque;
|
|
import java.util.Queue;
|
|
import kotlin.coroutines.CoroutineContext;
|
|
import kotlinx.coroutines.CoroutineDispatcher;
|
|
import s.a.a.n;
|
|
import s.a.k0;
|
|
import s.a.l1;
|
|
/* compiled from: DispatchQueue.kt */
|
|
public final class DispatchQueue {
|
|
private boolean finished;
|
|
private boolean isDraining;
|
|
private boolean paused = true;
|
|
private final Queue<Runnable> queue = new ArrayDeque();
|
|
|
|
public static final /* synthetic */ void access$enqueue(DispatchQueue dispatchQueue, Runnable runnable) {
|
|
dispatchQueue.enqueue(runnable);
|
|
}
|
|
|
|
@MainThread
|
|
private final void enqueue(Runnable runnable) {
|
|
if (this.queue.offer(runnable)) {
|
|
drainQueue();
|
|
return;
|
|
}
|
|
throw new IllegalStateException("cannot enqueue any more runnables".toString());
|
|
}
|
|
|
|
@MainThread
|
|
public final boolean canRun() {
|
|
return this.finished || !this.paused;
|
|
}
|
|
|
|
@AnyThread
|
|
@SuppressLint({"WrongThread"})
|
|
public final void dispatchAndEnqueue(CoroutineContext coroutineContext, Runnable runnable) {
|
|
m.checkNotNullParameter(coroutineContext, "context");
|
|
m.checkNotNullParameter(runnable, "runnable");
|
|
CoroutineDispatcher coroutineDispatcher = k0.a;
|
|
l1 H = n.b.H();
|
|
if (H.isDispatchNeeded(coroutineContext) || canRun()) {
|
|
H.dispatch(coroutineContext, new DispatchQueue$dispatchAndEnqueue$$inlined$with$lambda$1(this, coroutineContext, runnable));
|
|
} else {
|
|
enqueue(runnable);
|
|
}
|
|
}
|
|
|
|
@MainThread
|
|
public final void drainQueue() {
|
|
if (!this.isDraining) {
|
|
boolean z2 = false;
|
|
z2 = true;
|
|
try {
|
|
while (true) {
|
|
if (!(!this.queue.isEmpty())) {
|
|
break;
|
|
} else if (!canRun()) {
|
|
break;
|
|
} else {
|
|
Runnable poll = this.queue.poll();
|
|
if (poll != null) {
|
|
poll.run();
|
|
}
|
|
}
|
|
}
|
|
this.isDraining = false;
|
|
} finally {
|
|
this.isDraining = z2;
|
|
}
|
|
}
|
|
}
|
|
|
|
@MainThread
|
|
public final void finish() {
|
|
this.finished = true;
|
|
drainQueue();
|
|
}
|
|
|
|
@MainThread
|
|
public final void pause() {
|
|
this.paused = true;
|
|
}
|
|
|
|
@MainThread
|
|
public final void resume() {
|
|
if (this.paused) {
|
|
if (!this.finished) {
|
|
this.paused = false;
|
|
drainQueue();
|
|
return;
|
|
}
|
|
throw new IllegalStateException("Cannot resume a finished dispatcher".toString());
|
|
}
|
|
}
|
|
}
|