Skip to content

simplify lock/semaphore - #718

Closed
arnetheduck wants to merge 1 commit into
masterfrom
tryacq
Closed

simplify lock/semaphore#718
arnetheduck wants to merge 1 commit into
masterfrom
tryacq

Conversation

@arnetheduck

Copy link
Copy Markdown
Member

AsyncLock is a special case of AsyncSemaphore (with size 1) - make sure that they both behave the same.

In particular, simpify the AsyncLock implementation to use fewer poll rounds and futures during acquire while maintaining the same fairness guarantees as before.

Also, add tryAcquire and tryRelease which are exception-free variants of the existing functions - in particular, release is annoying to use in finally with raises: [] since it then requires an exception handler that never gets triggered.

  • release cancelled futures during acquire as well as release.

`AsyncLock` is a special case of `AsyncSemaphore` (with size 1) - make
sure that they both behave the same.

In particular, simpify the `AsyncLock` implementation to use fewer poll
rounds and futures during `acquire` while maintaining the same fairness
guarantees as before.

Also, add `tryAcquire` and `tryRelease` which are exception-free
variants of the existing functions - in particular, `release` is
annoying to use in `finally` with `raises: []` since it then requires an
exception handler that never gets triggered.

* release cancelled futures during `acquire` as well as `release`.
@arnetheduck

Copy link
Copy Markdown
Member Author

The alternative to adding tryRelease is to remove the exception from release and turn it into a Defect - this is a breaking change however which makes things a bit messy.

Comment thread chronos/asyncsync.nim

fut

proc tryRelease*(lock: AsyncLock): bool =

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
proc tryRelease*(lock: AsyncLock): bool =
proc tryRelease*(lock: AsyncLock): bool {.discardable.} =

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But in a way, discarding it isn't safe either. If there are logic bugs in the code, it just hides them...

@arnetheduck

Copy link
Copy Markdown
Member Author

Going with #721

@arnetheduck
arnetheduck deleted the tryacq branch August 31, 2026 07:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants