-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Prevent deadlocks when mixing threads with queue based execution…
… context (#3852) Prevent deadlocks and prevent pre-early exit when mixing threads with queue based execution context NativeThread.registry.aliveThreads now returns iterator instead of array
- Loading branch information
1 parent
6123483
commit 677b1bc
Showing
13 changed files
with
163 additions
and
65 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
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
25 changes: 0 additions & 25 deletions
25
nativelib/src/main/scala/scala/scalanative/runtime/JoinNonDeamonThreads.scala
This file was deleted.
Oops, something went wrong.
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
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
25 changes: 25 additions & 0 deletions
25
nativelib/src/main/scala/scala/scalanative/runtime/package.state.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package scala.scalanative.runtime | ||
|
||
import java.util.concurrent.locks.LockSupport | ||
import scala.scalanative.meta.LinktimeInfo.isMultithreadingEnabled | ||
|
||
// Extracted fields from runtime package to ensure it does not require initialization | ||
private[runtime] object MainThreadShutdownContext { | ||
@volatile var shutdownThread: Thread = _ | ||
var gracefully: Boolean = true | ||
|
||
def inShutdown: Boolean = shutdownThread != null | ||
|
||
/* Notify that thread has */ | ||
def onThreadFinished(thread: Thread): Unit = if (!thread.isDaemon()) signal() | ||
def onTaskEnqueued(): Unit = signal() | ||
|
||
private def signal() = | ||
if (isMultithreadingEnabled) | ||
if (inShutdown) | ||
LockSupport.unpark(shutdownThread) | ||
} | ||
|
||
private object ExecInfo { | ||
var filename: String = null | ||
} |
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
37 changes: 37 additions & 0 deletions
37
scripted-tests/run/shutdown/variants/QueueWithThreads.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import java.util.concurrent.locks.ReentrantLock | ||
import java.util.concurrent.CountDownLatch | ||
import scala.scalanative.runtime.NativeExecutionContext | ||
|
||
object Test { | ||
def main(args: Array[String]): Unit = { | ||
println("Hello, World!") | ||
def spawnRunnable(name: String)(fn: => Unit) = | ||
NativeExecutionContext.queue | ||
.execute(() => { fn; println(s"task $name done") }) | ||
|
||
def spawnThread(name: String)(fn: => Unit) = { | ||
val t = new Thread(() => { fn; println(s"thread $name done") }) | ||
t.setName(name) | ||
t.start() | ||
} | ||
|
||
spawnThread("T1") { | ||
val latch1 = new CountDownLatch(1) | ||
spawnRunnable("R1") { latch1.countDown() } | ||
spawnThread("T2") { | ||
latch1.await() // blocks until T1, R1 are done | ||
val latch2 = new CountDownLatch(1) | ||
val latch3 = new CountDownLatch(3) | ||
spawnThread("T3") { | ||
spawnRunnable("R2") { latch2.await(); latch3.countDown() } | ||
} | ||
spawnThread("T4") { | ||
spawnRunnable("R3") { latch2.await(); latch3.countDown() } | ||
} | ||
spawnRunnable("R4") { latch2.await(); latch3.countDown() } | ||
latch2.countDown() | ||
latch3.await() | ||
} | ||
} | ||
} | ||
} |
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