remove a race from the hashing write()
Calling load() and store() separately leaves a lot of opportunity for a race to occur. This changes the logic to use compare_exchange() and will only return if the previous value is absolutely not 1 at the point of comparison. I also don't think this needs to be SeqCst, so relaxing that requirement a little.
This commit is contained in:
parent
517f219066
commit
785be453f6
@ -50,9 +50,11 @@ impl HokmaLock {
|
|||||||
|
|
||||||
pub fn write(&'static self) -> WhenTheHokmaSuppression {
|
pub fn write(&'static self) -> WhenTheHokmaSuppression {
|
||||||
loop {
|
loop {
|
||||||
let previous = self.lock.load(Ordering::SeqCst);
|
// We are only interested in error results
|
||||||
self.lock.store(1, Ordering::SeqCst);
|
if let Err(previous) = self
|
||||||
|
.lock
|
||||||
|
.compare_exchange(1, 1, Ordering::Acquire, Ordering::Relaxed)
|
||||||
|
{
|
||||||
if previous != 1 {
|
if previous != 1 {
|
||||||
return WhenTheHokmaSuppression {
|
return WhenTheHokmaSuppression {
|
||||||
hokma: self,
|
hokma: self,
|
||||||
@ -61,6 +63,7 @@ impl HokmaLock {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct WhenTheHokmaSuppression {
|
struct WhenTheHokmaSuppression {
|
||||||
|
Loading…
Reference in New Issue
Block a user