Skip to content

Commit c724176

Browse files
initial commit
0 parents  commit c724176

File tree

7 files changed

+259
-0
lines changed

7 files changed

+259
-0
lines changed

.github/workflows/pull_request.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: Pull Request Workflow
2+
on:
3+
pull_request:
4+
workflow_dispatch:
5+
push:
6+
branches:
7+
- master
8+
jobs:
9+
checks:
10+
name: Workspace Checks
11+
runs-on: ubuntu-latest
12+
steps:
13+
14+
- name: Checkout
15+
uses: actions/checkout@v2
16+
17+
- uses: actions/setup-go@v2
18+
with:
19+
go-version: '1.20'
20+
21+
- name: Lint
22+
run: if [ "$(gofmt -s -l . | wc -l)" -gt 0 ]; then exit 1; fi
23+
24+
- name: Tidy
25+
run: |
26+
go mod tidy
27+
if [[ -n $(git status -s) ]]; then exit 1; fi
28+
29+
- name: Vet
30+
run: go vet ./...
31+
32+
- name: Test
33+
run: go test -race ./...

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 Spire Technology LLC
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# go-keymutex
2+
3+
Go library for keyed mutexes.
4+
5+
## Installation
6+
7+
```bash
8+
go get github.com/spiretechnology/go-keymutex
9+
```

go.mod

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module github.com/spiretechnology/go-keymutex
2+
3+
go 1.20
4+
5+
require github.com/stretchr/testify v1.8.4
6+
7+
require (
8+
github.com/davecgh/go-spew v1.1.1 // indirect
9+
github.com/pmezard/go-difflib v1.0.0 // indirect
10+
gopkg.in/yaml.v3 v3.0.1 // indirect
11+
)

go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5+
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
6+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
7+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
8+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
9+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
10+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

keymutex.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package keymutex
2+
3+
import "sync"
4+
5+
// KeyMutex is a mutex that can be locked and unlocked on arbitrary keys.
6+
type KeyMutex[T comparable] struct {
7+
mut sync.Mutex
8+
m map[T]*sync.Mutex
9+
refCounts map[T]int
10+
}
11+
12+
// Lock locks the mutex for the given key.
13+
func (km *KeyMutex[T]) Lock(key T) {
14+
km.lockWithWaiting(key, nil)
15+
}
16+
17+
// Unlock unlocks the mutex for the given key.
18+
func (km *KeyMutex[T]) Unlock(key T) {
19+
// Acquire the map lock
20+
km.mut.Lock()
21+
defer km.mut.Unlock()
22+
23+
// Ensure Unlock is not called more times than Lock
24+
if km.refCounts[key] <= 0 {
25+
return
26+
}
27+
28+
// Get the mutex for the key
29+
mut := km.m[key]
30+
31+
// Decrement the counter for the key
32+
km.refCounts[key]--
33+
34+
// If the counter is zero, delete the mutex
35+
if km.refCounts[key] == 0 {
36+
delete(km.m, key)
37+
delete(km.refCounts, key)
38+
}
39+
40+
// Unlock the mutex
41+
mut.Unlock()
42+
}
43+
44+
func (km *KeyMutex[T]) lockWithWaiting(key T, chanCallback chan<- struct{}) {
45+
// Acquire the map lock
46+
km.mut.Lock()
47+
48+
// Ensure the map exists
49+
if km.m == nil {
50+
km.m = map[T]*sync.Mutex{}
51+
}
52+
if km.refCounts == nil {
53+
km.refCounts = map[T]int{}
54+
}
55+
56+
// Get the mutex for the key. Create it if it doesn't exist
57+
mut, ok := km.m[key]
58+
if !ok {
59+
mut = &sync.Mutex{}
60+
km.m[key] = mut
61+
}
62+
63+
// Increment the counter for the key
64+
km.refCounts[key]++
65+
66+
// Lock the mutex
67+
if chanCallback != nil {
68+
chanCallback <- struct{}{}
69+
}
70+
km.mut.Unlock()
71+
mut.Lock()
72+
}

keymutex_test.go

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package keymutex
2+
3+
import (
4+
"sync"
5+
"testing"
6+
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestKeyMutex(t *testing.T) {
11+
var km KeyMutex[int]
12+
var wg sync.WaitGroup
13+
14+
var sequence1, sequence2 []string
15+
key1 := 1
16+
key2 := 2
17+
18+
km.Lock(1)
19+
20+
// In the background, queue a sequence of events
21+
wg.Add(2)
22+
go func() {
23+
defer wg.Done()
24+
km.Lock(key1)
25+
require.Equal(t, 1, km.refCounts[key1], "refCounts[key1] should be 1")
26+
defer km.Unlock(key1)
27+
go func() {
28+
defer wg.Done()
29+
km.Lock(key1)
30+
defer km.Unlock(key1)
31+
sequence1 = append(sequence1, "C")
32+
}()
33+
sequence1 = append(sequence1, "B")
34+
km.Unlock(key1)
35+
}()
36+
37+
// This should not deadlock, even though key1 is already locked
38+
km.Lock(key2)
39+
require.Equal(t, 1, km.refCounts[key2], "refCounts[key2] should be 1")
40+
sequence2 = append(sequence2, "A")
41+
km.Unlock(key2)
42+
key2RefCount, key2RefCountOk := km.refCounts[key2]
43+
require.Equal(t, 0, key2RefCount, "refCounts[key2] should be 0")
44+
require.Equal(t, false, key2RefCountOk, "refCounts[key2] should not exist")
45+
46+
// Add to the sequence and unlock the key, allowing the goroutines to continue
47+
sequence1 = append(sequence1, "A")
48+
km.Unlock(key1)
49+
50+
// Wait for the goroutines to finish
51+
wg.Wait()
52+
53+
require.Equal(t, []string{"A", "B", "C"}, sequence1)
54+
require.Equal(t, []string{"A"}, sequence2)
55+
require.Equal(t, 0, km.refCounts[key1], "refCounts[key1] should be 0")
56+
require.Equal(t, 0, km.refCounts[key2], "refCounts[key2] should be 0")
57+
}
58+
59+
func TestKeyMutexLocking(t *testing.T) {
60+
var km KeyMutex[int]
61+
var wgAcquiringLock sync.WaitGroup
62+
var wgAllLocksReleased sync.WaitGroup
63+
iterCount := 5
64+
var grantedCount int
65+
66+
km.Lock(1)
67+
68+
chanUnsuspend := make(chan struct{})
69+
70+
// Queue up a bunch of goroutines waiting to acquire the same lock
71+
for i := 0; i < iterCount; i++ {
72+
wgAcquiringLock.Add(1)
73+
wgAllLocksReleased.Add(1)
74+
go func() {
75+
defer wgAllLocksReleased.Done()
76+
defer km.Unlock(1)
77+
chanWaiting := make(chan struct{})
78+
go func() {
79+
<-chanWaiting
80+
wgAcquiringLock.Done()
81+
}()
82+
km.lockWithWaiting(1, chanWaiting)
83+
<-chanUnsuspend
84+
grantedCount++
85+
}()
86+
}
87+
88+
// Because we acquired the first lock, the grantedCount should still be zero here
89+
require.Equal(t, 0, grantedCount, "grantedCount should be 0")
90+
91+
// Wait for all goroutines to be waiting to acquire the lock
92+
wgAcquiringLock.Wait()
93+
require.Equal(t, iterCount+1, km.refCounts[1], "refCounts[1] should be %d", iterCount+1)
94+
95+
// Allow all locks to be acquired sequentially
96+
km.Unlock(1)
97+
close(chanUnsuspend)
98+
99+
// Acquire one more lock, which should wait until all the other locks are released
100+
wgAllLocksReleased.Wait()
101+
require.Equal(t, 0, km.refCounts[1], "refCounts[1] should be 0")
102+
require.Equal(t, iterCount, grantedCount, "grantedCount should be %d", iterCount)
103+
}

0 commit comments

Comments
 (0)