Patch ThreadTracker during Kotlin plugin init

This commit is contained in:
Nikolay Krasko
2016-09-21 22:18:32 +03:00
parent 68f0953b02
commit 63943c60d3
6 changed files with 81 additions and 78 deletions
@@ -95,9 +95,7 @@ fun doKotlinTearDown(project: Project, runnable: RunnableWithException) {
fun doKotlinTearDown(project: Project, runnable: () -> Unit) {
unInvalidateBuiltinsAndStdLib(project) {
patchThreadTracker {
runnable()
}
runnable()
}
}
@@ -148,55 +146,4 @@ fun Document.extractMarkerOffset(project: Project, caretMarker: String = "<caret
PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(this)
return offset
}
private val patched = AtomicBoolean(false)
fun patchThreadTracker(runnable: RunnableWithException) {
patchThreadTracker {
runnable.run()
}
}
fun patchThreadTracker(runnable: () -> Unit) {
fun patch() {
if (patched.get()) return
patched.compareAndSet(false, true)
IdeaForkJoinWorkerThreadFactory.setupForkJoinCommonPool()
// Check setup was successful
val commonPoolFactoryName = ForkJoinPool.commonPool().factory.javaClass.name
if (commonPoolFactoryName == IdeaForkJoinWorkerThreadFactory::class.java.name) {
return
}
println("Patching!")
try {
val wellKnownOffendersField = try {
ThreadTracker::class.java.getDeclaredField("wellKnownOffenders")
}
catch (communityPropertyNotFoundEx: NoSuchFieldException) {
ThreadTracker::class.java.getDeclaredField("a")
}
wellKnownOffendersField.isAccessible = true
@Suppress("UNCHECKED_CAST")
val wellKnownOffenders = wellKnownOffendersField.get(null) as MutableSet<String>
wellKnownOffenders.add("ForkJoinPool.commonPool-worker-")
}
catch (e: NoSuchFieldException) {
println("Patching failed: " + e)
}
catch (e: IllegalAccessException) {
println("Patching failed: " + e)
}
}
patch()
runnable()
}