gflock

package
v1.9.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 23, 2019 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package gflock implements a concurrent-safe sync.Locker interface for file locking.

Index

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

func New(file string) *Locker

New creates and returns a new file locker with given <file>. The parameter <file> usually is a absolute file path.

func (*Locker) IsLocked

func (l *Locker) IsLocked() bool

IsLocked returns whether the locker is locked.

func (*Locker) IsRLocked

func (l *Locker) IsRLocked() bool

IsRLocked returns whether the locker is rlocked.

func (*Locker) Lock

func (l *Locker) Lock() (err error)

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) Path

func (l *Locker) Path() string

Path returns the file path of the locker.

func (*Locker) RLock

func (l *Locker) RLock() (err error)

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

func (l *Locker) RUnlock() (err error)

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

func (l *Locker) TryLock() bool

TryLock tries get the writing lock of the locker. It returns true if success, or else returns false immediately.

func (*Locker) TryRLock

func (l *Locker) TryRLock() bool

TryRLock tries get the reading lock of the locker. It returns true if success, or else returns false immediately.

func (*Locker) Unlock

func (l *Locker) Unlock() (err error)

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL