Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/josephbuchma/comutex
Deadlock-free nested function calls that lock same mutex in same go routine [Go package]
https://github.com/josephbuchma/comutex
context golang mutex sync
Last synced: about 2 months ago
JSON representation
Deadlock-free nested function calls that lock same mutex in same go routine [Go package]
- Host: GitHub
- URL: https://github.com/josephbuchma/comutex
- Owner: josephbuchma
- License: mit
- Created: 2021-08-24T21:02:35.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2021-08-24T21:24:47.000Z (over 3 years ago)
- Last Synced: 2024-06-20T09:15:36.195Z (7 months ago)
- Topics: context, golang, mutex, sync
- Language: Go
- Homepage:
- Size: 10.7 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# comutex [![Go Report Card](https://goreportcard.com/badge/github.com/josephbuchma/comutex)](https://goreportcard.com/badge/github.com/josephbuchma/comutex) [![Go Reference](https://pkg.go.dev/badge/github.com/josephbuchma/comutex.svg)](https://pkg.go.dev/github.com/josephbuchma/comutex)
> Context + Mutex = Comutex
Comutex is a Go package which allows to perform nested calls of functions that lock same mutex
in the same thread/goroutine with a help of `context.Context`.
Both Mutex and RWMutex are supported.## Example
```go
package comutex_testimport (
"context"
"fmt"
"sync""github.com/josephbuchma/comutex"
)// IntKVStore is a simple shared stirng=>int KV store.
// Even though all of its methods are locking mutex,
// Get method is used inside of Sum,
// and Has is used inside of Insert and Update - and it doesn't cause a deadlock.
type IntKVStore struct {
mu sync.RWMutex
m map[string]int
}func (s *IntKVStore) Get(ctx context.Context, key string) int {
_, unlock := comutex.RLock(ctx, &s.mu)
defer unlock()
return s.m[key]
}func (s *IntKVStore) Has(ctx context.Context, key string) bool {
_, unlock := comutex.RLock(ctx, &s.mu)
defer unlock()
_, ok := s.m[key]
return ok
}func (s *IntKVStore) Insert(ctx context.Context, key string, v int) bool {
ctx, unlock := comutex.MustLock(ctx, &s.mu)
defer unlock()
if s.Has(ctx, key) {
return false
}
s.m[key] = v
return true
}func (s *IntKVStore) Update(ctx context.Context, key string, v int) bool {
ctx, unlock := comutex.MustLock(ctx, &s.mu)
defer unlock()
if s.Has(ctx, key) {
s.m[key] = v
return true
}
return false
}func (s *IntKVStore) Sum(ctx context.Context, keys ...string) int {
ctx, unlock := comutex.RLock(ctx, &s.mu)
defer unlock()
r := 0
for _, key := range keys {
r += s.Get(ctx, key)
}
return r
}func Example() {
ctx := context.Background()
s := &IntKVStore{m: map[string]int{}}
s.Insert(ctx, "a", 1)
s.Insert(ctx, "b", 2)
s.Update(ctx, "b", 3)fmt.Println("a+b:", s.Sum(ctx, "a", "b"))
// Output:
// a+b: 4
}```
## License
MIT