-
Couldn't load subscription status.
- Fork 1.4k
[9390] - add tryAcquire to Semaphore #9825
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -108,6 +108,43 @@ final class TSemaphore private (val permits: TRef[Long]) extends Serializable { | |||||||
| permits.unsafeSet(journal, current + n) | ||||||||
| } | ||||||||
|
|
||||||||
| /** | ||||||||
| * Tries to acquire a single permit in a transactional context. Returns `true` | ||||||||
| * if the permit was acquired, otherwise `false`. | ||||||||
| */ | ||||||||
| def tryAcquire: USTM[Boolean] = tryAcquireN(1L) | ||||||||
|
|
||||||||
| /** | ||||||||
| * Tries to acquire the specified number of permits in a transactional | ||||||||
| * context. Returns `true` if the permits were acquired, otherwise `false`. | ||||||||
| */ | ||||||||
| def tryAcquireN(n: Long): USTM[Boolean] = | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| ZSTM.Effect { (journal, _, _) => | ||||||||
| assertNonNegative(n) | ||||||||
|
|
||||||||
| val available: Long = permits.unsafeGet(journal) | ||||||||
| if (available >= n) { | ||||||||
| permits.unsafeSet(journal, available - n) | ||||||||
| true | ||||||||
| } else false | ||||||||
| } | ||||||||
|
|
||||||||
| /** | ||||||||
| * Executes the specified effect, acquiring `1` permit if available and | ||||||||
| * releasing them after execution. Returns `None` if no permits were | ||||||||
| * available. | ||||||||
| */ | ||||||||
| def tryWithPermit[R, E, A](zio: ZIO[R, E, A])(implicit trace: Trace): ZIO[R, E, Option[A]] = | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @hearnadam TSemaphore is a final class, hence all member of the class are effectively final by default. that is why There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah right, thanks |
||||||||
| tryWithPermits(1L)(zio) | ||||||||
|
|
||||||||
| /** | ||||||||
| * Executes the specified effect, acquiring `n` permits if available and | ||||||||
| * releasing them after execution. Returns `None` if no permits were | ||||||||
| * available. | ||||||||
| */ | ||||||||
| def tryWithPermits[R, E, A](n: Long)(zio: ZIO[R, E, A])(implicit trace: Trace): ZIO[R, E, Option[A]] = | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| ZSTM.acquireReleaseWith(tryAcquireN(n))(releaseN(n).commit.whenDiscard(_))(zio.when(_)) | ||||||||
|
|
||||||||
| /** | ||||||||
| * Executes the specified effect, acquiring a permit immediately before the | ||||||||
| * effect begins execution and releasing it immediately after the effect | ||||||||
|
|
||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.