2021-02-24 18:47:03 +08:00
|
|
|
// Package easylock...
|
|
|
|
//
|
|
|
|
// Description : 包装各种姿势的锁
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<张德满>
|
|
|
|
//
|
|
|
|
// Date : 2021-02-24 6:26 下午
|
|
|
|
package easylock
|
|
|
|
|
|
|
|
import "sync"
|
|
|
|
|
2021-02-24 23:44:14 +08:00
|
|
|
// NewLock获取普通锁实例,因为只有一把锁,flag没有意义,传空即可
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<张德满>
|
|
|
|
//
|
|
|
|
// Date : 10:42 下午 2021/2/24
|
2021-02-24 18:47:03 +08:00
|
|
|
func NewLock() EasyLock {
|
|
|
|
return &lock{
|
|
|
|
sc: &sync.RWMutex{},
|
2021-04-01 16:58:34 +08:00
|
|
|
lockCnt: &LockCnt{
|
|
|
|
Write: 0,
|
|
|
|
Read: 0,
|
|
|
|
},
|
2021-02-24 18:47:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type lock struct {
|
2021-04-01 16:58:34 +08:00
|
|
|
sc *sync.RWMutex
|
|
|
|
lockCnt *LockCnt
|
|
|
|
base
|
2021-02-24 18:47:03 +08:00
|
|
|
}
|
|
|
|
|
2021-04-01 16:28:23 +08:00
|
|
|
func (l *lock) Lock(optionFuncList ...OptionFunc) error {
|
2021-04-01 16:58:34 +08:00
|
|
|
defer l.AddLockCnt(l.lockCnt)
|
2021-02-24 18:47:03 +08:00
|
|
|
l.sc.Lock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-01 16:28:23 +08:00
|
|
|
func (l *lock) Unlock(optionFuncList ...OptionFunc) error {
|
2021-04-01 16:58:34 +08:00
|
|
|
defer l.DecreaseLockCnt(l.lockCnt)
|
2021-02-24 18:47:03 +08:00
|
|
|
l.sc.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-01 16:28:23 +08:00
|
|
|
func (l *lock) RLock(optionFuncList ...OptionFunc) error {
|
2021-04-01 16:58:34 +08:00
|
|
|
defer l.AddRLockCnt(l.lockCnt)
|
|
|
|
|
2021-02-24 18:47:03 +08:00
|
|
|
l.sc.RLock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-01 16:28:23 +08:00
|
|
|
func (l *lock) RUnlock(optionFuncList ...OptionFunc) error {
|
2021-04-01 16:58:34 +08:00
|
|
|
defer l.DecreaseRLockCnt(l.lockCnt)
|
2021-02-24 18:47:03 +08:00
|
|
|
l.sc.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
2021-04-01 16:58:34 +08:00
|
|
|
|
|
|
|
func (l *lock) GetLockCnt() *LockCnt {
|
|
|
|
return l.lockCnt
|
|
|
|
}
|