what is the purpose of trampoline in DBIO.sameThreadExecutionContext

21 views
Skip to first unread message

yvvq

unread,
Dec 3, 2018, 3:15:04 AM12/3/18
to Slick / ScalaQuery
 i'm  confused by the implementation of DBIO.sameThreadExecutionContext

the execute() and runTrampoline() is always executed  in the same thread,   so `case r => ` in execute() function  will never be reached .  is that right ?

private[slick] object sameThreadExecutionContext extends ExecutionContext {
    private[this] val trampoline = new ThreadLocal[List[Runnable]]

    private[this] def runTrampoline(first: Runnable): Unit = {
      trampoline.set(Nil)
      try {
        var err: Throwable = null
        var r = first
        while(r ne null) {
          try r.run() catch { case t: Throwable => err = t }
          trampoline.get() match {
            case r2 :: rest =>
              trampoline.set(rest)
              r = r2
            case _ => r = null
          }
        }
        if(err ne null) throw err
      } finally trampoline.set(null)
    }

    override def execute(runnable: Runnable): Unit = trampoline.get() match {
      case null => runTrampoline(runnable)
      case r => trampoline.set(runnable :: r)
    }

    override def reportFailure(t: Throwable): Unit = throw t
  }
}


Ivano Pagano

unread,
Dec 4, 2018, 4:46:48 AM12/4/18
to Slick / ScalaQuery
Possibly `r.run()` might call execute() itself, queuing another computation on the threadlocal's trampoline?

yvvq

unread,
Dec 7, 2018, 3:39:17 AM12/7/18
to Slick / ScalaQuery
thx, it seems correct to me

在 2018年12月4日星期二 UTC+8下午5:46:48,Ivano Pagano写道:
Reply all
Reply to author
Forward
0 new messages