filelock

package
v1.8.0 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Mutex

type Mutex struct {
	// contains filtered or unexported fields
}

Mutex is a file-based mutex intended to facilitate cross-process synchronization. Locks acquired by Mutex are advisory, so all participating processes must use advisory lock features in order to co-operate. Locks acquired by Mutex are not inherited by child processes and are automatically released when the process exits.

It is not intended for in-process synchronization, and should not be shared between goroutines without being appropriately guarded by a sync.Mutex.

Upgrading a read-lock to a write lock, or vice-versa, is not guaranteed to happen atomically (on Windows, it is guaranteed not to be atomic).

The io.ReadWriteSeeker, io.ReaderAt, and io.WriterAt interfaces are implemented by *Mutex and are safe to be used once any lock as been acquired (as the result of Mutex.RLock or Mutex.Lock), and until the mutex has been released (Mutex.Unlock). Attempting to use these interfaces without a lock being held results in fs.ErrClosed. On UNIX platforms, it is generally OK to use different file descriptors to access the locked file, as the lock is held by the process, not the individual file descriptor. On Windows however, it is important to use the Mutex to perform IO operations, in particular when a write lock is used, as the lock is tied to the specific file descriptor.

func MutexAt

func MutexAt(path string) *Mutex

MutexAt returns a new Mutex instance that will use the given path as the lock file.

func (*Mutex) Lock

func (m *Mutex) Lock(ctx context.Context) error

Lock attempts to lock the file for reading & writing. It blocks until the lock is acquired, or an error happens. If the file is already locked for reading, it will upgrade the lock to a read-write lock.

func (*Mutex) RLock

func (m *Mutex) RLock(ctx context.Context) error

RLock attempts to lock the file for reading. It blocks until the lock is acquired, or an error happens. If the file is already locked for writing, it will downgrade the lock to a read-only lock.

func (*Mutex) Read added in v1.1.0

func (m *Mutex) Read(b []byte) (int, error)

func (*Mutex) ReadAt added in v1.1.0

func (m *Mutex) ReadAt(b []byte, off int64) (int, error)

func (*Mutex) Seek added in v1.1.0

func (m *Mutex) Seek(offset int64, whence int) (int64, error)

func (*Mutex) Unlock

func (m *Mutex) Unlock(ctx context.Context) error

Unlock releases any lock acquired on the file.

func (*Mutex) Write added in v1.1.0

func (m *Mutex) Write(b []byte) (int, error)

func (*Mutex) WriteAt added in v1.1.0

func (m *Mutex) WriteAt(b []byte, off int64) (int, error)

Jump to

Keyboard shortcuts

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