lock

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: MIT Imports: 8 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 HeldRO

func HeldRO(path string) bool

HeldRO 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 not held. Without /proc/locks, Linux falls back to a brief try-lock, which can make a free lock look busy for that moment; darwin uses it only if F_GETLK fails. Locks of processes outside this pid namespace are not listed in /proc/locks.

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

func (l *File) Unlock() error

Jump to

Keyboard shortcuts

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