lock

package
v0.1.7 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2026 License: MIT Imports: 9 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package lock wraps flock(2) on O_CLOEXEC files (spec 6.2, 9).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Held

func Held(path string) bool

Held reports whether another open file description holds path's lock. A missing file is not held. It never creates the file.

func HoldersVisible added in v0.1.5

func HoldersVisible() bool

HoldersVisible reports whether Inspect can tell held from free: on Linux, whether /proc/locks can be read.

func SetProcLocksPath added in v0.1.5

func SetProcLocksPath(p string) (restore func())

SetProcLocksPath points the lock view at another file and returns the undo. It is for tests, in this package and others (Linux without a readable /proc/locks); it must not run in parallel, and it panics outside a test binary, like state.StubNetworkFS.

func TryHoldExisting added in v0.1.7

func TryHoldExisting(path string) (*File, State, error)

TryHoldExisting takes path's lock without blocking, on the file that is there (it never creates it), for a caller about to remove something because nobody holds it: Inspect only looks, and a look can miss a holder, so the removal is made under the lock instead. It returns:

  • StateFree with the open file, now locked: the caller holds the lock through its removal, then closes the file, which releases it;
  • StateFree with no file when path is not there;
  • StateHeld when another open file description holds it;
  • StateUnknown with the error for anything else (the caller keeps what it would remove).

Types

type File

type File struct {
	Path string
	// contains filtered or unexported fields
}

File is an open lock file. Locks belong to the open file description (flock semantics), so two Opens of one path exclude each other even within one process.

func Open

func Open(path string) (*File, error)

Open opens (creating 0600, parents 0700) path with O_CLOEXEC.

func (*File) Close

func (l *File) Close() error

func (*File) Fd

func (l *File) Fd() int

func (*File) LockWait

func (l *File) LockWait(ctx context.Context, d time.Duration) error

LockWait retries TryLock every 5 ms until d passes or ctx ends (no unbounded blocking).

func (*File) Rewrite

func (l *File) Rewrite(b []byte) error

Rewrite replaces the file's content in place (pwrite + ftruncate), keeping the inode and lock.

func (*File) TryLock

func (l *File) TryLock() (bool, error)

TryLock takes an exclusive lock without blocking.

func (*File) TryLockShared added in v0.1.6

func (l *File) TryLockShared() (bool, error)

TryLockShared takes a shared lock without blocking (compat.flock: the lock a flock -s caller takes beside other shared holders).

func (*File) Unlock

func (l *File) Unlock() error

type State added in v0.1.5

type State int

State is what a read-only look at a lock found.

const (
	StateFree    State = iota // nobody holds it (or the file is not there)
	StateHeld                 // some process holds it
	StateUnknown              // this host cannot tell without taking the lock (#24)
)

func Inspect added in v0.1.5

func Inspect(path string) State

Inspect reports whether any process holds a flock on path without making a free lock look busy to a concurrent non-blocking caller (#55). Linux reads /proc/locks, matching the device and inode it prints; darwin asks fcntl(F_GETLK), which sees flock locks there. Neither takes the lock. A missing file is free. On Linux without a readable /proc/locks (no procfs, an LSM or seccomp denial, gVisor) a flock cannot be seen without taking it, so the answer is StateUnknown and no lock is taken (#24). Locks of processes outside this pid namespace are not listed in /proc/locks.

The kernel hands /proc/locks out a page at a time and the list shifts when other locks go, so one look can miss a held lock. Inspect looks again until two looks in a row agree, and calls a lock held when inspectLooks looks never do. A look can still be wrong both times, so a caller that removes something on "free" takes the lock itself first (TryHoldExisting).

func (State) String added in v0.1.5

func (s State) String() string

Jump to

Keyboard shortcuts

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