Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mutex lock on write-to-file function #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion blockService.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func uint64FromBytes(b []byte) uint64 {

type blockService struct {
file *os.File
mu *sync.Mutex
}

func (bs *blockService) getLatestBlockID() (int64, error) {
Expand Down Expand Up @@ -165,6 +166,8 @@ func (bs *blockService) newBlock() (*diskBlock, error) {
}

func (bs *blockService) writeBlockToDisk(block *diskBlock) error {
bs.mu.Lock()
defer bs.mu.Unlock()
seekOffset := blockSize * block.id
blockBuffer := bs.getBufferFromBlock(block)
_, err := bs.file.Seek(int64(seekOffset), 0)
Expand Down Expand Up @@ -240,7 +243,7 @@ func (bs *blockService) updateRootNode(n *DiskNode) error {
}

func newBlockService(file *os.File) *blockService {
return &blockService{file}
return &blockService{file: file, mu: &sync.Mutex{}}
}

func (bs *blockService) rootBlockExists() bool {
Expand Down