39

A saw this on Mastodon, and found it interesting. Rust already prevents a lot of race conditions, but deadlocks when using a mutex is still possible (and I have actually had one myself, though I caught it during testing). So, it would be nice if it would be possible to catch these cases at compile time. Now, seems to be just a proof of concept, but it is always nice to see the direction people are going and what areas are explored.

you are viewing a single comment's thread
view the rest of the comments
[-] sugar_in_your_tea@sh.itjust.works 2 points 6 months ago* (last edited 6 months ago)

Every situation is different, but I've solved similar problems in the past with a work queue. So things like writes to a database are queued in a central worker task that has more knowledge about lock orders and can do bulk operations to reduce the time locks are needed, resulting in greater throughput at the risk of data loss if there's a crash. We did this mostly for write performance on an embedded product (we used SD cards with bursty writes).

So applying this to the more general problem of mutexed, I wonder if having a single thread manage locks would work. I'm thinking that single thread would store all locks as a bit field, and threads would interact with it like this:

  1. Thread calls Lock(locks_needed)
  2. Lock creates a future that unlocks when all of the requested resources are available, with two timeouts: wait and error
  3. The future checks the bit field if all locks are available; if the wait timeout is reached, the future is moved to a priority list, which pauses all other futures from locking the resource until this future resolves
  4. If the error timeout is reached, the future resolves with that error

This has quite a bit more overhead (perhaps it could be largely eliminated in the happy path case with atomic operations), but it should resolve the livelock issue since it degrades to a priority queue. It could work even better in an async context since you wouldn't need a separate async loop (have an AsyncLock entry point as well).

this post was submitted on 13 Mar 2024
39 points (97.6% liked)

Rust

5771 readers
29 users here now

Welcome to the Rust community! This is a place to discuss about the Rust programming language.

Wormhole

!performance@programming.dev

Credits

  • The icon is a modified version of the official rust logo (changing the colors to a gradient and black background)

founded 1 year ago
MODERATORS