diff options
| author | Matt Joiner <anacrolix@gmail.com> | 2019-11-05 11:49:42 +1100 |
|---|---|---|
| committer | Matt Joiner <anacrolix@gmail.com> | 2019-11-05 11:49:42 +1100 |
| commit | cc0357f5252666ef67bb8f73f86b47ddbdb245be (patch) | |
| tree | d32209c3cc4696849b79f245f8f2eef45b034b9d | |
| parent | Add expvars (diff) | |
| download | stm-cc0357f5252666ef67bb8f73f86b47ddbdb245be.tar.gz stm-cc0357f5252666ef67bb8f73f86b47ddbdb245be.tar.xz | |
Optimize AtomicSet
| -rw-r--r-- | funcs.go | 6 | ||||
| -rw-r--r-- | tx.go | 7 | ||||
| -rw-r--r-- | var.go | 11 |
3 files changed, 14 insertions, 10 deletions
@@ -69,9 +69,9 @@ func AtomicGet(v *Var) interface{} { // AtomicSet is a helper function that atomically writes a value. func AtomicSet(v *Var, val interface{}) { - Atomically(func(tx *Tx) { - tx.Set(v, val) - }) + v.mu.Lock() + v.changeValue(val) + v.mu.Unlock() } // Compose is a helper function that composes multiple transactions into a @@ -30,13 +30,6 @@ func (tx *Tx) verify() bool { func (tx *Tx) commit() { for v, val := range tx.writes { v.changeValue(val) - v.watchers.Range(func(k, _ interface{}) bool { - tx := k.(*Tx) - tx.mu.Lock() - tx.cond.Broadcast() - tx.mu.Unlock() - return true - }) } } @@ -19,6 +19,17 @@ func (v *Var) loadState() varSnapshot { func (v *Var) changeValue(new interface{}) { version := v.loadState().version v.state.Store(varSnapshot{version: version + 1, val: new}) + v.wakeWatchers() +} + +func (v *Var) wakeWatchers() { + v.watchers.Range(func(k, _ interface{}) bool { + tx := k.(*Tx) + tx.mu.Lock() + tx.cond.Broadcast() + tx.mu.Unlock() + return true + }) } type varSnapshot struct { |
