Mutex changes

This commit is contained in:
Alex Yatskov 2015-06-22 19:22:08 +09:00
parent b50c09f74a
commit 338e01223c
2 changed files with 27 additions and 22 deletions

34
dir.go
View File

@ -49,12 +49,14 @@ type verDir struct {
func newVerDir(node *verNode, parent *verDir) *verDir { func newVerDir(node *verNode, parent *verDir) *verDir {
dirs := make(map[string]*verDir) dirs := make(map[string]*verDir)
files := make(map[string]*verFile) files := make(map[string]*verFile)
mutex := sync.Mutex{}
return &verDir{dirs, files, node, allocInode(), parent, mutex} return &verDir{dirs, files, node, allocInode(), parent, sync.Mutex{}}
} }
func (vd *verDir) version() error { func (vd *verDir) version() error {
vd.mutex.Lock()
defer vd.mutex.Unlock()
if vd.node.flags&NodeFlagNew == NodeFlagNew { if vd.node.flags&NodeFlagNew == NodeFlagNew {
return nil return nil
} }
@ -64,8 +66,8 @@ func (vd *verDir) version() error {
return err return err
} }
node.ver.meta.modifyNode(node.path)
vd.node = node vd.node = node
node.ver.meta.modifyNode(node.path)
return nil return nil
} }
@ -82,9 +84,12 @@ func (vd *verDir) createDir(name string) (*verDir, error) {
node := newVerNode(childPath, vd.node.ver, nil, NodeFlagDir|NodeFlagNew) node := newVerNode(childPath, vd.node.ver, nil, NodeFlagDir|NodeFlagNew)
dir := newVerDir(node, vd) dir := newVerDir(node, vd)
vd.dirs[name] = dir
node.ver.meta.createNode(node.path)
vd.mutex.Lock()
vd.dirs[name] = dir
vd.mutex.Unlock()
node.ver.meta.createNode(node.path)
return dir, nil return dir, nil
} }
@ -102,9 +107,11 @@ func (vd *verDir) createFile(name string, flags fuse.OpenFlags, mode os.FileMode
return nil, nil, 0, err return nil, nil, 0, err
} }
vd.mutex.Lock()
vd.files[name] = file vd.files[name] = file
node.ver.meta.createNode(node.path) vd.mutex.Unlock()
node.ver.meta.createNode(node.path)
return file, handle, id, nil return file, handle, id, nil
} }
@ -122,7 +129,10 @@ func (vd *verDir) removeDir(name string) error {
vd.node.ver.meta.removeNode(node.path) vd.node.ver.meta.removeNode(node.path)
} }
vd.mutex.Lock()
delete(vd.dirs, name) delete(vd.dirs, name)
vd.mutex.Unlock()
return nil return nil
} }
@ -140,7 +150,10 @@ func (vd *verDir) removeFile(name string) error {
vd.node.ver.meta.removeNode(node.path) vd.node.ver.meta.removeNode(node.path)
} }
vd.mutex.Lock()
delete(vd.files, name) delete(vd.files, name)
vd.mutex.Unlock()
return nil return nil
} }
@ -167,9 +180,6 @@ func (vd *verDir) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *f
// NodeCreater // NodeCreater
func (vd *verDir) Create(ctx context.Context, req *fuse.CreateRequest, resp *fuse.CreateResponse) (node fs.Node, handle fs.Handle, err error) { func (vd *verDir) Create(ctx context.Context, req *fuse.CreateRequest, resp *fuse.CreateResponse) (node fs.Node, handle fs.Handle, err error) {
vd.mutex.Lock()
defer vd.mutex.Unlock()
if req.Mode.IsDir() { if req.Mode.IsDir() {
node, err = vd.createDir(req.Name) node, err = vd.createDir(req.Name)
handle = node handle = node
@ -184,17 +194,11 @@ func (vd *verDir) Create(ctx context.Context, req *fuse.CreateRequest, resp *fus
// NodeMkdirer // NodeMkdirer
func (vd *verDir) Mkdir(ctx context.Context, req *fuse.MkdirRequest) (fs.Node, error) { func (vd *verDir) Mkdir(ctx context.Context, req *fuse.MkdirRequest) (fs.Node, error) {
vd.mutex.Lock()
defer vd.mutex.Unlock()
return vd.createDir(req.Name) return vd.createDir(req.Name)
} }
// NodeRemover // NodeRemover
func (vd *verDir) Remove(ctx context.Context, req *fuse.RemoveRequest) error { func (vd *verDir) Remove(ctx context.Context, req *fuse.RemoveRequest) error {
vd.mutex.Lock()
defer vd.mutex.Unlock()
if req.Dir { if req.Dir {
return vd.removeDir(req.Name) return vd.removeDir(req.Name)
} else { } else {

15
file.go
View File

@ -46,12 +46,13 @@ type verFile struct {
} }
func newVerFile(node *verNode, parent *verDir) *verFile { func newVerFile(node *verNode, parent *verDir) *verFile {
handles := make(handleMap) return &verFile{node, allocInode(), parent, make(handleMap), sync.Mutex{}}
mutex := sync.Mutex{}
return &verFile{node, allocInode(), parent, handles, mutex}
} }
func (vf *verFile) version() error { func (vf *verFile) version() error {
vf.mutex.Lock()
defer vf.mutex.Unlock()
if vf.node.flags&NodeFlagNew == NodeFlagNew { if vf.node.flags&NodeFlagNew == NodeFlagNew {
return nil return nil
} }
@ -61,16 +62,13 @@ func (vf *verFile) version() error {
return err return err
} }
node.ver.meta.modifyNode(node.path)
vf.node = node vf.node = node
node.ver.meta.modifyNode(node.path)
return nil return nil
} }
func (vf *verFile) open(flags fuse.OpenFlags, mode os.FileMode) (*verFileHandle, fuse.HandleID, error) { func (vf *verFile) open(flags fuse.OpenFlags, mode os.FileMode) (*verFileHandle, fuse.HandleID, error) {
vf.mutex.Lock()
defer vf.mutex.Unlock()
if !flags.IsReadOnly() { if !flags.IsReadOnly() {
if err := vf.version(); err != nil { if err := vf.version(); err != nil {
return nil, 0, err return nil, 0, err
@ -86,7 +84,10 @@ func (vf *verFile) open(flags fuse.OpenFlags, mode os.FileMode) (*verFileHandle,
id := allocHandleId() id := allocHandleId()
verHandle := &verFileHandle{vf, path, handle} verHandle := &verFileHandle{vf, path, handle}
vf.mutex.Lock()
vf.handles[id] = verHandle vf.handles[id] = verHandle
vf.mutex.Unlock()
return verHandle, id, nil return verHandle, id, nil
} }