From 972c8f6970aa226508e6d262b0a1a2e21ea1bcc7 Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Tue, 22 Mar 2022 09:56:35 +0100 Subject: [PATCH] lock :D --- src/mutex.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/mutex.rs b/src/mutex.rs index f52701b..abd5e9a 100644 --- a/src/mutex.rs +++ b/src/mutex.rs @@ -28,6 +28,22 @@ impl Mutex { None } } + + pub fn lock(&self) -> MutexGuard<'_, T> { + // hahahahahahahaha a spin loop :D + // don't use spin loops + // but I can't be bothered with the proper solution + loop { + if self + .status + .compare_exchange(INIT, ACQUIRED, Ordering::Acquire, Ordering::Relaxed) + { + return MutexGuard { mutex: self }; + } else { + std::hint::spin_loop(); + } + } + } } pub struct MutexGuard<'a, T> {