From eb0cb3584af11eba64818586c209daab4c3c6d8b Mon Sep 17 00:00:00 2001 From: Evan Overman Date: Thu, 12 Oct 2023 22:22:18 -0700 Subject: [PATCH] use atomic bool --- src/main/java/frc/robot/subsystems/WriteLock.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/main/java/frc/robot/subsystems/WriteLock.java b/src/main/java/frc/robot/subsystems/WriteLock.java index 7eb1218..1889f6e 100644 --- a/src/main/java/frc/robot/subsystems/WriteLock.java +++ b/src/main/java/frc/robot/subsystems/WriteLock.java @@ -1,5 +1,7 @@ package frc.robot.subsystems; +import java.util.concurrent.atomic.AtomicBoolean; + /** * A write lock to prevent race conditions. This lock does not actually * guarantee the memory safety of the contained object, each method call @@ -7,7 +9,7 @@ */ public class WriteLock { private T obj; - private boolean locked; + private AtomicBoolean locked; /** * Creates a new lock around the given object. The given object mey not be used @@ -22,8 +24,8 @@ public WriteLock(T obj) { * Blockes the current thread until the object is made available. */ public T lock() { - while (locked == true) {} - locked = true; + while (locked.get()) {} + locked.set(true); return obj; } @@ -31,7 +33,7 @@ public T lock() { * Unocks the object, it should not be used after this call. */ public void unlock() { - locked = false; + locked.set(false); } /** @@ -40,6 +42,6 @@ public void unlock() { */ public void unlock(T obj) { this.obj = obj; - locked = false; + locked.set(false); } }