aboutsummaryrefslogtreecommitdiff
path: root/tx.go
diff options
context:
space:
mode:
Diffstat (limited to 'tx.go')
-rw-r--r--tx.go20
1 files changed, 17 insertions, 3 deletions
diff --git a/tx.go b/tx.go
index 123c490..5dd41ae 100644
--- a/tx.go
+++ b/tx.go
@@ -1,9 +1,14 @@
package stm
+import (
+ "sync"
+)
+
// A Tx represents an atomic transaction.
type Tx struct {
reads map[*Var]uint64
writes map[*Var]interface{}
+ cond sync.Cond
}
// Check that none of the logged values have changed since the transaction began.
@@ -25,17 +30,26 @@ func (tx *Tx) commit() {
v.mu.Lock()
v.val = val
v.version++
+ for tx := range v.watchers {
+ tx.cond.Broadcast()
+ }
v.mu.Unlock()
}
}
// wait blocks until another transaction modifies any of the Vars read by tx.
func (tx *Tx) wait() {
- globalCond.L.Lock()
+ globalLock.Lock()
+ for v := range tx.reads {
+ v.watchers[tx] = struct{}{}
+ }
for tx.verify() {
- globalCond.Wait()
+ tx.cond.Wait()
+ }
+ for v := range tx.reads {
+ delete(v.watchers, tx)
}
- globalCond.L.Unlock()
+ globalLock.Unlock()
}
// Get returns the value of v as of the start of the transaction.