Post

Also this don’t work for runtime locks. E.g Map<string, Lock<value>>. I came up with the “OrderedLock”. You specify it logically at compile time to fit into the DAG. At runtime each lock has an ordered key. Then it asserts ordered acquisition - which fits within compile time enforced ordering

Seems like surelock should handle runtime locks? Or do you mean the lock-ordering crate? I think @expede.wtf wasn't trying to list all prior art, just the ones that influenced the design.

The LockSet idea is really cool, but misses out on some flexibility. My understanding is you pre-declare the locks you need (LockSet does ordering internally). But it’s common to not know what locks you need upfront. For this the application needs to be in control of ordering.

useful.storeuseful.store

Yes exactly! This is what LockSet accounts for. It acquires multiple locks *atomically* and Surelock handles the ordering under the hood. Lock IDs only have to be stable and unique to avoid deadlocks, but as a tradeoff you must acquire all of the locks at once.

Surelock *additionally* lets you declare order for groups of locks in advance by setting them at a Level<_>. They'll default to Level<1> (you can omit the type param). This coarse mechanism allows you to acquire multiple LockSets incrementally (each LockSet atomically, but then several in sequence)

Not exactly, for _runtime_ ordering within a level, I am suggesting a slight adjustment for a more flexible API. Let the application provide custom ordering - thus (with less safety) now one can acquire locks one at a time with its known order with panic safety. `LockSet` can be a safer API on top.

Interesting approach! Does this mean that users can pick their own lock IDs for fine-grained ordering in addition to having them automatically assigned? Glancing through the code, I think one of the tradeoffs is that lock acquisition is not guaranteed to succeed, is that right?

Yes to user orderings. Since I didn’t think of the LockSet/pre-declaration api, no auto assignment (yet at least). auto assignment isn’t useful without the pre-declaration API. Since app doesn’t know what order to acquire itself.

Its layers of deadlock safety in my mind 1. compile time 2. pre-declaration (e.g. LockSet): guaranteed to succeed at runtime. 3. Then runtime ordering. My thought is fundamentally 2 should be built on 3 - a generic ordering mechanism. To let users have a hatch to (3) when necessary.