-
Notifications
You must be signed in to change notification settings - Fork 87
Description
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:
- When locking, set the value to something random (e.g.
SecureRandom.uuid
) - Return the random value on successful lock
- 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...