Commit 867f991 1 parent 2b164c0 commit 867f991 Copy full SHA for 867f991
File tree 3 files changed +32
-5
lines changed
3 files changed +32
-5
lines changed Original file line number Diff line number Diff line change 1
1
package ox
2
2
3
- def forever (f : => Unit ): Nothing =
3
+ import java .util .concurrent .locks .LockSupport
4
+
5
+ inline def forever (f : => Unit ): Nothing =
4
6
while true do f
5
7
throw new RuntimeException (" can't get here" )
6
8
7
9
/** Repeat evaluating `f` while it evaluates to `true`. */
8
- def repeatWhile (f : => Boolean ): Unit =
10
+ inline def repeatWhile (f : => Boolean ): Unit =
9
11
var loop = true
10
12
while loop do loop = f
11
13
12
14
/** Repeat evaluating `f` until it evaluates to `true`. */
13
- def repeatUntil (f : => Boolean ): Unit =
15
+ inline def repeatUntil (f : => Boolean ): Unit =
14
16
var loop = true
15
17
while loop do loop = ! f
16
18
17
- def uninterruptible [T ](f : => T ): T =
19
+ inline def uninterruptible [T ](f : => T ): T =
18
20
scoped {
19
21
val t = fork(f)
20
22
@@ -27,3 +29,9 @@ def uninterruptible[T](f: => T): T =
27
29
28
30
joinDespiteInterrupted
29
31
}
32
+
33
+ /** Blocks the current thread indefinitely, until it is interrupted. */
34
+ inline def never : Nothing = forever {
35
+ LockSupport .park()
36
+ if Thread .interrupted() then throw new InterruptedException ()
37
+ }
Original file line number Diff line number Diff line change @@ -42,4 +42,19 @@ class ControlTest extends AnyFlatSpec with Matchers {
42
42
43
43
trail.get shouldBe Vector (" no timeout" , " done" )
44
44
}
45
+
46
+ it should " block a thread indefinitely" in {
47
+ val trail = Trail ()
48
+ supervised {
49
+ fork {
50
+ never
51
+ trail.add(" never happened!" )
52
+ }
53
+
54
+ Thread .sleep(400 )
55
+ trail.add(" done" )
56
+ }
57
+
58
+ trail.get shouldBe Vector (" done" )
59
+ }
45
60
}
Original file line number Diff line number Diff line change 1
1
# Control flow methods
2
2
3
- There are some helper methods which might be useful when writing forked code:
3
+ There are some helper methods which might be useful when writing code using ox's concurrency operators :
4
4
5
5
* ` forever { ... } ` repeatedly evaluates the given code block forever
6
6
* ` repeatWhile { ... } ` repeatedly evaluates the given code block, as long as it returns ` true `
7
+ * ` repeatUntil { ... } ` repeatedly evaluates the given code block, until it returns ` true `
7
8
* ` uninterruptible { ... } ` evaluates the given code block making sure it can't be interrupted
9
+ * ` never ` blocks the current thread indefinitely, until it is interrupted
10
+
11
+ All of these are ` inline ` methods.
You can’t perform that action at this time.
0 commit comments