Fix race in item too.
R=vadimsh@chromium.org
BUG=
Review URL: https://chromiumcodereview.appspot.com/1410743011 .
diff --git a/item.go b/item.go
index 7fa5805..fd80b4e 100644
--- a/item.go
+++ b/item.go
@@ -15,10 +15,10 @@
Priority int32 // Use rand.Int31() for probabilistic balancing.
}
+var itemLocGL = sync.RWMutex{}
+
// A persistable item and its persistence location.
type itemLoc struct {
- m sync.Mutex
-
loc *ploc // can be nil if item is dirty (not yet persisted).
item *Item // can be nil if item is not fetched into memory yet.
}
@@ -49,26 +49,26 @@
}
func (i *itemLoc) Loc() *ploc {
- i.m.Lock()
- defer i.m.Unlock()
+ itemLocGL.RLock()
+ defer itemLocGL.RUnlock()
return i.loc
}
func (i *itemLoc) setLoc(n *ploc) {
- i.m.Lock()
- defer i.m.Unlock()
+ itemLocGL.Lock()
+ defer itemLocGL.Unlock()
i.loc = n
}
func (i *itemLoc) Item() *Item {
- i.m.Lock()
- defer i.m.Unlock()
+ itemLocGL.RLock()
+ defer itemLocGL.RUnlock()
return i.item
}
func (i *itemLoc) casItem(o, n *Item) bool {
- i.m.Lock()
- defer i.m.Unlock()
+ itemLocGL.Lock()
+ defer itemLocGL.Unlock()
if i.item == o {
i.item = n
return true
@@ -81,13 +81,13 @@
i.Copy(empty_itemLoc)
return
}
- newloc := src.Loc()
- newitem := src.Item()
- i.m.Lock()
- defer i.m.Unlock()
- i.loc = newloc
- i.item = newitem
+ itemLocGL.Lock()
+ defer itemLocGL.Unlock()
+ // NOTE: This trick only works because of the global lock. No reason to lock
+ // src independently of i.
+ i.loc = src.loc
+ i.item = src.item
}
const itemLoc_hdrLength int = 4 + 4 + 4 + 4