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

Code before:

  private var nextQueueName = 10000
  private var coordinatorWaiting = false
  private var coordinatorWakeUpAt = 0L

  /** Queues with tasks that are currently executing their [TaskQueue.activeTask]. */
  private val busyQueues = mutableListOf<TaskQueue>()

  /** Queues not in [busyQueues] that have non-empty [TaskQueue.futureTasks]. */
  private val readyQueues = mutableListOf<TaskQueue>()

  private val runnable: Runnable = object : Runnable {
    override fun run() {
      while (true) {
        val task = this@TaskRunner.lock.withLock {
          awaitTaskToRun()
        } ?: return

        logger.logElapsed(task, task.queue!!) {
          var completedNormally = false
          try {
            runTask(task)
            completedNormally = true
          } finally {
            // If the task is crashing start another thread to service the queues.
            if (!completedNormally) {
              lock.withLock {
                backend.execute(this@TaskRunner, this)
              }
            }
          }
        }
      }
    }
  }

  internal fun kickCoordinator(taskQueue: TaskQueue) {
    lock.assertThreadHolds()

    if (taskQueue.activeTask == null) {
      if (taskQueue.futureTasks.isNotEmpty()) {
        readyQueues.addIfAbsent(taskQueue)
      } else {
        readyQueues.remove(taskQueue)
      }
    }

    if (coordinatorWaiting) {
      backend.coordinatorNotify(this@TaskRunner)
    } else {
      backend.execute(this@TaskRunner, runnable)

Review comment (bug):
Being able to avoid the hazard of dereferencing the wrong `this` is a very nice improvement

Code after:
  private var nextQueueName = 10000
  private var coordinatorWaiting = false
  private var coordinatorWakeUpAt = 0L

  /** Queues with tasks that are currently executing their [TaskQueue.activeTask]. */
  private val busyQueues = mutableListOf<TaskQueue>()

  /** Queues not in [busyQueues] that have non-empty [TaskQueue.futureTasks]. */
  private val readyQueues = mutableListOf<TaskQueue>()

  private val runnable: Runnable = object : Runnable {
    override fun run() {
      while (true) {
        val task = this@TaskRunner.lock.withLock {
          awaitTaskToRun()
        } ?: return

        logger.logElapsed(task, task.queue!!) {
          var completedNormally = false
          try {
            runTask(task)
            completedNormally = true
          } finally {
            // If the task is crashing start another thread to service the queues.
            if (!completedNormally) {
              lock.withLock {
                backend.execute(this@TaskRunner, this)
              }
            }
          }
        }
      }
    }
  }

  internal fun kickCoordinator(taskQueue: TaskQueue) {
    lock.assertHeld()

    if (taskQueue.activeTask == null) {
      if (taskQueue.futureTasks.isNotEmpty()) {
        readyQueues.addIfAbsent(taskQueue)
      } else {
        readyQueues.remove(taskQueue)
      }
    }

    if (coordinatorWaiting) {
      backend.coordinatorNotify(this@TaskRunner)
    } else {
      backend.execute(this@TaskRunner, runnable)
    }