Skip to content

Commit

Permalink
use sync.map is better (#1145)
Browse files Browse the repository at this point in the history
* use sync.map is better

* Use LoadOrStore
  • Loading branch information
liu-song authored Nov 4, 2021
1 parent c15e642 commit d613502
Showing 1 changed file with 4 additions and 12 deletions.
16 changes: 4 additions & 12 deletions fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1370,18 +1370,10 @@ func fsModTime(t time.Time) time.Time {
return t.In(time.UTC).Truncate(time.Second)
}

var (
filesLockMap = make(map[string]*sync.Mutex)
filesLockMapLock sync.Mutex
)
var filesLockMap sync.Map

func getFileLock(absPath string) *sync.Mutex {
filesLockMapLock.Lock()
flock := filesLockMap[absPath]
if flock == nil {
flock = &sync.Mutex{}
filesLockMap[absPath] = flock
}
filesLockMapLock.Unlock()
return flock
v, _ := filesLockMap.LoadOrStore(absPath,&sync.Mutex{})
filelock:=v.(*sync.Mutex)
return filelock
}

0 comments on commit d613502

Please sign in to comment.