diff options
author | Matt Joiner <anacrolix@gmail.com> | 2020-09-30 18:12:44 +1000 |
---|---|---|
committer | Matt Joiner <anacrolix@gmail.com> | 2020-09-30 18:12:44 +1000 |
commit | 47e5cc608cded44f87c3120fd76fa31a9b8a8867 (patch) | |
tree | 24f618b1e37852423b2787325ceb40ff25221fbe /var.go | |
parent | Sleep by default again, and don't bother sleeping for less than 100 microseconds (diff) | |
download | stm-47e5cc608cded44f87c3120fd76fa31a9b8a8867.tar.gz stm-47e5cc608cded44f87c3120fd76fa31a9b8a8867.tar.xz |
Don't sleep and only wake watchers if the variable value has changed
Diffstat (limited to 'var.go')
-rw-r--r-- | var.go | 11 |
1 files changed, 9 insertions, 2 deletions
@@ -13,13 +13,20 @@ type Var struct { } func (v *Var) changeValue(new interface{}) { - v.value.Store(v.value.Load().(VarValue).Set(new)) - v.wakeWatchers() + old := v.value.Load().(VarValue) + newVarValue := old.Set(new) + v.value.Store(newVarValue) + if old.Changed(newVarValue) { + v.wakeWatchers() + } } func (v *Var) wakeWatchers() { v.watchers.Range(func(k, _ interface{}) bool { tx := k.(*Tx) + + // We have to lock here to ensure that the Tx is waiting before we signal it. Otherwise we + // could signal it before it goes to sleep and it will miss the notification. tx.mu.Lock() tx.cond.Broadcast() tx.mu.Unlock() |