1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
/* __ *\
** ________ ___ / / ___ Scala API **
** / __/ __// _ | / / / _ | (c) 2003-2011, LAMP/EPFL **
** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
** /____/\___/_/ |_/____/_/ | | **
** |/ **
\* */
package scala.concurrent.impl
import java.util.concurrent.{Callable, Executor, ExecutorService, Executors, ThreadFactory}
import scala.concurrent.forkjoin._
import scala.concurrent.{ExecutionContext, resolver, Awaitable}
import scala.concurrent.util.{ Duration }
private[scala] class ExecutionContextImpl(es: AnyRef) extends ExecutionContext with Executor {
import ExecutionContextImpl._
val executorService: AnyRef = if (es eq null) getExecutorService else es
// to ensure that the current execution context thread local is properly set
def executorsThreadFactory = new ThreadFactory {
def newThread(r: Runnable) = new Thread(new Runnable {
override def run() {
currentExecutionContext.set(ExecutionContextImpl.this)
r.run()
}
})
}
// to ensure that the current execution context thread local is properly set
def forkJoinPoolThreadFactory = new ForkJoinPool.ForkJoinWorkerThreadFactory {
def newThread(fjp: ForkJoinPool) = new ForkJoinWorkerThread(fjp) {
override def onStart() {
currentExecutionContext.set(ExecutionContextImpl.this)
}
}
}
def getExecutorService: AnyRef =
if (scala.util.Properties.isJavaAtLeast("1.6")) {
val vendor = scala.util.Properties.javaVmVendor
if ((vendor contains "Oracle") || (vendor contains "Sun") || (vendor contains "Apple"))
new ForkJoinPool(
Runtime.getRuntime.availableProcessors(),
forkJoinPoolThreadFactory,
null,
false)
else
Executors.newCachedThreadPool(executorsThreadFactory)
} else Executors.newCachedThreadPool(executorsThreadFactory)
def execute(runnable: Runnable): Unit = executorService match {
case fj: ForkJoinPool =>
Thread.currentThread match {
case fjw: ForkJoinWorkerThread if fjw.getPool eq fj =>
val fjtask = runnable match {
case fjt: ForkJoinTask[_] => fjt
case _ => ForkJoinTask.adapt(runnable)
}
fjtask.fork
case _ =>
fj.execute(runnable)
}
case executor: Executor =>
executor execute runnable
}
def internalBlockingCall[T](awaitable: Awaitable[T], atMost: Duration): T = {
Future.releaseStack(this)
executorService match {
case fj: ForkJoinPool =>
var result: T = null.asInstanceOf[T]
val managedBlocker = new ForkJoinPool.ManagedBlocker {
@volatile var isdone = false
def block() = {
result = awaitable.result(atMost)(scala.concurrent.Await.canAwaitEvidence)
isdone = true
true
}
def isReleasable = isdone
}
ForkJoinPool.managedBlock(managedBlocker)
result
case _ =>
awaitable.result(atMost)(scala.concurrent.Await.canAwaitEvidence)
}
}
def reportFailure(t: Throwable) = t match {
/*TODO: clarify that resolver should wrap Errors, in which case we do not want
Error to be re-thrown here */
case e: Error => throw e // rethrow serious errors
case t =>
//println(t.toString)
t.printStackTrace()
}
}
private[concurrent] object ExecutionContextImpl {
private[concurrent] def currentExecutionContext: ThreadLocal[ExecutionContext] = new ThreadLocal[ExecutionContext] {
override protected def initialValue = null
}
}
|