package org.webrtc; import android.os.Handler; import android.os.Looper; import android.os.SystemClock; import androidx.annotation.Nullable; import java.util.concurrent.Callable; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; /* loaded from: classes3.dex */ public class ThreadUtils { /* renamed from: org.webrtc.ThreadUtils$1 reason: invalid class name */ /* loaded from: classes3.dex */ public class AnonymousClass1 implements BlockingOperation { public final /* synthetic */ Thread val$thread; public AnonymousClass1(Thread thread) { this.val$thread = thread; } @Override // org.webrtc.ThreadUtils.BlockingOperation public void run() throws InterruptedException { this.val$thread.join(); } } /* renamed from: org.webrtc.ThreadUtils$1CaughtException reason: invalid class name */ /* loaded from: classes3.dex */ public class C1CaughtException { public Exception e; } /* renamed from: org.webrtc.ThreadUtils$1Result reason: invalid class name */ /* loaded from: classes3.dex */ public class C1Result { public V value; } /* renamed from: org.webrtc.ThreadUtils$2 reason: invalid class name */ /* loaded from: classes3.dex */ public class AnonymousClass2 implements BlockingOperation { public final /* synthetic */ CountDownLatch val$latch; public AnonymousClass2(CountDownLatch countDownLatch) { this.val$latch = countDownLatch; } @Override // org.webrtc.ThreadUtils.BlockingOperation public void run() throws InterruptedException { this.val$latch.await(); } } /* renamed from: org.webrtc.ThreadUtils$3 reason: invalid class name */ /* loaded from: classes3.dex */ public class AnonymousClass3 implements Runnable { public final /* synthetic */ CountDownLatch val$barrier; public final /* synthetic */ Callable val$callable; public final /* synthetic */ C1CaughtException val$caughtException; public final /* synthetic */ C1Result val$result; public AnonymousClass3(C1Result r1, Callable callable, C1CaughtException r3, CountDownLatch countDownLatch) { this.val$result = r1; this.val$callable = callable; this.val$caughtException = r3; this.val$barrier = countDownLatch; } @Override // java.lang.Runnable public void run() { try { this.val$result.value = this.val$callable.call(); } catch (Exception e) { this.val$caughtException.e = e; } this.val$barrier.countDown(); } } /* renamed from: org.webrtc.ThreadUtils$4 reason: invalid class name */ /* loaded from: classes3.dex */ public class AnonymousClass4 implements Callable { public final /* synthetic */ Runnable val$runner; public AnonymousClass4(Runnable runnable) { this.val$runner = runnable; } @Override // java.util.concurrent.Callable public Void call() { this.val$runner.run(); return null; } } /* loaded from: classes3.dex */ public interface BlockingOperation { void run() throws InterruptedException; } /* loaded from: classes3.dex */ public static class ThreadChecker { @Nullable private Thread thread = Thread.currentThread(); public void checkIsOnValidThread() { if (this.thread == null) { this.thread = Thread.currentThread(); } if (Thread.currentThread() != this.thread) { throw new IllegalStateException("Wrong thread"); } } public void detachThread() { this.thread = null; } } public static void awaitUninterruptibly(CountDownLatch countDownLatch) { executeUninterruptibly(new AnonymousClass2(countDownLatch)); } public static boolean awaitUninterruptibly(CountDownLatch countDownLatch, long j) { long elapsedRealtime = SystemClock.elapsedRealtime(); boolean z2 = false; long j2 = j; boolean z3 = false; do { try { z2 = countDownLatch.await(j2, TimeUnit.MILLISECONDS); break; } catch (InterruptedException unused) { z3 = true; j2 = j - (SystemClock.elapsedRealtime() - elapsedRealtime); if (j2 <= 0) { } } } while (j2 <= 0); if (z3) { Thread.currentThread().interrupt(); } return z2; } public static void checkIsOnMainThread() { if (Thread.currentThread() != Looper.getMainLooper().getThread()) { throw new IllegalStateException("Not on main thread!"); } } public static StackTraceElement[] concatStackTraces(StackTraceElement[] stackTraceElementArr, StackTraceElement[] stackTraceElementArr2) { StackTraceElement[] stackTraceElementArr3 = new StackTraceElement[stackTraceElementArr.length + stackTraceElementArr2.length]; System.arraycopy(stackTraceElementArr, 0, stackTraceElementArr3, 0, stackTraceElementArr.length); System.arraycopy(stackTraceElementArr2, 0, stackTraceElementArr3, stackTraceElementArr.length, stackTraceElementArr2.length); return stackTraceElementArr3; } public static void executeUninterruptibly(BlockingOperation blockingOperation) { boolean z2 = false; while (true) { try { blockingOperation.run(); break; } catch (InterruptedException unused) { z2 = true; } } if (z2) { Thread.currentThread().interrupt(); } } public static V invokeAtFrontUninterruptibly(Handler handler, Callable callable) { if (handler.getLooper().getThread() == Thread.currentThread()) { try { return callable.call(); } catch (Exception e) { throw new RuntimeException(e); } } else { C1Result r0 = new C1Result(); C1CaughtException r1 = new C1CaughtException(); CountDownLatch countDownLatch = new CountDownLatch(1); handler.post(new AnonymousClass3(r0, callable, r1, countDownLatch)); awaitUninterruptibly(countDownLatch); if (r1.e == null) { return r0.value; } RuntimeException runtimeException = new RuntimeException(r1.e); runtimeException.setStackTrace(concatStackTraces(r1.e.getStackTrace(), runtimeException.getStackTrace())); throw runtimeException; } } public static void invokeAtFrontUninterruptibly(Handler handler, Runnable runnable) { invokeAtFrontUninterruptibly(handler, new AnonymousClass4(runnable)); } public static void joinUninterruptibly(Thread thread) { executeUninterruptibly(new AnonymousClass1(thread)); } public static boolean joinUninterruptibly(Thread thread, long j) { long elapsedRealtime = SystemClock.elapsedRealtime(); boolean z2 = false; long j2 = j; while (j2 > 0) { try { thread.join(j2); break; } catch (InterruptedException unused) { j2 = j - (SystemClock.elapsedRealtime() - elapsedRealtime); z2 = true; } } if (z2) { Thread.currentThread().interrupt(); } return !thread.isAlive(); } }