commit | e3a74b5a3c624273975a41d5586e490de241c311 | [log] [tgz] |
---|---|---|
author | James Farrell <[email protected]> | Wed Sep 18 20:37:03 2024 +0000 |
committer | James Farrell <[email protected]> | Wed Sep 18 20:37:03 2024 +0000 |
tree | bd7116c534d29fd5c9137e2ee96c318b196f9131 | |
parent | 4f8edd77ad13ed60ff0e187d864087b51e07b68a [diff] |
Migrate 26 crates to monorepo tokio-util tower tower-layer tower-service tracing tracing-attributes tracing-core tracing-subscriber try-lock tungstenite twox-hash ucd-trie unicode-bidi unicode-normalization unicode-segmentation unicode-width unsafe-libyaml userfaultfd utf-8 uuid weak-table webpki which winnow x509-cert xml-rs Bug: http://b/339424309 Test: treehugger Change-Id: If0bc8bc6c4c512a4b741eef1c14618e08d2fd567
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");