Repository: square/okhttp
File: okhttp/src/jvmMain/kotlin/okhttp3/internal/concurrent/TaskQueue.kt (Kotlin)

Code before:
    // Insert in chronological order. Always compare deltas because nanoTime() is permitted to wrap.
    var insertAt = futureTasks.indexOfFirst { it.nextExecuteNanoTime - now > delayNanos }
    if (insertAt == -1) insertAt = futureTasks.size
    futureTasks.add(insertAt, task)

    // Impact the coordinator if we inserted at the front.
    return insertAt == 0
  }

  /**
   * Schedules immediate execution of [Task.tryCancel] on all currently-enqueued tasks. These calls
   * will not be made until any currently-executing task has completed. Tasks that return true will
   * be removed from the execution schedule.
   */
  fun cancelAll() {
    lock.assertThreadDoesntHold()

    taskRunner.lock.withLock {
      if (cancelAllAndDecide()) {
        taskRunner.kickCoordinator(this)
      }
    }
  }

  fun shutdown() {
    lock.assertThreadDoesntHold()

    taskRunner.lock.withLock {
      shutdown = true
      if (cancelAllAndDecide()) {
        taskRunner.kickCoordinator(this)
      }
    }
  }

  /** Returns true if the coordinator is impacted. */
  internal fun cancelAllAndDecide(): Boolean {
    if (activeTask != null && activeTask!!.cancelable) {
      cancelActiveTask = true
    }

    var tasksCanceled = false
    for (i in futureTasks.size - 1 downTo 0) {
      if (futureTasks[i].cancelable) {
        taskRunner.logger.taskLog(futureTasks[i], this) { "canceled" }
        tasksCanceled = true
        futureTasks.removeAt(i)
      }
    }
    return tasksCanceled
  }

Review comment (suggestion):
Rename to `assertNotHeld()` ?

(Previously the fact that `Any` is a lock is weird, and so this function got a deservingly weird name. Now that it’s a function on Lock, it can have a reasonable name?)

Code after:
    // Insert in chronological order. Always compare deltas because nanoTime() is permitted to wrap.
    var insertAt = futureTasks.indexOfFirst { it.nextExecuteNanoTime - now > delayNanos }
    if (insertAt == -1) insertAt = futureTasks.size
    futureTasks.add(insertAt, task)

    // Impact the coordinator if we inserted at the front.
    return insertAt == 0
  }

  /**
   * Schedules immediate execution of [Task.tryCancel] on all currently-enqueued tasks. These calls
   * will not be made until any currently-executing task has completed. Tasks that return true will
   * be removed from the execution schedule.
   */
  fun cancelAll() {
    lock.assertNotHeld()

    taskRunner.lock.withLock {
      if (cancelAllAndDecide()) {
        taskRunner.kickCoordinator(this)
      }
    }
  }

  fun shutdown() {
    lock.assertNotHeld()

    taskRunner.lock.withLock {
      shutdown = true
      if (cancelAllAndDecide()) {
        taskRunner.kickCoordinator(this)
      }
    }
  }

  /** Returns true if the coordinator is impacted. */
  internal fun cancelAllAndDecide(): Boolean {
    if (activeTask != null && activeTask!!.cancelable) {
      cancelActiveTask = true
    }

    var tasksCanceled = false
    for (i in futureTasks.size - 1 downTo 0) {
      if (futureTasks[i].cancelable) {
        taskRunner.logger.taskLog(futureTasks[i], this) { "canceled" }
        tasksCanceled = true
        futureTasks.removeAt(i)
      }
    }
    return tasksCanceled
  }