Skip to content

Commit

Permalink
mfs: use rwlock for the nodelk
Browse files Browse the repository at this point in the history
This allows us to, e.g., get the size, etc. in parallel.

License: MIT
Signed-off-by: Steven Allen <steven@stebalien.com>
  • Loading branch information
Stebalien committed Dec 21, 2017
1 parent a5ed5f3 commit feafd11
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
4 changes: 2 additions & 2 deletions mfs/fd.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,9 @@ func (fi *fileDescriptor) flushUp(fullsync bool) error {
return nil
}
if nd == nil {
fi.inode.nodelk.Lock()
fi.inode.nodelk.RLock()
nd = fi.inode.node
fi.inode.nodelk.Unlock()
fi.inode.nodelk.RUnlock()
}

if err := fi.inode.parent.closeChild(fi.inode.name, nd, fullsync); err != nil {
Expand Down
15 changes: 8 additions & 7 deletions mfs/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type File struct {

dserv dag.DAGService
node node.Node
nodelk sync.Mutex
nodelk sync.RWMutex

RawLeaves bool
}
Expand Down Expand Up @@ -60,9 +60,10 @@ func (fi *File) Open(flags Flags) (_ FileDescriptor, _retErr error) {
} else {
return nil, fmt.Errorf("file opened for neither reading nor writing")
}
fi.nodelk.Lock()

fi.nodelk.RLock()
node := fi.node
fi.nodelk.Unlock()
fi.nodelk.RUnlock()

switch node := node.(type) {
case *dag.ProtoNode:
Expand Down Expand Up @@ -98,8 +99,8 @@ func (fi *File) Open(flags Flags) (_ FileDescriptor, _retErr error) {

// Size returns the size of this file
func (fi *File) Size() (int64, error) {
fi.nodelk.Lock()
defer fi.nodelk.Unlock()
fi.nodelk.RLock()
defer fi.nodelk.RUnlock()
switch nd := fi.node.(type) {
case *dag.ProtoNode:
pbd, err := ft.FromBytes(nd.Data())
Expand All @@ -116,8 +117,8 @@ func (fi *File) Size() (int64, error) {

// GetNode returns the dag node associated with this file
func (fi *File) GetNode() (node.Node, error) {
fi.nodelk.Lock()
defer fi.nodelk.Unlock()
fi.nodelk.RLock()
defer fi.nodelk.RUnlock()
return fi.node, nil
}

Expand Down

0 comments on commit feafd11

Please sign in to comment.