Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(308)

Unified Diff: item.go

Issue 1410743011: Fix race in item too. (Closed) Base URL: https://chromium.googlesource.com/external/github.com/luci/gkvlite.git@master
Patch Set: Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: item.go
diff --git a/item.go b/item.go
index 7fa580523ba50c0b6972dc17b7c23c075fe93f33..fd80b4e5e0082a57b6775c55f3dba136d2a05123 100644
--- a/item.go
+++ b/item.go
@@ -15,10 +15,10 @@ type Item struct {
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 *Item) Copy() *Item {
}
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 @@ func (i *itemLoc) Copy(src *itemLoc) {
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
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698