trylock

package module
v0.0.0-...-ff7e133 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2019 License: MIT Imports: 3 Imported by: 22

README

trylock - TryLock implementation for Go

Build Status GoDoc

trylock uses unsafe, which is sorta "unsafe", but should work until sync.Mutex will change its layout (I hope it never will).

Usage

type LockedStruct struct {
	mu trylock.Mutex
}

storage := &LockedStruct{}

if storage.mu.TryLock() {
	// do something with storage
} else {
	// return busy or use some logic for unavailable storage
}

Documentation

Overview

Example
package main

import (
	"fmt"

	"github.com/LK4D4/trylock"
)

func main() {
	var mu trylock.Mutex
	fmt.Println(mu.TryLock())
	fmt.Println(mu.TryLock())
	mu.Unlock()
	fmt.Println(mu.TryLock())
}
Output:

true
false
true

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Mutex

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

Mutex is simple sync.Mutex + ability to try to Lock.

func (*Mutex) Lock

func (m *Mutex) Lock()

Lock locks m. If the lock is already in use, the calling goroutine blocks until the mutex is available.

func (*Mutex) TryLock

func (m *Mutex) TryLock() bool

TryLock tries to lock m. It returns true in case of success, false otherwise.

func (*Mutex) Unlock

func (m *Mutex) Unlock()

Unlock unlocks m. It is a run-time error if m is not locked on entry to Unlock.

A locked Mutex is not associated with a particular goroutine. It is allowed for one goroutine to lock a Mutex and then arrange for another goroutine to unlock it.

Jump to

Keyboard shortcuts

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