trylock

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: May 29, 2018 License: Apache-2.0 Imports: 3 Imported by: 3

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 (
    "time"
    "errors"
    "github.com/subchen/go-trylock"
)

var mu = trylock.New()

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

func goroutineRead() {
    if ok := mu.RTryLock(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 added in v1.3.0

type TryLocker interface {
	// TryLock 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.
	TryLock(timeout 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 negative timeout means no timeout.
	// A zero timeout means try and return at once.
	RTryLock(timeout 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