mirror of https://github.com/golang/go.git
os: fix race condition in readdir by atomically initializing dirinfo
Signed-off-by: Amirhossein Akhlaghpour <m9.akhlaghpoor@gmail.com>
This commit is contained in:
parent
ce7ea0a6a5
commit
eac6fc15cc
|
|
@ -11,12 +11,19 @@ import (
|
|||
)
|
||||
|
||||
func (file *File) readdir(n int, mode readdirMode) (names []string, dirents []DirEntry, infos []FileInfo, err error) {
|
||||
// If this file has no dirinfo, create one.
|
||||
d := file.dirinfo.Load()
|
||||
if d == nil {
|
||||
d = new(dirInfo)
|
||||
file.dirinfo.Store(d)
|
||||
var d *dirInfo
|
||||
for {
|
||||
d = file.dirinfo.Load()
|
||||
if d != nil {
|
||||
break
|
||||
}
|
||||
newD := new(dirInfo)
|
||||
if file.dirinfo.CompareAndSwap(nil, newD) {
|
||||
d = newD
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
d.mu.Lock()
|
||||
defer d.mu.Unlock()
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue