-
Notifications
You must be signed in to change notification settings - Fork 22
/
rw_mutex.go
56 lines (43 loc) · 1.14 KB
/
rw_mutex.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright 2023 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
package syncutils
import (
"sync"
)
type RWMutex struct {
lockers []*sync.RWMutex
countLockers int
}
func NewRWMutex(count int) *RWMutex {
if count <= 0 {
count = 1
}
var lockers = []*sync.RWMutex{}
for i := 0; i < count; i++ {
lockers = append(lockers, &sync.RWMutex{})
}
return &RWMutex{
lockers: lockers,
countLockers: len(lockers),
}
}
func (this *RWMutex) Lock(index int) {
this.lockers[index%this.countLockers].Lock()
}
func (this *RWMutex) Unlock(index int) {
this.lockers[index%this.countLockers].Unlock()
}
func (this *RWMutex) RLock(index int) {
this.lockers[index%this.countLockers].RLock()
}
func (this *RWMutex) RUnlock(index int) {
this.lockers[index%this.countLockers].RUnlock()
}
func (this *RWMutex) TryLock(index int) bool {
return this.lockers[index%this.countLockers].TryLock()
}
func (this *RWMutex) TryRLock(index int) bool {
return this.lockers[index%this.countLockers].TryRLock()
}
func (this *RWMutex) RWMutex(index int) *sync.RWMutex {
return this.lockers[index%this.countLockers]
}