filemutex

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2024 License: MIT Imports: 2 Imported by: 50

README

FileMutex

FileMutex is similar to sync.RWMutex, but also synchronizes across processes. On Linux, OSX, and other POSIX systems it uses the flock system call. On windows it uses the LockFileEx and UnlockFileEx system calls.

import (
	"log"
	"github.com/alexflint/go-filemutex"
)

func main() {
	m, err := filemutex.New("/tmp/foo.lock")
	if err != nil {
		log.Fatalln("Directory did not exist or file could not created")
	}

	m.Lock()  // Will block until lock can be acquired

	// Code here is protected by the mutex

	m.Unlock()
}
Installation
go get github.com/alexflint/go-filemutex

Forked from https://github.com/golang/build/tree/master/cmd/builder/filemutex_*.go

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AlreadyLocked = errors.New("lock already acquired")

Functions

This section is empty.

Types

type FileMutex

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

FileMutex is similar to sync.RWMutex, but also synchronizes across processes. This implementation is based on flock syscall.

func New

func New(filename string) (*FileMutex, error)

func (*FileMutex) Close

func (m *FileMutex) Close() error

Close unlocks the lock and closes the underlying file descriptor.

func (*FileMutex) Lock

func (m *FileMutex) Lock() error

func (*FileMutex) RLock

func (m *FileMutex) RLock() error

func (*FileMutex) RUnlock

func (m *FileMutex) RUnlock() error

func (*FileMutex) TryLock added in v1.1.0

func (m *FileMutex) TryLock() error

func (*FileMutex) Unlock

func (m *FileMutex) Unlock() error

Jump to

Keyboard shortcuts

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