Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add never, make control flow methods inline #103

Merged
merged 3 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions core/src/main/scala/ox/control.scala
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package ox

def forever(f: => Unit): Nothing =
import java.util.concurrent.locks.LockSupport

inline def forever(f: => Unit): Nothing =
while true do f
throw new RuntimeException("can't get here")

/** Repeat evaluating `f` while it evaluates to `true`. */
def repeatWhile(f: => Boolean): Unit =
inline def repeatWhile(f: => Boolean): Unit =
var loop = true
while loop do loop = f

/** Repeat evaluating `f` until it evaluates to `true`. */
def repeatUntil(f: => Boolean): Unit =
inline def repeatUntil(f: => Boolean): Unit =
var loop = true
while loop do loop = !f

def uninterruptible[T](f: => T): T =
inline def uninterruptible[T](f: => T): T =
scoped {
val t = fork(f)

Expand All @@ -27,3 +29,9 @@ def uninterruptible[T](f: => T): T =

joinDespiteInterrupted
}

/** Blocks the current thread indefinitely, until it is interrupted. */
inline def never: Nothing = forever {
LockSupport.park()
if Thread.interrupted() then throw new InterruptedException()
}
15 changes: 15 additions & 0 deletions core/src/test/scala/ox/ControlTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,19 @@ class ControlTest extends AnyFlatSpec with Matchers {

trail.get shouldBe Vector("no timeout", "done")
}

it should "block a thread indefinitely" in {
val trail = Trail()
supervised {
fork {
never
trail.add("never happened!")
}

Thread.sleep(400)
trail.add("done")
}

trail.get shouldBe Vector("done")
}
}
6 changes: 5 additions & 1 deletion doc/control-flow.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# Control flow methods

There are some helper methods which might be useful when writing forked code:
There are some helper methods which might be useful when writing code using ox's concurrency operators:

* `forever { ... }` repeatedly evaluates the given code block forever
* `repeatWhile { ... }` repeatedly evaluates the given code block, as long as it returns `true`
* `repeatUntil { ... }` repeatedly evaluates the given code block, until it returns `true`
* `uninterruptible { ... }` evaluates the given code block making sure it can't be interrupted
* `never` blocks the current thread indefinitely, until it is interrupted

All of these are `inline` methods.
Loading