trylock

package module
v1.2.0 Latest Latest
Warning

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

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

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 MutexLock

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

MutexLock is a simple sync.RWMutex + ability to try to Lock.

func New

func New() *MutexLock

New returns a new MutexLock

func (*MutexLock) Lock

func (m *MutexLock) Lock()

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

func (*MutexLock) RLock

func (m *MutexLock) RLock()

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

func (*MutexLock) RTryLock added in v1.2.0

func (m *MutexLock) RTryLock(timeout time.Duration) bool

RTryLock tries to lock for reading. It returns true in case of success, false if timeout. A negative timeout means no timeout. If timeout is 0 that means try at once and quick return.

func (*MutexLock) RUnlock

func (m *MutexLock) RUnlock()

RUnlock unlocks for reading. It is a panic if m is not locked for reading on entry to Unlock.

func (*MutexLock) TryLock

func (m *MutexLock) TryLock(timeout time.Duration) bool

TryLock tries to lock for writing. It returns true in case of success, false if timeout. A negative timeout means no timeout. If timeout is 0 that means try at once and quick return. If the lock is currently held by another goroutine, TryLock will wait until it has a chance to acquire it.

func (*MutexLock) Unlock

func (m *MutexLock) Unlock()

Unlock unlocks for writing. It is a panic if m is not locked for writing on entry to Unlock.

Jump to

Keyboard shortcuts

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