Skip to content

Commit

Permalink
Add a command line option & directive for enabling/disabling Scala Na…
Browse files Browse the repository at this point in the history
…tive multithreading (#3011)
  • Loading branch information
Gedochao authored Jul 10, 2024
1 parent 63b4748 commit 66298a5
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -236,4 +236,17 @@ class ScalaNativeUsingDirectiveTests extends TestUtil.ScalaCliBuildSuite {
)
}
}

for { multithreadingDirective <- Seq("`native-multithreading`", "nativeMultithreading") }
test(s"ScalaNativeOptions for $multithreadingDirective") {
val inputs = TestInputs(
os.rel / "p.sc" ->
s"""//> using $multithreadingDirective
|def foo() = println("hello foo")
|""".stripMargin
)
inputs.withLoadedBuild(buildOptions, buildThreads, bloopConfig) { (_, _, maybeBuild) =>
assert(maybeBuild.options.scalaNativeOptions.multithreading.get)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ final case class ScalaNativeOptions(
@Group(HelpGroup.ScalaNative.toString)
@HelpMessage("Embed resources into the Scala Native binary (can be read with the Java resources API)")
@Tag(tags.should)
embedResources: Option[Boolean] = None
embedResources: Option[Boolean] = None,

@Group(HelpGroup.ScalaNative.toString)
@HelpMessage("Enable/disable Scala Native multithreading support")
@Tag(tags.should)
nativeMultithreading: Option[Boolean] = None

)
// format: on
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ final case class SharedOptions(
compileDefaults = nativeCompileDefaults,
embedResources = embedResources,
buildTargetStr = nativeTarget,
multithreading = nativeMultithreading,
maxDefaultNativeVersions = maxDefaultScalaNativeVersions
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ final case class ScalaNative(
nativeClangPP: Option[String] = None,
nativeEmbedResources: Option[Boolean] = None,
nativeTarget: Option[String] = None,
nativeMultithreading: Option[Boolean] = None
) extends HasBuildOptions {
// format: on
def buildOptions: Either[BuildException, BuildOptions] = {
Expand All @@ -59,7 +60,8 @@ final case class ScalaNative(
clang = nativeClang,
clangpp = nativeClangPP,
embedResources = nativeEmbedResources,
buildTargetStr = nativeTarget
buildTargetStr = nativeTarget,
multithreading = nativeMultithreading
)
val buildOpt = BuildOptions(scalaNativeOptions = nativeOptions)
Right(buildOpt)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,4 +401,60 @@ trait RunScalaNativeTestDefinitions { _: RunTestDefinitions =>
}
}
}

for {
expectedMultithreadingState <- Seq(true, false)
// multithreading should be enabled by Scala Native by default
setExplicitly <- if (expectedMultithreadingState) Seq(true, false) else Seq(true)
useDirective <- if (setExplicitly) Seq(true, false) else Seq(false)
directive =
if (useDirective && setExplicitly)
s"//> using nativeMultithreading $expectedMultithreadingState"
else ""
cliOptions =
if (!useDirective && setExplicitly)
Seq(s"--native-multithreading=$expectedMultithreadingState")
else Nil
testDescriptionString = useDirective -> setExplicitly match {
case (_, false) => "(implicitly)"
case (true, true) => "with directive"
case (false, true) => "with command line option"
}
}
test(
s"Scala Native multithreading set to $expectedMultithreadingState $testDescriptionString"
) {
val fileName = "multithreading.sc"
val expectedOutput = "42"
val threadSleep = "100"
val threadAwait = "2.seconds"
val inputs = TestInputs(
os.rel / fileName ->
s"""$directive
|import scala.concurrent._
|import scala.concurrent.duration._
|import ExecutionContext.Implicits.global
|val promise = Promise[Int]()
|val thread = new Thread(new Runnable {
| def run(): Unit = {
| Thread.sleep($threadSleep)
| promise.success($expectedOutput)
| }
| })
|thread.start()
|val result = Await.result(promise.future, $threadAwait)
|println(result)
|""".stripMargin
)
inputs.fromRoot { root =>
val r = os.proc(TestUtil.cli, extraOptions, fileName, "--native", cliOptions)
.call(cwd = root, stderr = os.Pipe, check = expectedMultithreadingState)
if (!expectedMultithreadingState) expect(r.exitCode == 1)
else {
expect(r.exitCode == 0)
expect(r.out.trim() == expectedOutput)
}
expect(r.err.trim().contains(s"multithreadingEnabled=$expectedMultithreadingState"))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ final case class ScalaNativeOptions(
compileDefaults: Option[Boolean] = None,
embedResources: Option[Boolean] = None,
buildTargetStr: Option[String] = None,
multithreading: Option[Boolean] = None,
maxDefaultNativeVersions: List[(String, String)] = Nil
) {

Expand Down Expand Up @@ -122,6 +123,9 @@ final case class ScalaNativeOptions(
}
else Nil

private def multithreadingCliOption(): List[String] =
multithreading.toList.flatMap(m => List("--multithreading", m.toString))

def platformSuffix: String =
"native" + ScalaVersion.nativeBinary(finalVersion).getOrElse(finalVersion)

Expand Down Expand Up @@ -174,7 +178,8 @@ final case class ScalaNativeOptions(
linkingCliOptions() ++
compileCliOptions() ++
resourcesCliOptions(resourcesExist) ++
targetCliOption()
targetCliOption() ++
multithreadingCliOption()

}

Expand Down
4 changes: 4 additions & 0 deletions website/docs/reference/cli-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -1396,6 +1396,10 @@ Build target type

Embed resources into the Scala Native binary (can be read with the Java resources API)

### `--native-multithreading`

Enable/disable Scala Native multithreading support

## Scalac options

Available in commands:
Expand Down
6 changes: 6 additions & 0 deletions website/docs/reference/scala-command/cli-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,12 @@ Build target type

Embed resources into the Scala Native binary (can be read with the Java resources API)

### `--native-multithreading`

`SHOULD have` per Scala Runner specification

Enable/disable Scala Native multithreading support

## Scalac options

Available in commands:
Expand Down
36 changes: 36 additions & 0 deletions website/docs/reference/scala-command/runner-specification.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ Build target type

Embed resources into the Scala Native binary (can be read with the Java resources API)

**--native-multithreading**

Enable/disable Scala Native multithreading support

**--repository**

Add repositories for dependency resolution.
Expand Down Expand Up @@ -967,6 +971,10 @@ Build target type

Embed resources into the Scala Native binary (can be read with the Java resources API)

**--native-multithreading**

Enable/disable Scala Native multithreading support

**--repository**

Add repositories for dependency resolution.
Expand Down Expand Up @@ -1532,6 +1540,10 @@ Build target type

Embed resources into the Scala Native binary (can be read with the Java resources API)

**--native-multithreading**

Enable/disable Scala Native multithreading support

**--repository**

Add repositories for dependency resolution.
Expand Down Expand Up @@ -2129,6 +2141,10 @@ Build target type

Embed resources into the Scala Native binary (can be read with the Java resources API)

**--native-multithreading**

Enable/disable Scala Native multithreading support

**--repository**

Add repositories for dependency resolution.
Expand Down Expand Up @@ -2739,6 +2755,10 @@ Build target type

Embed resources into the Scala Native binary (can be read with the Java resources API)

**--native-multithreading**

Enable/disable Scala Native multithreading support

**--repository**

Add repositories for dependency resolution.
Expand Down Expand Up @@ -3325,6 +3345,10 @@ Build target type

Embed resources into the Scala Native binary (can be read with the Java resources API)

**--native-multithreading**

Enable/disable Scala Native multithreading support

**--repository**

Add repositories for dependency resolution.
Expand Down Expand Up @@ -3948,6 +3972,10 @@ Build target type

Embed resources into the Scala Native binary (can be read with the Java resources API)

**--native-multithreading**

Enable/disable Scala Native multithreading support

**--repository**

Add repositories for dependency resolution.
Expand Down Expand Up @@ -4622,6 +4650,10 @@ Build target type

Embed resources into the Scala Native binary (can be read with the Java resources API)

**--native-multithreading**

Enable/disable Scala Native multithreading support

**--repository**

Add repositories for dependency resolution.
Expand Down Expand Up @@ -5537,6 +5569,10 @@ Build target type

Embed resources into the Scala Native binary (can be read with the Java resources API)

**--native-multithreading**

Enable/disable Scala Native multithreading support

**--repository**

Add repositories for dependency resolution.
Expand Down

0 comments on commit 66298a5

Please sign in to comment.