package kotlinx.coroutines.internal;

/* JADX INFO: compiled from: LimitedDispatcher.kt */
/* JADX INFO: loaded from: classes.dex */
public final class k extends d2.u implements Runnable, d2.g0 {

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private final d2.u f5213f;

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

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    private final /* synthetic */ d2.g0 f5215h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    private final p<Runnable> f5216i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    private final Object f5217j;
    private volatile int runningWorkers;

    /* JADX WARN: Multi-variable type inference failed */
    public k(d2.u uVar, int i3) {
        this.f5213f = uVar;
        this.f5214g = i3;
        d2.g0 g0Var = uVar instanceof d2.g0 ? (d2.g0) uVar : null;
        this.f5215h = g0Var == null ? d2.f0.a() : g0Var;
        this.f5216i = new p<>(false);
        this.f5217j = new Object();
    }

    private final boolean j(Runnable runnable) {
        this.f5216i.a(runnable);
        return this.runningWorkers >= this.f5214g;
    }

    private final boolean n() {
        synchronized (this.f5217j) {
            if (this.runningWorkers >= this.f5214g) {
                return false;
            }
            this.runningWorkers++;
            return true;
        }
    }

    @Override // d2.u
    public void d(n1.f fVar, Runnable runnable) {
        if (!j(runnable) && n()) {
            this.f5213f.d(this, this);
        }
    }

    /* JADX WARN: Code restructure failed: missing block: B:16:0x002a, code lost:
    
        r1 = r4.f5217j;
     */
    /* JADX WARN: Code restructure failed: missing block: B:17:0x002c, code lost:
    
        monitor-enter(r1);
     */
    /* JADX WARN: Code restructure failed: missing block: B:18:0x002d, code lost:
    
        r4.runningWorkers--;
     */
    /* JADX WARN: Code restructure failed: missing block: B:19:0x0039, code lost:
    
        if (r4.f5216i.c() != 0) goto L22;
     */
    /* JADX WARN: Code restructure failed: missing block: B:20:0x003b, code lost:
    
        monitor-exit(r1);
     */
    /* JADX WARN: Code restructure failed: missing block: B:21:0x003c, code lost:
    
        return;
     */
    /* JADX WARN: Code restructure failed: missing block: B:22:0x003d, code lost:
    
        r4.runningWorkers++;
        r2 = k1.o.f5171a;
     */
    @Override // java.lang.Runnable
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public void run() {
        /*
            r4 = this;
            r0 = 0
        L1:
            r1 = r0
        L2:
            kotlinx.coroutines.internal.p<java.lang.Runnable> r2 = r4.f5216i
            java.lang.Object r2 = r2.d()
            java.lang.Runnable r2 = (java.lang.Runnable) r2
            if (r2 == 0) goto L2a
            r2.run()     // Catch: java.lang.Throwable -> L10
            goto L16
        L10:
            r2 = move-exception
            n1.g r3 = n1.g.f5679e
            d2.y.a(r3, r2)
        L16:
            int r1 = r1 + 1
            r2 = 16
            if (r1 < r2) goto L2
            d2.u r2 = r4.f5213f
            boolean r2 = r2.g(r4)
            if (r2 == 0) goto L2
            d2.u r0 = r4.f5213f
            r0.d(r4, r4)
            return
        L2a:
            java.lang.Object r1 = r4.f5217j
            monitor-enter(r1)
            int r2 = r4.runningWorkers     // Catch: java.lang.Throwable -> L47
            int r2 = r2 + (-1)
            r4.runningWorkers = r2     // Catch: java.lang.Throwable -> L47
            kotlinx.coroutines.internal.p<java.lang.Runnable> r2 = r4.f5216i     // Catch: java.lang.Throwable -> L47
            int r2 = r2.c()     // Catch: java.lang.Throwable -> L47
            if (r2 != 0) goto L3d
            monitor-exit(r1)
            return
        L3d:
            int r2 = r4.runningWorkers     // Catch: java.lang.Throwable -> L47
            int r2 = r2 + 1
            r4.runningWorkers = r2     // Catch: java.lang.Throwable -> L47
            k1.o r2 = k1.o.f5171a     // Catch: java.lang.Throwable -> L47
            monitor-exit(r1)
            goto L1
        L47:
            r4 = move-exception
            monitor-exit(r1)
            throw r4
        */
        throw new UnsupportedOperationException("Method not decompiled: kotlinx.coroutines.internal.k.run():void");
    }
}
