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

Request: Allow Mutex/Semaphore to be instantiated in a locked state #156

Open
bjeanes opened this issue Sep 18, 2018 · 1 comment
Open

Comments

@bjeanes
Copy link

bjeanes commented Sep 18, 2018

I have a use case where I'm taking an exclusive lock in one process but releasing it in another. It's a bit odd, I know...

Some other libraries, like Suo, have a notion of a "lock token" that is returned when successfully locked which can be used to unlock from anywhere.

It's harder to see how this would work with the Semaphore as it is solely backed by a counter, but at least for the Mutex this could look something like:

  1. When locking, set the value to something random (e.g. SecureRandom.uuid)
  2. Return the random value on successful lock
  3. Allow the lock token to be passed to unlock/leave in place of the check against @locked to allow the unlock to proceed if its a match for the lock value.

I'm not 100% sure this can be done atomically with the existing interface, without being able to conditionally update/delete when the value is a known value (compare-and-set), but I don't know the interface semantics and guarantees well enough.

Perhaps, a feature that could be added is "swap"? E.g. #swap(key, old_val, new_val) #=> true|false / old_val|new_val for compare-and-swap. If this isn't possible today, I can add a feature request for it...

@bjeanes
Copy link
Author

bjeanes commented Sep 18, 2018

(Adding a swap may not work well when using "emulated" expiry though, as you'd potentially have to compare with the same expiry to successfully swap atomically)

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

No branches or pull requests

1 participant