package androidx.work.impl.utils; import androidx.annotation.GuardedBy; import androidx.annotation.NonNull; import androidx.annotation.VisibleForTesting; import androidx.work.impl.utils.taskexecutor.SerialExecutor; import java.util.ArrayDeque; import java.util.concurrent.Executor; /* loaded from: classes.dex */ public class SerialExecutorImpl implements SerialExecutor { @GuardedBy("mLock") private Runnable mActive; private final Executor mExecutor; private final ArrayDeque mTasks = new ArrayDeque<>(); final Object mLock = new Object(); @NonNull @VisibleForTesting public Executor getDelegatedExecutor() { return this.mExecutor; } public SerialExecutorImpl(@NonNull Executor executor) { this.mExecutor = executor; } @Override // java.util.concurrent.Executor public void execute(@NonNull Runnable runnable) { synchronized (this.mLock) { try { this.mTasks.add(new Task(this, runnable)); if (this.mActive == null) { scheduleNext(); } } catch (Throwable th) { throw th; } } } @GuardedBy("mLock") public void scheduleNext() { Task poll = this.mTasks.poll(); this.mActive = poll; if (poll != null) { this.mExecutor.execute(poll); } } @Override // androidx.work.impl.utils.taskexecutor.SerialExecutor public boolean hasPendingTasks() { boolean z; synchronized (this.mLock) { z = !this.mTasks.isEmpty(); } return z; } public static class Task implements Runnable { final Runnable mRunnable; final SerialExecutorImpl mSerialExecutor; public Task(@NonNull SerialExecutorImpl serialExecutorImpl, @NonNull Runnable runnable) { this.mSerialExecutor = serialExecutorImpl; this.mRunnable = runnable; } @Override // java.lang.Runnable public void run() { try { this.mRunnable.run(); synchronized (this.mSerialExecutor.mLock) { this.mSerialExecutor.scheduleNext(); } } catch (Throwable th) { synchronized (this.mSerialExecutor.mLock) { this.mSerialExecutor.scheduleNext(); throw th; } } } } }