-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMyMutex.go
More file actions
51 lines (44 loc) · 761 Bytes
/
MyMutex.go
File metadata and controls
51 lines (44 loc) · 761 Bytes
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
package go_utils
import "sync"
var lock1 sync.Mutex
type MyMutex struct {
*sync.Mutex
Name string
IsLock bool
m *map[string]*MyMutex
}
var (
lockMap = map[string]*MyMutex{}
)
func GetLock(s string) *MyMutex {
lock1.Lock()
defer lock1.Unlock()
if o, ok := lockMap[s]; ok {
return o
}
return NewMyMutex(&lockMap, s)
}
func NewMyMutex(m1 *map[string]*MyMutex, Name string) *MyMutex {
r := MyMutex{Name: Name}
r.Mutex = &sync.Mutex{}
(*m1)[Name] = &r
r.m = m1
return &r
}
func (r *MyMutex) Lock() *MyMutex {
r.Mutex.Lock()
r.IsLock = true
return r
}
func (r *MyMutex) Unlock() {
lock1.Lock()
defer lock1.Unlock()
if r.IsLock {
r.Mutex.Unlock()
if nil != r.m {
delete(*r.m, r.Name)
}
r.IsLock = false
r.m = nil
}
}