gopkg/easylock/lock.go

60 lines
1.1 KiB
Go
Raw Permalink Normal View History

2021-12-24 23:55:35 +08:00
// Package easylock ...
2021-02-24 18:47:03 +08:00
//
// Description : 包装各种姿势的锁
//
2021-07-25 19:05:59 +08:00
// Author : go_developer@163.com<白茶清欢>
2021-02-24 18:47:03 +08:00
//
// Date : 2021-02-24 6:26 下午
package easylock
import "sync"
2021-02-24 23:44:14 +08:00
// NewLock获取普通锁实例,因为只有一把锁,flag没有意义,传空即可
//
2021-07-25 19:05:59 +08:00
// Author : go_developer@163.com<白茶清欢>
2021-02-24 23:44:14 +08:00
//
// 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-04-01 20:17:30 +08:00
l.sc.RUnlock()
2021-02-24 18:47:03 +08:00
return nil
}
2021-04-01 16:58:34 +08:00
func (l *lock) GetLockCnt() *LockCnt {
return l.lockCnt
}