ctxlock

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2023 License: MIT Imports: 2 Imported by: 0

README

ctxlock: Context-aware Lock

ctxlock provides context-aware locks (Lock / SharableLock). User can safely cancel a wating to acquire the lock through context.Context.

Unlike standard sync.Mutex, there is no unlock method. An unlock function (ctxlock.UnlockFunc) is returned from its lock methods instead.

The unlock function works only once and it is safe to call it multiple times.

The unlock function is not connected to the context.Context. If you want to call it when a context is canceled, UnlockFunc.UnlockOnCancel(ctx) method can be used.

ctxlock is based on goroutine and channel, so that it might have performance overhead than that of sync.Lock.

(Pseudo) Example

Ordinary Exclusive Lock
L := ctxlock.NewLock()

// Lock() method tries to lock and returns unlock function.
unlock, err := L.Lock(context.Background())
Reader-Writer Lock
L := ctxlock.NewSharableLock()

// Reader Lock
unlock, err := L.SharedLock(context.Background())

// Writer Lock
unlock, err := L.ExclusiveLock(context.Backgroun())

Documentation

Overview

Package ctxlock provides context aware lock

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Lock

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

Lock implements ordinary exclusive lock.

func NewLock

func NewLock() *Lock

NewLock creates a new Lock and returns the pointer to it.

func (*Lock) Lock

func (L *Lock) Lock(ctx context.Context) (UnlockFunc, error)

Lock tries to lock and returns unlock function when it succeed. If ctx is canceled, lock is canceled and context.Cause(ctx) error is returned.

type SharableLock

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

SharableLock implements exclusive lock for writer and shared lock for reader.

func NewSharableLock

func NewSharableLock() *SharableLock

NewSharableLock creates a new SharableLock and returns the pointer to it.

func (*SharableLock) ExclusiveLock

func (L *SharableLock) ExclusiveLock(ctx context.Context) (UnlockFunc, error)

ExclusiveLock tries to lock for writer and returns unlock function when it succeed. If ctx is canceled, lock is canceled and context.Cause(ctx) error is returned.

func (*SharableLock) SharedLock

func (L *SharableLock) SharedLock(ctx context.Context) (UnlockFunc, error)

SharedLock tries to lock for reader and returns unlock function when it succeed. If ctx is canceled, lock is canceled and context.Cause(ctx) error is returned.

type UnlockFunc

type UnlockFunc func()

func (UnlockFunc) UnlockOnCancel

func (f UnlockFunc) UnlockOnCancel(ctx context.Context)

UnlockOnCancel schedules to call unlock when ctx cancels. If the ctx has already been canceled, unlock is called immediately.

Jump to

Keyboard shortcuts

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