| 1 |
// Copyright 2015 Tim Heckman. All rights reserved. |
| 2 |
// Use of this source code is governed by the BSD 3-Clause |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package flock |
| 6 |
|
| 7 |
import ( |
| 8 |
"syscall" |
| 9 |
) |
| 10 |
|
| 11 |
// ErrorLockViolation is the error code returned from the Windows syscall when a |
| 12 |
// lock would block and you ask to fail immediately. |
| 13 |
const ErrorLockViolation syscall.Errno = 0x21 // 33 |
| 14 |
|
| 15 |
// Lock is a blocking call to try and take an exclusive file lock. It will wait |
| 16 |
// until it is able to obtain the exclusive file lock. It's recommended that |
| 17 |
// TryLock() be used over this function. This function may block the ability to |
| 18 |
// query the current Locked() or RLocked() status due to a RW-mutex lock. |
| 19 |
// |
| 20 |
// If we are already locked, this function short-circuits and returns |
| 21 |
// immediately assuming it can take the mutex lock. |
| 22 |
func (f *Flock) Lock() error { |
| 23 |
return f.lock(&f.l, winLockfileExclusiveLock) |
| 24 |
} |
| 25 |
|
| 26 |
// RLock is a blocking call to try and take a shared file lock. It will wait |
| 27 |
// until it is able to obtain the shared file lock. It's recommended that |
| 28 |
// TryRLock() be used over this function. This function may block the ability to |
| 29 |
// query the current Locked() or RLocked() status due to a RW-mutex lock. |
| 30 |
// |
| 31 |
// If we are already locked, this function short-circuits and returns |
| 32 |
// immediately assuming it can take the mutex lock. |
| 33 |
func (f *Flock) RLock() error { |
| 34 |
return f.lock(&f.r, winLockfileSharedLock) |
| 35 |
} |
| 36 |
|
| 37 |
func (f *Flock) lock(locked *bool, flag uint32) error { |
| 38 |
f.m.Lock() |
| 39 |
defer f.m.Unlock() |
| 40 |
|
| 41 |
if *locked { |
| 42 |
return nil |
| 43 |
} |
| 44 |
|
| 45 |
if f.fh == nil { |
| 46 |
if err := f.setFh(); err != nil { |
| 47 |
return err |
| 48 |
} |
| 49 |
defer f.ensureFhState() |
| 50 |
} |
| 51 |
|
| 52 |
if _, errNo := lockFileEx(syscall.Handle(f.fh.Fd()), flag, 0, 1, 0, &syscall.Overlapped{}); errNo > 0 { |
| 53 |
return errNo |
| 54 |
} |
| 55 |
|
| 56 |
*locked = true |
| 57 |
return nil |
| 58 |
} |
| 59 |
|
| 60 |
// Unlock is a function to unlock the file. This file takes a RW-mutex lock, so |
| 61 |
// while it is running the Locked() and RLocked() functions will be blocked. |
| 62 |
// |
| 63 |
// This function short-circuits if we are unlocked already. If not, it calls |
| 64 |
// UnlockFileEx() on the file and closes the file descriptor. It does not remove |
| 65 |
// the file from disk. It's up to your application to do. |
| 66 |
func (f *Flock) Unlock() error { |
| 67 |
f.m.Lock() |
| 68 |
defer f.m.Unlock() |
| 69 |
|
| 70 |
// if we aren't locked or if the lockfile instance is nil |
| 71 |
// just return a nil error because we are unlocked |
| 72 |
if (!f.l && !f.r) || f.fh == nil { |
| 73 |
return nil |
| 74 |
} |
| 75 |
|
| 76 |
// mark the file as unlocked |
| 77 |
if _, errNo := unlockFileEx(syscall.Handle(f.fh.Fd()), 0, 1, 0, &syscall.Overlapped{}); errNo > 0 { |
| 78 |
return errNo |
| 79 |
} |
| 80 |
|
| 81 |
f.fh.Close() |
| 82 |
|
| 83 |
f.l = false |
| 84 |
f.r = false |
| 85 |
f.fh = nil |
| 86 |
|
| 87 |
return nil |
| 88 |
} |
| 89 |
|
| 90 |
// TryLock is the preferred function for taking an exclusive file lock. This |
| 91 |
// function does take a RW-mutex lock before it tries to lock the file, so there |
| 92 |
// is the possibility that this function may block for a short time if another |
| 93 |
// goroutine is trying to take any action. |
| 94 |
// |
| 95 |
// The actual file lock is non-blocking. If we are unable to get the exclusive |
| 96 |
// file lock, the function will return false instead of waiting for the lock. If |
| 97 |
// we get the lock, we also set the *Flock instance as being exclusive-locked. |
| 98 |
func (f *Flock) TryLock() (bool, error) { |
| 99 |
return f.try(&f.l, winLockfileExclusiveLock) |
| 100 |
} |
| 101 |
|
| 102 |
// TryRLock is the preferred function for taking a shared file lock. This |
| 103 |
// function does take a RW-mutex lock before it tries to lock the file, so there |
| 104 |
// is the possibility that this function may block for a short time if another |
| 105 |
// goroutine is trying to take any action. |
| 106 |
// |
| 107 |
// The actual file lock is non-blocking. If we are unable to get the shared file |
| 108 |
// lock, the function will return false instead of waiting for the lock. If we |
| 109 |
// get the lock, we also set the *Flock instance as being shared-locked. |
| 110 |
func (f *Flock) TryRLock() (bool, error) { |
| 111 |
return f.try(&f.r, winLockfileSharedLock) |
| 112 |
} |
| 113 |
|
| 114 |
func (f *Flock) try(locked *bool, flag uint32) (bool, error) { |
| 115 |
f.m.Lock() |
| 116 |
defer f.m.Unlock() |
| 117 |
|
| 118 |
if *locked { |
| 119 |
return true, nil |
| 120 |
} |
| 121 |
|
| 122 |
if f.fh == nil { |
| 123 |
if err := f.setFh(); err != nil { |
| 124 |
return false, err |
| 125 |
} |
| 126 |
defer f.ensureFhState() |
| 127 |
} |
| 128 |
|
| 129 |
_, errNo := lockFileEx(syscall.Handle(f.fh.Fd()), flag|winLockfileFailImmediately, 0, 1, 0, &syscall.Overlapped{}) |
| 130 |
|
| 131 |
if errNo > 0 { |
| 132 |
if errNo == ErrorLockViolation || errNo == syscall.ERROR_IO_PENDING { |
| 133 |
return false, nil |
| 134 |
} |
| 135 |
|
| 136 |
return false, errNo |
| 137 |
} |
| 138 |
|
| 139 |
*locked = true |
| 140 |
|
| 141 |
return true, nil |
| 142 |
} |