package kotlinx.coroutines.q2;

import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import kotlinx.coroutines.k1;

/* JADX INFO: compiled from: Dispatcher.kt */
/* JADX INFO: loaded from: classes.dex */
final class e extends k1 implements j, Executor {

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    private static final /* synthetic */ AtomicIntegerFieldUpdater f1147k = AtomicIntegerFieldUpdater.newUpdater(e.class, "inFlightTasks");

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private final c f1148f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private final int f1149g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    private final String f1150h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    private final int f1151i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    private final ConcurrentLinkedQueue<Runnable> f1152j = new ConcurrentLinkedQueue<>();
    private volatile /* synthetic */ int inFlightTasks = 0;

    public e(c cVar, int i2, String str, int i3) {
        this.f1148f = cVar;
        this.f1149g = i2;
        this.f1150h = str;
        this.f1151i = i3;
    }

    private final void K(Runnable runnable, boolean z) {
        while (f1147k.incrementAndGet(this) > this.f1149g) {
            this.f1152j.add(runnable);
            if (f1147k.decrementAndGet(this) >= this.f1149g || (runnable = this.f1152j.poll()) == null) {
                return;
            }
        }
        this.f1148f.N(runnable, this, z);
    }

    @Override // kotlinx.coroutines.q2.j
    public int E() {
        return this.f1151i;
    }

    @Override // kotlinx.coroutines.i0
    public void H(h.u.g gVar, Runnable runnable) {
        K(runnable, false);
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public void close() {
        throw new IllegalStateException("Close cannot be invoked on LimitingBlockingDispatcher".toString());
    }

    @Override // java.util.concurrent.Executor
    public void execute(Runnable runnable) {
        K(runnable, false);
    }

    @Override // kotlinx.coroutines.q2.j
    public void p() {
        Runnable runnablePoll = this.f1152j.poll();
        if (runnablePoll != null) {
            this.f1148f.N(runnablePoll, this, true);
            return;
        }
        f1147k.decrementAndGet(this);
        Runnable runnablePoll2 = this.f1152j.poll();
        if (runnablePoll2 == null) {
            return;
        }
        K(runnablePoll2, true);
    }

    @Override // kotlinx.coroutines.i0
    public String toString() {
        String str = this.f1150h;
        if (str != null) {
            return str;
        }
        return super.toString() + "[dispatcher = " + this.f1148f + ']';
    }
}
