Documentation ¶
Overview ¶
Package gflock implements a concurrent-safe sync.Locker interface for file locking.
Index ¶
- type Locker
- func (l *Locker) IsLocked() bool
- func (l *Locker) IsRLocked() bool
- func (l *Locker) Lock() (err error)
- func (l *Locker) Path() string
- func (l *Locker) RLock() (err error)
- func (l *Locker) RUnlock() (err error)
- func (l *Locker) TryLock() bool
- func (l *Locker) TryRLock() bool
- func (l *Locker) Unlock() (err error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Locker ¶
type Locker struct {
// contains filtered or unexported fields
}
File locker.
func New ¶
New creates and returns a new file locker with given <file>. The parameter <file> usually is a absolute file path.
func (*Locker) Lock ¶
Lock is a blocking call to try and take an exclusive file lock. It will wait until it is able to obtain the exclusive file lock. It's recommended that TryLock() be used over this function. This function may block the ability to query the current Locked() or RLocked() status due to a RW-mutex lock.
If we are already exclusive-locked, this function short-circuits and returns immediately assuming it can take the mutex lock.
If the *Flock has a shared lock (RLock), this may transparently replace the shared lock with an exclusive lock on some UNIX-like operating systems. Be careful when using exclusive locks in conjunction with shared locks (RLock()), because calling Unlock() may accidentally release the exclusive lock that was once a shared lock.
func (*Locker) RLock ¶
RLock is a blocking call to try and take a ahred file lock. It will wait until it is able to obtain the shared file lock. It's recommended that TryRLock() be used over this function. This function may block the ability to query the current Locked() or RLocked() status due to a RW-mutex lock.
If we are already shared-locked, this function short-circuits and returns immediately assuming it can take the mutex lock.
func (*Locker) RUnlock ¶
Unlock is a function to unlock the file. This file takes a RW-mutex lock, so while it is running the Locked() and RLocked() functions will be blocked.
This function short-circuits if we are unlocked already. If not, it calls syscall.LOCK_UN on the file and closes the file descriptor. It does not remove the file from disk. It's up to your application to do.
Please note, if your shared lock became an exclusive lock this may unintentionally drop the exclusive lock if called by the consumer that believes they have a shared lock. Please see Lock() for more details.
func (*Locker) TryLock ¶
TryLock tries get the writing lock of the locker. It returns true if success, or else returns false immediately.
func (*Locker) TryRLock ¶
TryRLock tries get the reading lock of the locker. It returns true if success, or else returns false immediately.
func (*Locker) Unlock ¶
Unlock is a function to unlock the file. This file takes a RW-mutex lock, so while it is running the Locked() and RLocked() functions will be blocked.
This function short-circuits if we are unlocked already. If not, it calls syscall.LOCK_UN on the file and closes the file descriptor. It does not remove the file from disk. It's up to your application to do.
Please note, if your shared lock became an exclusive lock this may unintentionally drop the exclusive lock if called by the consumer that believes they have a shared lock. Please see Lock() for more details.