trylock

package module
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2019 License: Apache-2.0 Imports: 4 Imported by: 8 modules, 7 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.

README

go-trylock

GoDoc Build Status Coverage Status Go Report Card License

TryLock support on read-write lock for Golang

Interface

go-trylock implements sync.Locker.

Have same interfaces with sync.RWMutex

Documentation can be found at Godoc

Examples

import (
    "context"
    "time"
    "errors"
    "github.com/subchen/go-trylock/v2"
)

var mu = trylock.New()

func goroutineWrite() error {
    if ok := mu.TryLock(context.Background()); !ok {
    	return errors.New("timeout, cannot TryLock !!!")
    }
    defer mu.Unlock()
    
    // write something
}

func goroutineWriteTimeout() error {
    if ok := mu.TryLockTimeout(1 * time.Second); !ok {
    	return errors.New("timeout, cannot TryLock !!!")
    }
    defer mu.Unlock()
    
    // write something
}

func goroutineRead() {
    if ok := mu.RTryLock(context.Background()); !ok {
    	return errors.New("timeout, cannot RTryLock !!!")
    }
    defer mu.RUnlock()
    
    // read something
}

func goroutineReadTimeout() {
    if ok := mu.RTryLockTimeout(1 * time.Second); !ok {
    	return errors.New("timeout, cannot RTryLock !!!")
    }
    defer mu.RUnlock()
    
    // read something
}

LICENSE

Apache 2.0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type TryLocker

type TryLocker interface {
	// TryLock acquires the write lock without blocking.
	// On success, returns true. On failure or context cancellation,
	// returns false.
	// A nil Context means try and return at once.
	TryLock(context.Context) bool

	// TryLockTimeout acquires the write lock without blocking.
	// On success, returns true. On failure or timeout, returns false.
	// A negative timeout means no timeout.
	// A zero timeout means try and return at once.
	TryLockTimeout(time.Duration) bool

	// Lock locks for writing.
	// If the lock is already locked for reading or writing, Lock blocks until the lock is available.
	Lock()

	// Unlock unlocks for writing.
	// It is a panic if is not locked for writing before.
	Unlock()

	// RTryLock acquires the read lock without blocking.
	// On success, returns true. On failure or timeout, returns false.
	// A nil Context means try and return at once.
	RTryLock(context.Context) bool

	// RTryLockTimeout acquires the read lock without blocking.
	// On success, returns true. On failure or timeout, returns false.
	// A negative timeout means no timeout.
	// A zero timeout means try and return at once.
	RTryLockTimeout(time.Duration) bool

	// RLock locks for reading.
	// If the lock is already locked for writing, RLock blocks until the lock is available.
	RLock()

	// RUnlock unlocks for reading.
	// It is a panic if is not locked for reading before.
	RUnlock()
}

TryLocker is a RWMutex with trylock support

func New

func New() TryLocker

New create a new TryLocker instance

Jump to

Keyboard shortcuts

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