Skip to content

Commit

Permalink
Fix detection of multithreading usage - it was broken, becouse shutdo…
Browse files Browse the repository at this point in the history
…wn hooks and weak references registry are using threads (#3856)
  • Loading branch information
WojciechMazur committed Mar 31, 2024
1 parent d4bfe5d commit a06f46d
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 7 deletions.
15 changes: 15 additions & 0 deletions scripted-tests/run/detect-multithreading/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
enablePlugins(ScalaNativePlugin)

nativeConfig ~= {
_.withMultithreading(None) // force detection
}

scalaVersion := {
val scalaVersion = System.getProperty("scala.version")
if (scalaVersion == null)
throw new RuntimeException(
"""|The system property 'scala.version' is not defined.
|Specify this property using the scriptedLaunchOpts -D.""".stripMargin
)
else scalaVersion
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
val pluginVersion = System.getProperty("plugin.version")
if (pluginVersion == null)
throw new RuntimeException(
"""|The system property 'plugin.version' is not defined.
|Specify this property using the scriptedLaunchOpts -D.""".stripMargin
)
else addSbtPlugin("org.scala-native" % "sbt-scala-native" % pluginVersion)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import scala.scalanative.unsafe._
import scala.scalanative.libc.stdio._
import scala.scalanative.meta.LinktimeInfo.isMultithreadingEnabled

object MultiThreaded {
def main(args: Array[String]): Unit = {
assert(isMultithreadingEnabled == true)
new Thread(() => println("hello world")).start()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import scala.scalanative.unsafe._
import scala.scalanative.libc.stdio._
import scala.scalanative.meta.LinktimeInfo.isMultithreadingEnabled

object SingleThreaded {
def main(args: Array[String]): Unit = {
assert(isMultithreadingEnabled == false)
println("Hello world")
}
}
5 changes: 5 additions & 0 deletions scripted-tests/run/detect-multithreading/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
> set Compile/mainClass := Some("SingleThreaded")
> run

> set Compile/mainClass := Some("MultiThreaded")
> run
12 changes: 8 additions & 4 deletions tools/src/main/scala/scala/scalanative/build/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,14 @@ object Build {
if (config.compilerConfig.multithreading.isEmpty) {
// format: off
val jlThread = nir.Global.Top("java.lang.Thread")
val jlThreadStart = jlThread.member(nir.Sig.Method("start", Seq(nir.Type.Unit)))
val jlPlatformContext = nir.Global.Top("java.lang.PlatformThreadContext")
val jlPlatformContextStart = jlPlatformContext.member(nir.Sig.Method("start", Seq(nir.Type.Ref(jlThread), nir.Type.Unit)))
val usesSystemThreads = analysis.infos.contains(jlThreadStart) || analysis.infos.contains(jlPlatformContextStart)
val jlMainThread = nir.Global.Top("java.lang.Thread$MainThread$")
val jlVirtualThread = nir.Global.Top("java.lang.VirtualThread")
val usesSystemThreads = analysis.infos.get(jlThread).collect{
case cls: linker.Class =>
cls.subclasses.size > 2 ||
cls.subclasses.map(_.name).diff(Set(jlMainThread, jlVirtualThread)).nonEmpty ||
cls.allocations > 4 // minimal number of allocations
}.getOrElse(false)
// format: on
if (!usesSystemThreads) {
config.logger.info(
Expand Down
3 changes: 2 additions & 1 deletion tools/src/main/scala/scala/scalanative/linker/Infos.scala
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ final class Class(
val implementors = mutable.SortedSet[Class](this)
val subclasses = mutable.Set.empty[Class]
val defaultResponds = mutable.Map.empty[nir.Sig, nir.Global.Member]
var allocated = false
var allocations = 0
def allocated = allocations > 0

lazy val fields: Seq[Field] = {
val out = mutable.UnrolledBuffer.empty[Field]
Expand Down
5 changes: 3 additions & 2 deletions tools/src/main/scala/scala/scalanative/linker/Reach.scala
Original file line number Diff line number Diff line change
Expand Up @@ -418,8 +418,9 @@ private[linker] class Reach(
def reachAllocation(
info: Class
)(implicit srcPosition: nir.SourcePosition): Unit =
if (!info.allocated) {
info.allocated = true
if (info.allocated) info.allocations += 1
else {
info.allocations += 1

// Handle all class and trait virtual calls
// on this class. This includes virtual calls
Expand Down

0 comments on commit a06f46d

Please sign in to comment.