/
mutex.go
45 lines (38 loc) · 956 Bytes
/
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
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
// Package mutex provides switch of concurrent safe feature for sync.Mutex.
package mutex
import "sync"
// Mutex is a sync.Mutex with a switch for concurrent safe feature.
type Mutex struct {
sync.Mutex
safe bool
}
// New creates and returns a new *Mutex.
// The parameter `safe` is used to specify whether using this mutex in concurrent-safety,
// which is false in default.
func New(safe ...bool) *Mutex {
mu := new(Mutex)
if len(safe) > 0 {
mu.safe = safe[0]
} else {
mu.safe = false
}
return mu
}
func (mu *Mutex) IsSafe() bool {
return mu.safe
}
func (mu *Mutex) Lock() {
if mu.safe {
mu.Mutex.Lock()
}
}
func (mu *Mutex) Unlock() {
if mu.safe {
mu.Mutex.Unlock()
}
}