|
1 | 1 | package ox
|
2 | 2 |
|
3 |
| -/** Use the given resource in the current scope. The resource is allocated using `acquire`, and released after the scope is done using |
4 |
| - * `release`. Releasing is uninterruptible. |
| 3 | +/** Use the given resource in the current concurrency scope. The resource is allocated using `acquire`, and released after the all forks in |
| 4 | + * the scope complete (either successfully or with an error), using `release`. Releasing is uninterruptible. |
5 | 5 | */
|
6 | 6 | def useInScope[T](acquire: => T)(release: T => Unit)(using Ox): T =
|
7 | 7 | val t = acquire
|
8 | 8 | summon[Ox].addFinalizer(() => release(t))
|
9 | 9 | t
|
10 | 10 |
|
| 11 | +/** Use the given resource, which implements [[AutoCloseable]], in the current concurrency scope. The resource is allocated using `acquire`, |
| 12 | + * and released after the all forks in the scope complete (either successfully or with an error), using [[AutoCloseable.close()]]. |
| 13 | + * Releasing is uninterruptible. |
| 14 | + */ |
11 | 15 | def useCloseableInScope[T <: AutoCloseable](c: => T)(using Ox): T = useInScope(c)(_.close())
|
12 | 16 |
|
13 |
| -def useScoped[T, U](acquire: => T)(release: T => Unit)(b: T => U): U = scoped(b(useInScope(acquire)(release))) |
14 |
| -def useScoped[T <: AutoCloseable, U](acquire: => T)(b: T => U): U = scoped(b(useInScope(acquire)(_.close()))) |
| 17 | +/** Release the given resource, by running the `release` code block. Releasing is done after all the forks in the scope complete (either |
| 18 | + * successfully or with an error), but before the current concurrency scope completes. Releasing is uninterruptible. |
| 19 | + */ |
| 20 | +def releaseAfterScope(release: => Unit)(using Ox): Unit = useInScope(())(_ => release) |
15 | 21 |
|
16 |
| -def useSupervised[T, U](acquire: => T)(release: T => Unit)(b: T => U): U = supervised(b(useInScope(acquire)(release))) |
17 |
| -def useSupervised[T <: AutoCloseable, U](acquire: => T)(b: T => U): U = supervised(b(useInScope(acquire)(_.close()))) |
| 22 | +/** Release the given resource, which implements [[AutoCloseable]], by running its `.close()` method. Releasing is done after all the forks |
| 23 | + * in the scope complete (either successfully or with an error), but before the current concurrency scope completes. Releasing is |
| 24 | + * uninterruptible. |
| 25 | + */ |
| 26 | +def releaseCloseableAfterScope(toRelease: AutoCloseable)(using Ox): Unit = useInScope(())(_ => toRelease.close()) |
0 commit comments