package io.grpc.internal; import com.google.common.base.Stopwatch; import java.util.concurrent.Executor; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes6.dex */ public final class Rescheduler { private boolean enabled; private long runAtNanos; private final Runnable runnable; private final ScheduledExecutorService scheduler; private final Executor serializingExecutor; private final Stopwatch stopwatch; private ScheduledFuture wakeUp; /* JADX INFO: Access modifiers changed from: package-private */ public Rescheduler(Runnable runnable, Executor executor, ScheduledExecutorService scheduledExecutorService, Stopwatch stopwatch) { this.runnable = runnable; this.serializingExecutor = executor; this.scheduler = scheduledExecutorService; this.stopwatch = stopwatch; stopwatch.start(); } /* JADX INFO: Access modifiers changed from: package-private */ public final void reschedule(long j, TimeUnit timeUnit) { long nanos = timeUnit.toNanos(j); long nanoTime = nanoTime() + nanos; this.enabled = true; if (nanoTime - this.runAtNanos < 0 || this.wakeUp == null) { ScheduledFuture scheduledFuture = this.wakeUp; if (scheduledFuture != null) { scheduledFuture.cancel(false); } this.wakeUp = this.scheduler.schedule(new FutureRunnable(), nanos, TimeUnit.NANOSECONDS); } this.runAtNanos = nanoTime; } /* JADX INFO: Access modifiers changed from: package-private */ public final void cancel(boolean z) { ScheduledFuture scheduledFuture; this.enabled = false; if (!z || (scheduledFuture = this.wakeUp) == null) { return; } scheduledFuture.cancel(false); this.wakeUp = null; } /* loaded from: classes6.dex */ final class FutureRunnable implements Runnable { final Rescheduler this$0; private FutureRunnable(Rescheduler rescheduler) { this.this$0 = rescheduler; } @Override // java.lang.Runnable public final void run() { this.this$0.serializingExecutor.execute(new ChannelFutureRunnable()); } /* JADX INFO: Access modifiers changed from: private */ public boolean isEnabled() { return this.this$0.enabled; } } /* loaded from: classes6.dex */ final class ChannelFutureRunnable implements Runnable { final Rescheduler this$0; private ChannelFutureRunnable(Rescheduler rescheduler) { this.this$0 = rescheduler; } @Override // java.lang.Runnable public final void run() { if (!this.this$0.enabled) { this.this$0.wakeUp = null; return; } long nanoTime = this.this$0.nanoTime(); if (this.this$0.runAtNanos - nanoTime > 0) { Rescheduler rescheduler = this.this$0; rescheduler.wakeUp = rescheduler.scheduler.schedule(new FutureRunnable(), this.this$0.runAtNanos - nanoTime, TimeUnit.NANOSECONDS); return; } this.this$0.enabled = false; this.this$0.wakeUp = null; this.this$0.runnable.run(); } } static boolean isEnabled(Runnable runnable) { return ((FutureRunnable) runnable).isEnabled(); } /* JADX INFO: Access modifiers changed from: private */ public long nanoTime() { return this.stopwatch.elapsed(TimeUnit.NANOSECONDS); } }