Skip to content

Commit

Permalink
Replaced atomic package uses with mutexes
Browse files Browse the repository at this point in the history
  • Loading branch information
mangoslicer committed Jul 19, 2017
1 parent 66f2cf2 commit 6f0b938
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
21 changes: 10 additions & 11 deletions clientv3/ordering/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package ordering
import (
"context"
"sync"
"sync/atomic"

"github.com/coreos/etcd/clientv3"
)
Expand All @@ -29,25 +28,25 @@ type kvOrdering struct {
clientv3.KV
orderViolationFunc OrderViolationFunc
prevRev int64
revMu sync.RWMutex
}

func NewKV(kv clientv3.KV, orderViolationFunc OrderViolationFunc) *kvOrdering {
return &kvOrdering{kv, orderViolationFunc, 0}
return &kvOrdering{kv, orderViolationFunc, 0, sync.RWMutex{}}
}

func (kv *kvOrdering) getPrevRev() int64 {
return atomic.LoadInt64(&kv.prevRev)
kv.revMu.RLock()
defer kv.revMu.RUnlock()
return kv.prevRev
}

func (kv *kvOrdering) setPrevRev(currRev int64) {
for {
prevRev := kv.getPrevRev()
if prevRev > currRev {
return
}
if atomic.CompareAndSwapInt64(&kv.prevRev, prevRev, currRev) {
return
}
prevRev := kv.getPrevRev()
kv.revMu.Lock()
defer kv.revMu.Unlock()
if currRev > prevRev {
kv.prevRev = currRev
}
}

Expand Down
2 changes: 2 additions & 0 deletions clientv3/ordering/kv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ func TestKvOrdering(t *testing.T) {
}
}(tt.response),
tt.prevRev,
sync.RWMutex{},
}
res, err := kv.Get(nil, "mockKey")
if err != nil {
Expand Down Expand Up @@ -267,6 +268,7 @@ func TestTxnOrdering(t *testing.T) {
}
}(tt.response),
tt.prevRev,
sync.RWMutex{},
}
txn := &txnOrdering{
kv.Txn(context.Background()),
Expand Down

0 comments on commit 6f0b938

Please sign in to comment.