1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-11-27 09:12:53 +00:00

Moreatomic: Fixed upgradable mutex usage

This commit is contained in:
diamondburned 2020-12-18 23:57:56 -08:00
parent 3ddb472644
commit 36c2f166be
2 changed files with 9 additions and 3 deletions

2
go.mod
View file

@ -5,7 +5,7 @@ go 1.13
require (
github.com/gorilla/schema v1.2.0
github.com/gorilla/websocket v1.4.2
github.com/kawasin73/umutex v0.2.1 // indirect
github.com/kawasin73/umutex v0.2.1
github.com/pkg/errors v0.9.1
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897
golang.org/x/sys v0.0.0-20201018230417-eeed37f84f13 // indirect

View file

@ -34,16 +34,22 @@ func (sm *Map) Reset() error {
// given constructor then return that value.
func (sm *Map) LoadOrStore(k interface{}) (lv interface{}, loaded bool) {
sm.upmu.RLock()
defer sm.upmu.RUnlock()
lv, loaded = sm.smap[k]
if !loaded {
lv = sm.ctor()
sm.upmu.Upgrade()
// Wait until upgrade succeeds.
for !sm.upmu.Upgrade() {
}
sm.smap[k] = lv
sm.upmu.Unlock()
return
}
sm.upmu.RUnlock()
return
}