diff options
author | Felix Mulder <felix.mulder@gmail.com> | 2017-04-07 13:47:07 +0200 |
---|---|---|
committer | Felix Mulder <felix.mulder@gmail.com> | 2017-04-12 11:31:15 +0200 |
commit | 55803b2657a473a1ebbebfd9ab7ba4c1b4e27d38 (patch) | |
tree | 4a562a3316999788fe42ef3ec9c1b3fff84b3fd9 /tests/partest-test/deadlock.scala | |
parent | 8fe0d14c1d2bdc26088fc4a29388b961ae1552a2 (diff) | |
download | dotty-55803b2657a473a1ebbebfd9ab7ba4c1b4e27d38.tar.gz dotty-55803b2657a473a1ebbebfd9ab7ba4c1b4e27d38.tar.bz2 dotty-55803b2657a473a1ebbebfd9ab7ba4c1b4e27d38.zip |
Implement meta tests suggested by @DarkDimius
Note that merging this as-is will not protect us against fork bombs.
This is because the timeout of tests is currently 180 seconds. A
forkbomb that is allowed to run for that long...
Diffstat (limited to 'tests/partest-test/deadlock.scala')
-rw-r--r-- | tests/partest-test/deadlock.scala | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/tests/partest-test/deadlock.scala b/tests/partest-test/deadlock.scala new file mode 100644 index 000000000..df561aff3 --- /dev/null +++ b/tests/partest-test/deadlock.scala @@ -0,0 +1,44 @@ +object Test { + class Lock + val lock1 = new Lock + val lock2 = new Lock + + private[this] var took2: Boolean = false + def lock2Taken(): Unit = synchronized { + took2 = true + notify() + } + def tookLock2: Boolean = synchronized(took2) + + val thread1 = new Thread { + override def run(): Unit = synchronized { + lock1.synchronized { + while (!tookLock2) wait() + lock2.synchronized { + println("thread1 in lock2!") + } + } + println("thread1, done!") + } + } + + val thread2 = new Thread { + override def run(): Unit = synchronized { + lock2.synchronized { + lock2Taken() + lock1.synchronized { + println("thread2 in lock1!") + } + } + println("thread2, done!") + } + } + + def main(args: Array[String]): Unit = { + thread1.start() // takes lock1 then sleeps 1s - tries to take lock2 + thread2.start() // takes lock2 then sleeps 1s - tries to take lock1 + + thread1.join() // wait for threads to complete, can't because deadlock! + thread2.join() + } +} |