trylock

package module
v1.1.0 Latest Latest
Warning

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

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

README

go-trylock

Build Status GoDoc Go Report Card License

TryLock support on read-write lock for Golang

Interface

go-trylock implements sync.Locker.

Have same interfaces with sync.RWMutex

Examples

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

var mu = trylock.New()

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

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

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) 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) TryRLock

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

TryRLock 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) 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