|
|
|
@ -36,18 +36,30 @@ func (f *FileLock) openFileForLock() error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (f *FileLock) Lock() error {
|
|
|
|
|
func (f *FileLock) Lock(Exclusive bool) error {
|
|
|
|
|
var lockType int
|
|
|
|
|
if Exclusive {
|
|
|
|
|
lockType = syscall.LOCK_EX
|
|
|
|
|
} else {
|
|
|
|
|
lockType = syscall.LOCK_SH
|
|
|
|
|
}
|
|
|
|
|
if err := f.openFileForLock(); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return syscall.Flock(f.fd, syscall.LOCK_EX)
|
|
|
|
|
return syscall.Flock(f.fd, lockType)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (f *FileLock) LockNoBlocking() error {
|
|
|
|
|
func (f *FileLock) LockNoBlocking(Exclusive bool) error {
|
|
|
|
|
var lockType int
|
|
|
|
|
if Exclusive {
|
|
|
|
|
lockType = syscall.LOCK_EX
|
|
|
|
|
} else {
|
|
|
|
|
lockType = syscall.LOCK_SH
|
|
|
|
|
}
|
|
|
|
|
if err := f.openFileForLock(); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
err := syscall.Flock(f.fd, syscall.LOCK_EX|syscall.LOCK_NB)
|
|
|
|
|
err := syscall.Flock(f.fd, lockType|syscall.LOCK_NB)
|
|
|
|
|
if err != nil {
|
|
|
|
|
syscall.Close(f.fd)
|
|
|
|
|
if err == syscall.EWOULDBLOCK {
|
|
|
|
@ -65,9 +77,9 @@ func (f *FileLock) Unlock() error {
|
|
|
|
|
return syscall.Close(f.fd)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (f *FileLock) LockWithTimeout(tm time.Duration) error {
|
|
|
|
|
func (f *FileLock) LockWithTimeout(tm time.Duration, Exclusive bool) error {
|
|
|
|
|
return stario.StopUntilTimeout(tm, func(tmout chan struct{}) error {
|
|
|
|
|
err := f.Lock()
|
|
|
|
|
err := f.Lock(Exclusive)
|
|
|
|
|
select {
|
|
|
|
|
case <-tmout:
|
|
|
|
|
f.Unlock()
|
|
|
|
|