kemu is a simple, efficient implementation of a keyed mutex (lock map) for Go. It provides a way to lock operations based on string keys, allowing concurrent access to different keys while serializing access to the same key.
- Lock operations by string keys
- Non-blocking TryLock operation
- Clean memory management (no leaks from unused locks)
- Thread-safe implementation
- Simple, easy-to-use API
go get github.com/modfin/kemupackage main
import (
"fmt"
"github.com/modfin/kemu"
)
func main() {
// Create a new keyed mutex
km := kemu.New()
// Lock based on a key
key := "user-123"
km.Lock(key)
// Critical section for this key
// ... perform operations that need exclusive access to this key ...
// Unlock when done
km.Unlock(key)
// Try to acquire a lock without blocking
if km.TryLock(key) {
// Lock acquired
// ... do something ...
km.Unlock(key)
} else {
// Lock not acquired, do something else
fmt.Println("Could not acquire lock for", key)
}
// Check if a key is currently locked
if km.Locked(key) {
fmt.Println("Key is locked")
}
}Creates a new keyed mutex instance.
Acquires a lock for the specified key. If the key is already locked, this will block until the lock becomes available.
Attempts to acquire a lock for the specified key without blocking. Returns true if the lock was acquired, false otherwise.
Releases a lock for the specified key. Panics if the key is not locked.
Returns true if the key is currently locked, false otherwise.
- The keyed mutex uses a map of string keys to lock entries
- Each lock entry contains a mutex and a reference count
- The implementation is not reentrant (the same goroutine cannot lock the same key multiple times)
- Locks are automatically cleaned up when the last reference is released, preventing memory leaks
kemu is designed for high-concurrency environments and has been thoroughly tested with concurrent access patterns. It is safe to use from multiple goroutines simultaneously.
Contributions are welcome! Please feel free to submit a Pull Request.