discord-jadx/app/src/main/java/s/a/y1/a.java
2021-06-27 22:44:35 +02:00

639 lines
23 KiB
Java

package s.a.y1;
import c.c.a.y.b;
import d0.c0.c;
import d0.d0.f;
import d0.z.d.m;
import java.io.Closeable;
import java.util.ArrayList;
import java.util.Objects;
import java.util.concurrent.Executor;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicLongFieldUpdater;
import java.util.concurrent.atomic.AtomicReferenceArray;
import java.util.concurrent.locks.LockSupport;
import s.a.a.s;
/* compiled from: CoroutineScheduler.kt */
public final class a implements Executor, Closeable {
public static final AtomicLongFieldUpdater i = AtomicLongFieldUpdater.newUpdater(a.class, "parkedWorkersStack");
public static final AtomicLongFieldUpdater j = AtomicLongFieldUpdater.newUpdater(a.class, "controlState");
public static final AtomicIntegerFieldUpdater k = AtomicIntegerFieldUpdater.newUpdater(a.class, "_isTerminated");
public static final s l = new s("NOT_IN_STACK");
public volatile int _isTerminated;
public volatile long controlState;
public final d m;
public final d n;
public final AtomicReferenceArray<C0343a> o;
public final int p;
public volatile long parkedWorkersStack;
public final int q;
public final long r;
/* renamed from: s reason: collision with root package name */
public final String f2873s;
/* compiled from: CoroutineScheduler.kt */
/* renamed from: s.a.y1.a$a reason: collision with other inner class name */
public final class C0343a extends Thread {
public static final AtomicIntegerFieldUpdater i = AtomicIntegerFieldUpdater.newUpdater(C0343a.class, "workerCtl");
public volatile int indexInArray;
public final m j = new m();
public int k = 4;
public long l;
public long m;
public int n = c.b.nextInt();
public volatile Object nextParkedWorker = a.l;
public boolean o;
public volatile int workerCtl = 0;
public C0343a(int i2) {
setDaemon(true);
d(i2);
}
/* JADX WARNING: Removed duplicated region for block: B:14:0x0033 */
/* JADX WARNING: Removed duplicated region for block: B:29:0x006a */
public final h a(boolean z2) {
boolean z3;
h hVar;
h c2;
h c3;
boolean z4;
boolean z5 = true;
if (this.k != 1) {
a aVar = a.this;
while (true) {
long j = aVar.controlState;
if (((int) ((9223367638808264704L & j) >> 42)) != 0) {
if (a.j.compareAndSet(aVar, j, j - 4398046511104L)) {
z4 = true;
break;
}
} else {
z4 = false;
break;
}
}
if (z4) {
this.k = 1;
} else {
z3 = false;
if (!z3) {
if (z2) {
if (b(a.this.p * 2) != 0) {
z5 = false;
}
if (z5 && (c3 = c()) != null) {
return c3;
}
h e = this.j.e();
if (e != null) {
return e;
}
if (!z5 && (c2 = c()) != null) {
return c2;
}
} else {
h c4 = c();
if (c4 != null) {
return c4;
}
}
return f(false);
}
if (z2) {
hVar = this.j.e();
if (hVar == null) {
hVar = a.this.n.d();
}
} else {
hVar = a.this.n.d();
}
return hVar != null ? hVar : f(true);
}
}
z3 = true;
if (!z3) {
}
}
public final int b(int i2) {
int i3 = this.n;
int i4 = i3 ^ (i3 << 13);
int i5 = i4 ^ (i4 >> 17);
int i6 = i5 ^ (i5 << 5);
this.n = i6;
int i7 = i2 - 1;
return (i7 & i2) == 0 ? i6 & i7 : (i6 & Integer.MAX_VALUE) % i2;
}
public final h c() {
if (b(2) == 0) {
h d = a.this.m.d();
return d != null ? d : a.this.n.d();
}
h d2 = a.this.n.d();
return d2 != null ? d2 : a.this.m.d();
}
public final void d(int i2) {
StringBuilder sb = new StringBuilder();
sb.append(a.this.f2873s);
sb.append("-worker-");
sb.append(i2 == 0 ? "TERMINATED" : String.valueOf(i2));
setName(sb.toString());
this.indexInArray = i2;
}
public final boolean e(int i2) {
int i3 = this.k;
boolean z2 = true;
if (i3 != 1) {
z2 = false;
}
if (z2) {
a.j.addAndGet(a.this, 4398046511104L);
}
if (i3 != i2) {
this.k = i2;
}
return z2;
}
public final h f(boolean z2) {
long j;
int i2 = (int) (a.this.controlState & 2097151);
if (i2 < 2) {
return null;
}
int b = b(i2);
long j2 = Long.MAX_VALUE;
for (int i3 = 0; i3 < i2; i3++) {
b++;
if (b > i2) {
b = 1;
}
C0343a aVar = a.this.o.get(b);
if (aVar != null && aVar != this) {
if (z2) {
m mVar = this.j;
m mVar2 = aVar.j;
Objects.requireNonNull(mVar);
int i4 = mVar2.consumerIndex;
int i5 = mVar2.producerIndex;
AtomicReferenceArray<h> atomicReferenceArray = mVar2.e;
while (true) {
if (i4 == i5) {
break;
}
int i6 = i4 & 127;
if (mVar2.blockingTasksInBuffer == 0) {
break;
}
h hVar = atomicReferenceArray.get(i6);
if (hVar != null) {
if ((hVar.j.s() == 1) && atomicReferenceArray.compareAndSet(i6, hVar, null)) {
m.d.decrementAndGet(mVar2);
mVar.a(hVar, false);
j = -1;
break;
}
}
i4++;
}
j = mVar.g(mVar2, true);
} else {
m mVar3 = this.j;
m mVar4 = aVar.j;
Objects.requireNonNull(mVar3);
h f = mVar4.f();
if (f != null) {
mVar3.a(f, false);
j = -1;
} else {
j = mVar3.g(mVar4, false);
}
}
if (j == -1) {
return this.j.e();
}
if (j > 0) {
j2 = Math.min(j2, j);
}
}
}
if (j2 == Long.MAX_VALUE) {
j2 = 0;
}
this.m = j2;
return null;
}
@Override // java.lang.Thread, java.lang.Runnable
public void run() {
long j;
int i2;
loop0:
while (true) {
boolean z2 = false;
while (a.this._isTerminated == 0 && this.k != 5) {
h a = a(this.o);
if (a == null) {
this.o = false;
if (this.m == 0) {
Object obj = this.nextParkedWorker;
s sVar = a.l;
if (obj != sVar) {
this.workerCtl = -1;
while (true) {
if (!(this.nextParkedWorker != a.l) || a.this._isTerminated != 0 || this.k == 5) {
break;
}
e(3);
Thread.interrupted();
if (this.l == 0) {
this.l = System.nanoTime() + a.this.r;
}
LockSupport.parkNanos(a.this.r);
if (System.nanoTime() - this.l >= 0) {
this.l = 0;
synchronized (a.this.o) {
if (a.this._isTerminated == 0) {
if (((int) (a.this.controlState & 2097151)) > a.this.p) {
if (i.compareAndSet(this, -1, 1)) {
int i3 = this.indexInArray;
d(0);
a.this.p(this, i3, 0);
int andDecrement = (int) (a.j.getAndDecrement(a.this) & 2097151);
if (andDecrement != i3) {
C0343a aVar = a.this.o.get(andDecrement);
m.checkNotNull(aVar);
C0343a aVar2 = aVar;
a.this.o.set(i3, aVar2);
aVar2.d(i3);
a.this.p(aVar2, andDecrement, i3);
}
a.this.o.set(andDecrement, null);
this.k = 5;
}
}
}
}
}
}
} else {
a aVar3 = a.this;
Objects.requireNonNull(aVar3);
if (this.nextParkedWorker == sVar) {
do {
j = aVar3.parkedWorkersStack;
i2 = this.indexInArray;
this.nextParkedWorker = aVar3.o.get((int) (j & 2097151));
} while (!a.i.compareAndSet(aVar3, j, ((long) i2) | ((2097152 + j) & -2097152)));
}
}
} else if (!z2) {
z2 = true;
} else {
e(3);
Thread.interrupted();
LockSupport.parkNanos(this.m);
this.m = 0;
}
} else {
this.m = 0;
int s2 = a.j.s();
this.l = 0;
if (this.k == 3) {
this.k = 2;
}
if (s2 != 0 && e(2)) {
a.this.s();
}
a.this.r(a);
if (s2 != 0) {
a.j.addAndGet(a.this, -2097152);
if (this.k != 5) {
this.k = 4;
}
}
}
}
}
e(5);
}
}
public a(int i2, int i3, long j2, String str) {
this.p = i2;
this.q = i3;
this.r = j2;
this.f2873s = str;
if (i2 >= 1) {
if (i3 >= i2) {
if (i3 <= 2097150) {
if (j2 > 0) {
this.m = new d();
this.n = new d();
this.parkedWorkersStack = 0;
this.o = new AtomicReferenceArray<>(i3 + 1);
this.controlState = ((long) i2) << 42;
this._isTerminated = 0;
return;
}
throw new IllegalArgumentException(c.d.b.a.a.r("Idle worker keep alive time ", j2, " must be positive").toString());
}
throw new IllegalArgumentException(c.d.b.a.a.n("Max pool size ", i3, " should not exceed maximal supported number of threads 2097150").toString());
}
throw new IllegalArgumentException(c.d.b.a.a.o("Max pool size ", i3, " should be greater than or equals to core pool size ", i2).toString());
}
throw new IllegalArgumentException(c.d.b.a.a.n("Core pool size ", i2, " should be at least 1").toString());
}
public static /* synthetic */ void e(a aVar, Runnable runnable, i iVar, boolean z2, int i2) {
g gVar = (i2 & 2) != 0 ? g.i : null;
if ((i2 & 4) != 0) {
z2 = false;
}
aVar.d(runnable, gVar, z2);
}
public final int a() {
synchronized (this.o) {
if (this._isTerminated != 0) {
return -1;
}
long j2 = this.controlState;
int i2 = (int) (j2 & 2097151);
boolean z2 = false;
int coerceAtLeast = f.coerceAtLeast(i2 - ((int) ((j2 & 4398044413952L) >> 21)), 0);
if (coerceAtLeast >= this.p) {
return 0;
}
if (i2 >= this.q) {
return 0;
}
int i3 = ((int) (this.controlState & 2097151)) + 1;
if (i3 > 0 && this.o.get(i3) == null) {
C0343a aVar = new C0343a(i3);
this.o.set(i3, aVar);
if (i3 == ((int) (2097151 & j.incrementAndGet(this)))) {
z2 = true;
}
if (z2) {
aVar.start();
return coerceAtLeast + 1;
}
throw new IllegalArgumentException("Failed requirement.".toString());
}
throw new IllegalArgumentException("Failed requirement.".toString());
}
}
public final h b(Runnable runnable, i iVar) {
long a = k.e.a();
if (!(runnable instanceof h)) {
return new j(runnable, a, iVar);
}
h hVar = (h) runnable;
hVar.i = a;
hVar.j = iVar;
return hVar;
}
public final C0343a c() {
Thread currentThread = Thread.currentThread();
if (!(currentThread instanceof C0343a)) {
currentThread = null;
}
C0343a aVar = (C0343a) currentThread;
if (aVar == null || !m.areEqual(a.this, this)) {
return null;
}
return aVar;
}
/* JADX WARNING: Code restructure failed: missing block: B:28:0x0073, code lost:
if (r1 != null) goto L_0x007e;
*/
@Override // java.io.Closeable, java.lang.AutoCloseable
public void close() {
int i2;
h hVar;
boolean z2;
if (k.compareAndSet(this, 0, 1)) {
C0343a c2 = c();
synchronized (this.o) {
i2 = (int) (this.controlState & 2097151);
}
if (1 <= i2) {
int i3 = 1;
while (true) {
C0343a aVar = this.o.get(i3);
m.checkNotNull(aVar);
C0343a aVar2 = aVar;
if (aVar2 != c2) {
while (aVar2.isAlive()) {
LockSupport.unpark(aVar2);
aVar2.join(10000);
}
m mVar = aVar2.j;
d dVar = this.n;
Objects.requireNonNull(mVar);
h hVar2 = (h) m.a.getAndSet(mVar, null);
if (hVar2 != null) {
dVar.a(hVar2);
}
do {
h f = mVar.f();
if (f != null) {
dVar.a(f);
z2 = true;
continue;
} else {
z2 = false;
continue;
}
} while (z2);
}
if (i3 == i2) {
break;
}
i3++;
}
}
this.n.b();
this.m.b();
while (true) {
if (c2 != null) {
hVar = c2.a(true);
}
hVar = this.m.d();
if (hVar == null) {
hVar = this.n.d();
}
if (hVar == null) {
break;
}
r(hVar);
}
if (c2 != null) {
c2.e(5);
}
this.parkedWorkersStack = 0;
this.controlState = 0;
}
}
public final void d(Runnable runnable, i iVar, boolean z2) {
h hVar;
h b = b(runnable, iVar);
C0343a c2 = c();
boolean z3 = true;
if (c2 == null || c2.k == 5 || (b.j.s() == 0 && c2.k == 2)) {
hVar = b;
} else {
c2.o = true;
hVar = c2.j.a(b, z2);
}
if (hVar != null) {
if (!(hVar.j.s() == 1 ? this.n.a(hVar) : this.m.a(hVar))) {
throw new RejectedExecutionException(c.d.b.a.a.D(new StringBuilder(), this.f2873s, " was terminated"));
}
}
if (!z2 || c2 == null) {
z3 = false;
}
if (b.j.s() != 0) {
long addAndGet = j.addAndGet(this, 2097152);
if (!z3 && !w() && !t(addAndGet)) {
w();
}
} else if (!z3) {
s();
}
}
@Override // java.util.concurrent.Executor
public void execute(Runnable runnable) {
e(this, runnable, null, false, 6);
}
public final int f(C0343a aVar) {
Object obj = aVar.nextParkedWorker;
while (obj != l) {
if (obj == null) {
return 0;
}
C0343a aVar2 = (C0343a) obj;
int i2 = aVar2.indexInArray;
if (i2 != 0) {
return i2;
}
obj = aVar2.nextParkedWorker;
}
return -1;
}
public final void p(C0343a aVar, int i2, int i3) {
while (true) {
long j2 = this.parkedWorkersStack;
int i4 = (int) (2097151 & j2);
long j3 = (2097152 + j2) & -2097152;
if (i4 == i2) {
i4 = i3 == 0 ? f(aVar) : i3;
}
if (i4 >= 0 && i.compareAndSet(this, j2, j3 | ((long) i4))) {
return;
}
}
}
public final void r(h hVar) {
try {
hVar.run();
} catch (Throwable th) {
Thread currentThread = Thread.currentThread();
currentThread.getUncaughtExceptionHandler().uncaughtException(currentThread, th);
}
}
public final void s() {
if (!w() && !t(this.controlState)) {
w();
}
}
public final boolean t(long j2) {
if (f.coerceAtLeast(((int) (2097151 & j2)) - ((int) ((j2 & 4398044413952L) >> 21)), 0) < this.p) {
int a = a();
if (a == 1 && this.p > 1) {
a();
}
if (a > 0) {
return true;
}
}
return false;
}
@Override // java.lang.Object
public String toString() {
ArrayList arrayList = new ArrayList();
int length = this.o.length();
int i2 = 0;
int i3 = 0;
int i4 = 0;
int i5 = 0;
int i6 = 0;
for (int i7 = 1; i7 < length; i7++) {
C0343a aVar = this.o.get(i7);
if (aVar != null) {
int d = aVar.j.d();
int h = b.h(aVar.k);
if (h == 0) {
i2++;
arrayList.add(String.valueOf(d) + "c");
} else if (h == 1) {
i3++;
arrayList.add(String.valueOf(d) + "b");
} else if (h == 2) {
i4++;
} else if (h == 3) {
i5++;
if (d > 0) {
arrayList.add(String.valueOf(d) + "d");
}
} else if (h == 4) {
i6++;
}
}
}
long j2 = this.controlState;
return this.f2873s + '@' + c.q.a.k.a.t(this) + "[Pool Size {core = " + this.p + ", max = " + this.q + "}, Worker States {CPU = " + i2 + ", blocking = " + i3 + ", parked = " + i4 + ", dormant = " + i5 + ", terminated = " + i6 + "}, running workers queues = " + arrayList + ", global CPU queue size = " + this.m.c() + ", global blocking queue size = " + this.n.c() + ", Control State {created workers= " + ((int) (2097151 & j2)) + ", blocking tasks = " + ((int) ((4398044413952L & j2) >> 21)) + ", CPUs acquired = " + (this.p - ((int) ((9223367638808264704L & j2) >> 42))) + "}]";
}
public final boolean w() {
while (true) {
long j2 = this.parkedWorkersStack;
C0343a aVar = this.o.get((int) (2097151 & j2));
if (aVar != null) {
long j3 = (2097152 + j2) & -2097152;
int f = f(aVar);
if (f >= 0 && i.compareAndSet(this, j2, ((long) f) | j3)) {
aVar.nextParkedWorker = l;
}
} else {
aVar = null;
}
if (aVar == null) {
return false;
}
if (C0343a.i.compareAndSet(aVar, -1, 0)) {
LockSupport.unpark(aVar);
return true;
}
}
}
}