All Downloads are FREE. Search and download functionalities are using the official Maven repository.

monix.execution.internal.Trampoline.scala Maven / Gradle / Ivy

Go to download

Sub-module of Monix, exposing low-level primitives for dealing with async execution. See: https://monix.io

There is a newer version: 3.4.1
Show newest version
/*
 * Copyright (c) 2014-2021 by The Monix Project Developers.
 * See the project homepage at: https://monix.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package monix.execution.internal

import monix.execution.internal.collection.ChunkedArrayQueue
import scala.util.control.NonFatal
import scala.annotation.tailrec
import scala.concurrent.{BlockContext, CanAwait, ExecutionContext}

private[execution] class Trampoline {
  private def makeQueue(): ChunkedArrayQueue[Runnable] = ChunkedArrayQueue[Runnable](chunkSize = 16)
  private[this] var immediateQueue = makeQueue()
  private[this] var withinLoop = false

  def startLoop(runnable: Runnable, ec: ExecutionContext): Unit = {
    withinLoop = true
    try immediateLoop(runnable, ec)
    finally {
      withinLoop = false
    }
  }

  final def execute(runnable: Runnable, ec: ExecutionContext): Unit = {
    if (!withinLoop) {
      startLoop(runnable, ec)
    } else {
      immediateQueue.enqueue(runnable)
    }
  }

  protected final def forkTheRest(ec: ExecutionContext): Unit = {
    final class ResumeRun(head: Runnable, rest: ChunkedArrayQueue[Runnable]) extends Runnable {

      def run(): Unit = {
        immediateQueue.enqueueAll(rest)
        immediateLoop(head, ec)
      }
    }

    val head = immediateQueue.dequeue()
    if (head ne null) {
      val rest = immediateQueue
      immediateQueue = makeQueue()
      ec.execute(new ResumeRun(head, rest))
    }
  }

  @tailrec
  private final def immediateLoop(task: Runnable, ec: ExecutionContext): Unit = {
    try {
      task.run()
    } catch {
      case ex: Throwable =>
        forkTheRest(ec)
        if (NonFatal(ex)) ec.reportFailure(ex)
        else throw ex
    }

    val next = immediateQueue.dequeue()
    if (next ne null) immediateLoop(next, ec)
  }

  protected final def trampolineContext(ec: ExecutionContext): BlockContext =
    new BlockContext {
      def blockOn[T](thunk: => T)(implicit permission: CanAwait): T = {
        // In case of blocking, execute all scheduled local tasks on
        // a separate thread, otherwise we could end up with a dead-lock
        forkTheRest(ec)
        thunk
      }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy