commit | 3795546d6b399c429228714316bd33ec2bb2441c | [log] [tgz] |
---|---|---|
author | Frank Piva <[email protected]> | Fri Jun 07 05:40:55 2024 +0000 |
committer | Automerger Merge Worker <[email protected]> | Fri Jun 07 05:40:55 2024 +0000 |
tree | 8076e3d75a836de2bd2d336ecba4c97dff415aa8 | |
parent | 14027f77b92e84fc6207638628ff84d87137d9d6 [diff] | |
parent | 6eee6a40b40af1568f4fa8b2dbcd34cd53a2a464 [diff] |
Merge remote-tracking branch 'origin/upstream' am: 6eee6a40b4 Original change: undetermined Change-Id: I24e5088553113e0722070f32a2d4d94631d171d5 Signed-off-by: Automerger Merge Worker <[email protected]>
A light-weight lock guarded by an atomic boolean.
Most efficient when contention is low, acquiring the lock is a single atomic swap, and releasing it just 1 more atomic swap.
use std::sync::Arc; use try_lock::TryLock; // a thing we want to share struct Widget { name: String, } // lock it up! let widget1 = Arc::new(TryLock::new(Widget { name: "Spanner".into(), })); let widget2 = widget1.clone(); // mutate the widget let mut locked = widget1.try_lock().expect("example isn't locked yet"); locked.name.push_str(" Bundle"); // hands off, buddy let not_locked = widget2.try_lock(); assert!(not_locked.is_none(), "widget1 has the lock"); // ok, you can have it drop(locked); let locked2 = widget2.try_lock().expect("widget1 lock is released"); assert_eq!(locked2.name, "Spanner Bundle");