Skip to content

Commit

Permalink
fixes based on PR comments
Browse files Browse the repository at this point in the history
Signed-off-by: Mike Norgate <[email protected]>
  • Loading branch information
Mike Norgate committed Jul 11, 2023
1 parent 91d0fbf commit cfa60ea
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 23 deletions.
7 changes: 7 additions & 0 deletions pkg/lockfile/lastwrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
cryptorand "crypto/rand"
"encoding/binary"
"os"
"sync"
"sync/atomic"
"time"
)
Expand All @@ -19,6 +20,12 @@ type LastWrite struct {
state []byte // Contents of the lock file.
}

var (
lockFiles map[string]*LockFile
lockFilesLock sync.Mutex
lastWriterIDCounter uint64 // Private state for newLastWriterID
)

const lastWriterIDSize = 64 // This must be the same as len(stringid.GenerateRandomID)
// newLastWrite returns a new "last write" ID.
// The value must be different on every call, and also differ from values
Expand Down
18 changes: 5 additions & 13 deletions pkg/lockfile/lockfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,6 @@ type LockFile struct {
fd fileHandle
}

var (
lockFiles map[string]*LockFile
lockFilesLock sync.Mutex
lastWriterIDCounter uint64 // Private state for newLastWriterID
)

// GetLockFile opens a read-write lock file, creating it if necessary. The
// *LockFile object may already be locked if the path has already been requested
// by the current process.
Expand Down Expand Up @@ -130,13 +124,13 @@ func (l *LockFile) Lock() {
if l.ro {
panic("can't take write lock on read-only lock file")
} else {
l.lock(false)
l.lock(writeLock)
}
}

// LockRead locks the lockfile as a reader.
func (l *LockFile) RLock() {
l.lock(true)
l.lock(readLock)
}

// Unlock unlocks the lockfile.
Expand Down Expand Up @@ -383,10 +377,8 @@ func openLock(path string, ro bool) (fd fileHandle, err error) {

// lock locks the lockfile via syscall based on the specified type and
// command.
func (l *LockFile) lock(ro bool) {
lType := writeLock
if ro {
lType = readLock
func (l *LockFile) lock(lType lockType) {
if lType == readLock {
l.rwMutex.RLock()
} else {
l.rwMutex.Lock()
Expand All @@ -404,7 +396,7 @@ func (l *LockFile) lock(ro bool) {
// Optimization: only use the (expensive) syscall when
// the counter is 0. In this case, we're either the first
// reader lock or a writer lock.
lockHandle(l.fd, ro)
lockHandle(l.fd, lType)
}
l.lockType = lType
l.locked = true
Expand Down
10 changes: 5 additions & 5 deletions pkg/lockfile/lockfile_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ func openHandle(path string, mode int) (fileHandle, error) {
return fileHandle(fd), err
}

func lockHandle(fd fileHandle, ro bool) {
lType := unix.F_RDLCK
if !ro {
lType = unix.F_WRLCK
func lockHandle(fd fileHandle, lType lockType) {
fType := unix.F_RDLCK
if lType != readLock {
fType = unix.F_WRLCK
}
lk := unix.Flock_t{
Type: int16(lType),
Type: int16(fType),
Whence: int16(unix.SEEK_SET),
Start: 0,
Len: 0,
Expand Down
10 changes: 5 additions & 5 deletions pkg/lockfile/lockfile_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,13 @@ func openHandle(path string, mode int) (fileHandle, error) {
return fileHandle(fd), err
}

func lockHandle(fd fileHandle, ro bool) {
lType := 0
if !ro {
lType = windows.LOCKFILE_EXCLUSIVE_LOCK
func lockHandle(fd fileHandle, lType lockType) {
flags := 0
if lType != readLock {
flags = windows.LOCKFILE_EXCLUSIVE_LOCK
}
ol := new(windows.Overlapped)
if err := windows.LockFileEx(windows.Handle(fd), uint32(lType), reserved, allBytes, allBytes, ol); err != nil {
if err := windows.LockFileEx(windows.Handle(fd), uint32(flags), reserved, allBytes, allBytes, ol); err != nil {
panic(err)
}
}
Expand Down

0 comments on commit cfa60ea

Please sign in to comment.