-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from natsukagami/cross-platform
Multiplatform support
- Loading branch information
Showing
40 changed files
with
457 additions
and
204 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[submodule "dependencies/scala-native"] | ||
path = dependencies/scala-native | ||
url = [email protected]:scala-native/scala-native.git | ||
[submodule "dependencies/munit"] | ||
path = dependencies/munit | ||
url = [email protected]:natsukagami/munit.git | ||
branch = use-0.5-snapshot-sn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Contributing | ||
|
||
## Building `gears` | ||
|
||
`gears` currently require: | ||
- **On the JVM**: JVM with support for virtual threads. This usually means JVM 21+, or 19+ with `--enable-preview`. | ||
- **On Scala Native**: Scala Native with delimited continuations support. See the pinned versions in [`dependencies`](./dependencies/README.md). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,26 @@ | ||
import sbtcrossproject.CrossPlugin.autoImport.{crossProject, CrossType} | ||
import scalanative.build._ | ||
|
||
ThisBuild / scalaVersion := "3.3.1" | ||
|
||
lazy val root = project | ||
lazy val root = | ||
crossProject(JVMPlatform, NativePlatform) | ||
.crossType(CrossType.Full) | ||
.in(file(".")) | ||
.settings( | ||
.settings(Seq( | ||
name := "Gears", | ||
organization := "ch.epfl.lamp", | ||
version := "0.1.0-SNAPSHOT", | ||
libraryDependencies += "org.scalameta" %% "munit" % "0.7.29" % Test | ||
) | ||
testFrameworks += new TestFramework("munit.Framework") | ||
)) | ||
.jvmSettings(Seq( | ||
javaOptions += "--version 21", | ||
libraryDependencies += "org.scalameta" %% "munit" % "1.0.0-M10" % Test | ||
)) | ||
.nativeSettings(Seq( | ||
nativeConfig ~= { c => | ||
c.withMultithreadingSupport(true) | ||
.withGC(GC.boehm) // immix doesn't work yet | ||
}, | ||
libraryDependencies += "org.scalameta" %%% "munit" % "1.0.0-M10+15-3940023e-SNAPSHOT" % Test | ||
)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
## Custom Dependencies for Scala Native | ||
|
||
Scala Native requires some libraries to be compiled from source and `publishLocal`'d. | ||
|
||
### TL; DR | ||
|
||
```bash | ||
./publish-deps.sh | ||
``` | ||
|
||
### What are included? | ||
|
||
- The current snapshot version of Scala Native, pinned in `scala-native`: for the delimited continuation support. | ||
This needs to be published for both `3.3.1` (for `gears`) and `3.1.2` (for `munit`): | ||
```bash | ||
sbt "publish-local-dev 3; ++3.1.2 publishLocal" | ||
``` | ||
- A fork of `munit` that uses the above snapshot, with a simple fix (https://github.com/scalameta/munit/pull/714) to make it compile. | ||
Pinned in `munit`. | ||
```bash | ||
sbt "munitNative/publishLocal" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -e | ||
|
||
cd "$(dirname "${BASH_SOURCE[0]}")" # change to this directory | ||
|
||
cd scala-native && sbt 'publish-local-dev 3' && cd .. | ||
|
||
if test "$1" != "--scala-native-only"; then | ||
cd scala-native && sbt '++3.1.2 publishLocal' && cd .. | ||
cd munit && sbt "++3.1.2 munitNative/publishLocal" && cd .. | ||
fi | ||
|
Submodule scala-native
added at
f4b907
3 changes: 2 additions & 1 deletion
3
src/main/scala/PosixLikeIO/PIO.scala → jvm/src/main/scala/PosixLikeIO/PIO.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
...sixLikeIO/examples/readAndWriteFile.scala → ...sixLikeIO/examples/readAndWriteFile.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
.../PosixLikeIO/examples/readWholeFile.scala → .../PosixLikeIO/examples/readWholeFile.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package gears.async.default | ||
|
||
import gears.async._ | ||
|
||
given AsyncOperations = JvmAsyncOperations | ||
given VThreadSupport.type = VThreadSupport | ||
given VThreadSupport.Scheduler = VThreadScheduler |
2 changes: 0 additions & 2 deletions
2
...main/scala/async/JvmAsyncOperations.scala → ...main/scala/async/JvmAsyncOperations.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
...ain/scala/measurements/measureTimes.scala → ...ain/scala/measurements/measureTimes.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import gears.async.{Async, Future, AsyncSupport, uninterruptible} | ||
import gears.async.AsyncOperations.* | ||
import gears.async.default.given | ||
import scala.util.boundary | ||
import boundary.break | ||
import scala.concurrent.duration.{Duration, DurationInt} | ||
import java.util.concurrent.CancellationException | ||
import scala.util.Success | ||
import scala.util.Properties | ||
|
||
// JVM-only since `munitTimeout` is not available on scala native. | ||
// See (here)[https://scalameta.org/munit/docs/tests.html#customize-test-timeouts]. | ||
class JVMCancellationBehavior extends munit.FunSuite: | ||
override def munitTimeout: Duration = 2.seconds | ||
test("no cancel -> timeout".fail): | ||
Async.blocking: | ||
val f = Future: | ||
Thread.sleep(5000) | ||
1 | ||
f.result | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package gears.async.default | ||
|
||
import gears.async._ | ||
import gears.async.native.ForkJoinSupport | ||
|
||
object DefaultSupport extends ForkJoinSupport | ||
|
||
given AsyncSupport = DefaultSupport | ||
given DefaultSupport.Scheduler = DefaultSupport | ||
given AsyncOperations = DefaultSupport |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package gears.async.native | ||
|
||
import gears.async._ | ||
import scala.scalanative.runtime.{Continuations => native} | ||
import java.util.concurrent.ForkJoinPool | ||
import scala.concurrent.ExecutionContext | ||
import scala.concurrent.JavaConversions._ | ||
import scala.concurrent.duration._ | ||
import java.util.concurrent.atomic.AtomicBoolean | ||
import gears.async.Future.Promise | ||
import java.util.concurrent.CancellationException | ||
|
||
class NativeContinuation[-T, +R] private[native] (val cont: T => R) extends Suspension[T, R]: | ||
def resume(arg: T): R = cont(arg) | ||
|
||
trait NativeSuspend extends SuspendSupport: | ||
type Label[R] = native.BoundaryLabel[R] | ||
type Suspension[T, R] = NativeContinuation[T, R] | ||
|
||
override def boundary[R](body: (Label[R]) ?=> R): R = | ||
native.boundary(body) | ||
|
||
override def suspend[T, R](body: Suspension[T, R] => R)(using Label[R]): T = native.suspend[T, R](f => body(NativeContinuation(f))) | ||
end NativeSuspend | ||
|
||
/** Spawns a single thread that does all the sleeping. */ | ||
class ExecutorWithSleepThread(val exec: ExecutionContext) extends ExecutionContext with Scheduler { | ||
import scala.collection.mutable | ||
private case class Sleeper(wakeTime: Deadline, toRun: Runnable): | ||
var isCancelled = false | ||
private given Ordering[Sleeper] = Ordering.by((sleeper: Sleeper) => sleeper.wakeTime).reverse | ||
private val sleepers = mutable.PriorityQueue.empty[Sleeper] | ||
private var sleepingUntil: Option[Deadline] = None | ||
|
||
override def execute(body: Runnable): Unit = exec.execute(body) | ||
override def reportFailure(cause: Throwable): Unit = exec.reportFailure(cause) | ||
override def schedule(delay: FiniteDuration, body: Runnable): Cancellable = { | ||
val sleeper = Sleeper(delay.fromNow, body) | ||
// push to the sleeping priority queue | ||
this.synchronized { | ||
sleepers += sleeper | ||
val shouldWake = sleepingUntil.map(sleeper.wakeTime < _).getOrElse(true) | ||
if shouldWake then this.notifyAll() | ||
} | ||
() => { sleeper.isCancelled = true } | ||
} | ||
|
||
// Sleep until the first timer is due, or .wait() otherwise | ||
private def sleepLoop(): Unit = this.synchronized { | ||
while (true) { | ||
sleepingUntil = sleepers.headOption.map(_.wakeTime) | ||
val current = sleepingUntil match | ||
case None => | ||
this.wait() | ||
Deadline.now | ||
case Some(value) => | ||
val current0 = Deadline.now | ||
val timeLeft = value - current0 | ||
|
||
if timeLeft.toNanos > 0 then | ||
this.wait(timeLeft.toMillis.max(10)) | ||
Deadline.now | ||
else current0 | ||
|
||
// Pop sleepers until no more available | ||
while (sleepers.headOption.exists(_.wakeTime <= current)) { | ||
val task = sleepers.dequeue() | ||
if !task.isCancelled then execute(task.toRun) | ||
} | ||
} | ||
} | ||
|
||
val sleeperThread = Thread(() => sleepLoop()) | ||
sleeperThread.setDaemon(true) | ||
sleeperThread.start() | ||
} | ||
|
||
class SuspendExecutorWithSleep(exec: ExecutionContext) extends ExecutorWithSleepThread(exec) | ||
with AsyncSupport | ||
with AsyncOperations | ||
with NativeSuspend { | ||
type Scheduler = this.type | ||
override def sleep(millis: Long)(using Async): Unit = | ||
Future.withResolver[Unit]: resolver => | ||
val cancellable = schedule(millis.millis, () => resolver.resolve(())) | ||
resolver.onCancel(cancellable.cancel) | ||
.link() | ||
.value | ||
} | ||
|
||
class ForkJoinSupport extends SuspendExecutorWithSleep(new ForkJoinPool()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
addSbtPlugin("org.portable-scala" % "sbt-scala-native-crossproject" % "1.2.0") | ||
addSbtPlugin("org.scala-native" % "sbt-scala-native" % "0.5.0-SNAPSHOT") |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.