madsim-rs / madsim

Magical Deterministic Simulator for distributed systems in Rust.
Apache License 2.0
648 stars 46 forks source link

Replace std::sync::<Lock> with parking_lot lib #44

Open rogercloud opened 2 years ago

rogercloud commented 2 years ago

The main API difference between the two libs is poison detection. Poison detection is useful when panic happens in some thread while others are still runing, but it's not the case in this lib. In a rpc lib, we usually panic when the whole system breaks and can't be recovered, otherwise we should not panic. In another word there's no need to detection mutex poison in madsim.

Apart from poison detection parking_lot locks have better performance in most cases. So switch to parking_lot sync lock is the obvious choice from my view.

wangrunji0408 commented 2 years ago

I have done this in #41 (replacing std lock with spin). In fact, since everything is running on a single thread, we know that there won't be any contention but it's possible to be deadlock. So what we really want is something like RefCell but also Sync. It should panic once try_lock failed.

rogercloud commented 2 years ago

I have done this in #41 (replacing std lock with spin). In fact, since everything is running on a single thread, we know that there won't be any contention but it's possible to be deadlock. So what we really want is something like RefCell but also Sync. It should panic once try_lock failed.

We can't guarantee the single thread scenario as a multi-thread runtime, such as tokio, may be adopted. I guess you mean single thread in sim scenario.

wangrunji0408 commented 2 years ago

Oh yes. I'll do it for std part soon.