HappyLock 0.3: Deadlock-free mutexes
Hello. A few months ago I posted a link to HappyLock, a library I made which uses the borrow checker to prevent deadlocks. Although most of the checking can be done at compile-time, there was an O(n^2) runtime check to ensure that lock collections didn't contain duplicate locks. There was also the possibility of livelock due to releasing and reobtaining mutexes. In the comments, several of you made suggestions on how to improve this. I took those ideas and published HappyLock 0.3.
I created a blog, and a blog post explaining how HappyLock works and what I've changed. Ignore the fact that the CSS doesn't exist right now: https://www.botahamec.dev/blog/how-happylock-works.html
Here's a short list of changes
The
Lockabletrait has been replaced with aLocktrait and a newLockabletrait.Lockis a bit likeRawRwLockandLockabletranslates that into something that can have a guard.These new traits don't have lifetimes
Four new lock collection types exist, replacing the old ones. Each new collection type has different performance characteristics.
OwnedLockCollectionis free, but only accepts anOwnedLockabletype.RefLockCollectiondoes the sorting of lock pointers, but is a reference.BoxedLockCollectionboxes the data, so it doesn't need a lifetime.RetryingLockCollectionis the equivalent of the oldLockCollection. The default lock collection isBoxedLockCollection, for which there is a type alias at the top of the crate.A new
Sharabletrait has been added as a subtrait ofLockable. If a lock collection only contains rwlocks, then the lock collection will have areadmethod.New trait implementations and methods for locking types
I don't love making a new post for an update to a small crate, but I think the ideas here have a lot of potential. It's cool that in addition to all of the memory unsafety issues that Rust already prevents, Rust can also prevent deadlocks if the libraries are designed around it.
Edit: bulleted list formatting