diff options
author | Matt Joiner <anacrolix@gmail.com> | 2020-09-30 19:13:27 +1000 |
---|---|---|
committer | Matt Joiner <anacrolix@gmail.com> | 2020-09-30 19:13:27 +1000 |
commit | a3e788d52494b75b927538c9c2bfee1573aa50e1 (patch) | |
tree | f1eed1cda0c25008c51b375799b9d65a33ce080d /var.go | |
parent | Don't sleep and only wake watchers if the variable value has changed (diff) | |
download | stm-a3e788d52494b75b927538c9c2bfee1573aa50e1.tar.gz stm-a3e788d52494b75b927538c9c2bfee1573aa50e1.tar.xz |
Wake watchers until the var changes again
Diffstat (limited to 'var.go')
-rw-r--r-- | var.go | 10 |
1 files changed, 6 insertions, 4 deletions
@@ -17,20 +17,22 @@ func (v *Var) changeValue(new interface{}) { newVarValue := old.Set(new) v.value.Store(newVarValue) if old.Changed(newVarValue) { - v.wakeWatchers() + go v.wakeWatchers(newVarValue) } } -func (v *Var) wakeWatchers() { +func (v *Var) wakeWatchers(new VarValue) { 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() + for !tx.waiting && !tx.completed { + tx.cond.Wait() + } tx.mu.Unlock() - return true + return !v.value.Load().(VarValue).Changed(new) }) } |