package
module
Version:
v1.1.0
Opens a new window with list of versions in this module.
Published: Oct 6, 2024
License: BSD-3-Clause
Opens a new window with license information.
Imports: 1
Opens a new window with list of imports.
Imported by: 10
Opens a new window with list of known importers.
README
¶
Example
import "import.name/lock"
import "import.name/lock/unlock"
func ExampleLock() {
var (
mu sync.Mutex
count int
)
lock.Guard(&mu, func() {
count++
})
n := lock.Guarded(&mu, func() int {
return count
})
fmt.Println(n)
}
func ExampleUnlock() {
var mu sync.Mutex
mu.Lock()
defer mu.Unlock()
unlock.Guard(&mu, func() {
time.Sleep(time.Second)
})
}
func ExampleTagLock() {
type countLocked struct{}
var (
mu lock.TagMutex[countLocked]
count int
)
incrementWithLock := func(l countLocked) {
count++
}
readWithLock := func(l countLocked) int {
return count
}
lock.GuardTag(&mu, func(l countLocked) {
incrementWithLock(l)
})
n := lock.GuardTagged(&mu, func(l countLocked) int {
return readWithLock(l)
})
fmt.Println(n)
}
Documentation
¶
Package lock provides locking helpers.
Guard invokes f while keeping the lock locked.
package main
import (
"fmt"
"sync"
"import.name/lock"
)
func main() {
var (
mu sync.Mutex
count int
)
lock.Guard(&mu, func() {
count++
})
n := lock.Guarded(&mu, func() int {
return count
})
fmt.Println(n)
}
Output:
func GuardTagged[Tag any, Val any](lock TagLocker[Tag], f func(Tag) Val) Val
Guarded invokes f while keeping the lock locked. The return value is
passed through.
type TagLocker[Tag any] interface {
Lock() Tag
Unlock()
}
package main
import (
"fmt"
"import.name/lock"
)
func main() {
type countLocked struct{}
var (
mu lock.TagMutex[countLocked]
count int
)
incrementWithLock := func(l countLocked) {
count++
}
readWithLock := func(l countLocked) int {
return count
}
lock.GuardTag(&mu, func(l countLocked) {
incrementWithLock(l)
})
n := lock.GuardTagged(&mu, func(l countLocked) int {
return readWithLock(l)
})
fmt.Println(n)
}
Output:
Source Files
¶
Directories
¶
Package unlock provides unlocking helpers.
|
Package unlock provides unlocking helpers. |
Click to show internal directories.
Click to hide internal directories.