Skip to content

Instantly share code, notes, and snippets.

@reem
Created December 2, 2014 01:58
Show Gist options
  • Save reem/89434a762b9b8b98944f to your computer and use it in GitHub Desktop.
Save reem/89434a762b9b8b98944f to your computer and use it in GitHub Desktop.
pub fn lock(&self) -> Option<OnceMutexGuard<T>> {
match state.load(SeqCst) {
UNUSED => {
state.store(LOCKED, SeqCst);
Some(OnceMutexGuard::new(self))
},
// Other thread got here first or already locked.
// Either way, no lock.
_ => None
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment