-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfixed.go
41 lines (34 loc) · 887 Bytes
/
fixed.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package multilock
import (
"sync"
)
// Fixed is a fixed length structure of sync.Mutex
type Fixed struct {
length int
mutexes []sync.Mutex
distribution func(i interface{}, length int) int
}
// NewFixed creates a fixed length structure of sync.Mutex
func NewFixed(length int, opts ...Option) *Fixed {
var options options
for _, opt := range opts {
opt.apply(&options)
}
if options.distribution == nil {
options.distribution = distribution
}
return &Fixed{
length: length,
distribution: options.distribution,
mutexes: make([]sync.Mutex, length),
}
}
// Get retrieves a sync.Mutex from an interface
func (m *Fixed) Get(i interface{}) *sync.Mutex {
return m.GetID(addr(i))
}
// GetID retrieves a sync.Mutex from an identifier
func (m *Fixed) GetID(id string) *sync.Mutex {
index := m.distribution(id, m.length)
return &m.mutexes[index]
}