-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify resources allocation, add releaseAfterScope (#102)
- Loading branch information
Showing
4 changed files
with
31 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,26 @@ | ||
package ox | ||
|
||
/** Use the given resource in the current scope. The resource is allocated using `acquire`, and released after the scope is done using | ||
* `release`. Releasing is uninterruptible. | ||
/** Use the given resource in the current concurrency scope. The resource is allocated using `acquire`, and released after the all forks in | ||
* the scope complete (either successfully or with an error), using `release`. Releasing is uninterruptible. | ||
*/ | ||
def useInScope[T](acquire: => T)(release: T => Unit)(using Ox): T = | ||
val t = acquire | ||
summon[Ox].addFinalizer(() => release(t)) | ||
t | ||
|
||
/** Use the given resource, which implements [[AutoCloseable]], in the current concurrency scope. The resource is allocated using `acquire`, | ||
* and released after the all forks in the scope complete (either successfully or with an error), using [[AutoCloseable.close()]]. | ||
* Releasing is uninterruptible. | ||
*/ | ||
def useCloseableInScope[T <: AutoCloseable](c: => T)(using Ox): T = useInScope(c)(_.close()) | ||
|
||
def useScoped[T, U](acquire: => T)(release: T => Unit)(b: T => U): U = scoped(b(useInScope(acquire)(release))) | ||
def useScoped[T <: AutoCloseable, U](acquire: => T)(b: T => U): U = scoped(b(useInScope(acquire)(_.close()))) | ||
/** Release the given resource, by running the `release` code block. Releasing is done after all the forks in the scope complete (either | ||
* successfully or with an error), but before the current concurrency scope completes. Releasing is uninterruptible. | ||
*/ | ||
def releaseAfterScope(release: => Unit)(using Ox): Unit = useInScope(())(_ => release) | ||
|
||
def useSupervised[T, U](acquire: => T)(release: T => Unit)(b: T => U): U = supervised(b(useInScope(acquire)(release))) | ||
def useSupervised[T <: AutoCloseable, U](acquire: => T)(b: T => U): U = supervised(b(useInScope(acquire)(_.close()))) | ||
/** Release the given resource, which implements [[AutoCloseable]], by running its `.close()` method. Releasing is done after all the forks | ||
* in the scope complete (either successfully or with an error), but before the current concurrency scope completes. Releasing is | ||
* uninterruptible. | ||
*/ | ||
def releaseCloseableAfterScope(toRelease: AutoCloseable)(using Ox): Unit = useInScope(())(_ => toRelease.close()) |
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