增加锁计数
This commit is contained in:
parent
7553846941
commit
5312092ad6
@ -21,4 +21,6 @@ type EasyLock interface {
|
||||
RLock(optionFuncList ...OptionFunc) error
|
||||
// RUnlock ...
|
||||
RUnlock(optionFuncList ...OptionFunc) error
|
||||
// GetLockCnt 获取锁总数
|
||||
GetLockCnt() *LockCnt
|
||||
}
|
||||
|
@ -29,6 +29,16 @@ func WithFlag(flag string) OptionFunc {
|
||||
}
|
||||
}
|
||||
|
||||
// LockCnt 计数lockCnt
|
||||
//
|
||||
// Author : go_developer@163.com<张德满>
|
||||
//
|
||||
// Date : 4:41 下午 2021/4/1
|
||||
type LockCnt struct {
|
||||
Write int64 `json:"write"`
|
||||
Read int64 `json:"read"`
|
||||
}
|
||||
|
||||
type base struct {
|
||||
}
|
||||
|
||||
@ -44,3 +54,39 @@ func (b *base) ParseOption(optionFuncList ...OptionFunc) *option {
|
||||
}
|
||||
return o
|
||||
}
|
||||
|
||||
// AddLockCnt 锁数量加一
|
||||
//
|
||||
// Author : go_developer@163.com<张德满>
|
||||
//
|
||||
// Date : 4:44 下午 2021/4/1
|
||||
func (b *base) AddLockCnt(lockCnt *LockCnt) {
|
||||
lockCnt.Write++
|
||||
}
|
||||
|
||||
// DecreaseLockCnt 锁数量
|
||||
//
|
||||
// Author : go_developer@163.com<张德满>
|
||||
//
|
||||
// Date : 4:45 下午 2021/4/1
|
||||
func (b *base) DecreaseLockCnt(lockCnt *LockCnt) {
|
||||
lockCnt.Write--
|
||||
}
|
||||
|
||||
// AddRLockCnt 锁数量加一
|
||||
//
|
||||
// Author : go_developer@163.com<张德满>
|
||||
//
|
||||
// Date : 4:44 下午 2021/4/1
|
||||
func (b *base) AddRLockCnt(lockCnt *LockCnt) {
|
||||
lockCnt.Read++
|
||||
}
|
||||
|
||||
// DecreaseRLockCnt 锁数量
|
||||
//
|
||||
// Author : go_developer@163.com<张德满>
|
||||
//
|
||||
// Date : 4:45 下午 2021/4/1
|
||||
func (b *base) DecreaseRLockCnt(lockCnt *LockCnt) {
|
||||
lockCnt.Read--
|
||||
}
|
||||
|
@ -17,29 +17,44 @@ import "sync"
|
||||
func NewLock() EasyLock {
|
||||
return &lock{
|
||||
sc: &sync.RWMutex{},
|
||||
lockCnt: &LockCnt{
|
||||
Write: 0,
|
||||
Read: 0,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
type lock struct {
|
||||
sc *sync.RWMutex
|
||||
lockCnt *LockCnt
|
||||
base
|
||||
}
|
||||
|
||||
func (l *lock) Lock(optionFuncList ...OptionFunc) error {
|
||||
defer l.AddLockCnt(l.lockCnt)
|
||||
l.sc.Lock()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *lock) Unlock(optionFuncList ...OptionFunc) error {
|
||||
defer l.DecreaseLockCnt(l.lockCnt)
|
||||
l.sc.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *lock) RLock(optionFuncList ...OptionFunc) error {
|
||||
defer l.AddRLockCnt(l.lockCnt)
|
||||
|
||||
l.sc.RLock()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *lock) RUnlock(optionFuncList ...OptionFunc) error {
|
||||
defer l.DecreaseRLockCnt(l.lockCnt)
|
||||
l.sc.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *lock) GetLockCnt() *LockCnt {
|
||||
return l.lockCnt
|
||||
}
|
||||
|
@ -21,6 +21,10 @@ func NewSegment(segmentCnt int) (EasyLock, error) {
|
||||
s := &segment{
|
||||
lockTable: make([]EasyLock, segmentCnt),
|
||||
segmentCnt: segmentCnt,
|
||||
lockCnt: &LockCnt{
|
||||
Write: 0,
|
||||
Read: 0,
|
||||
},
|
||||
}
|
||||
for i := 0; i < segmentCnt; i++ {
|
||||
s.lockTable[i] = NewLock()
|
||||
@ -31,25 +35,34 @@ func NewSegment(segmentCnt int) (EasyLock, error) {
|
||||
type segment struct {
|
||||
lockTable []EasyLock
|
||||
segmentCnt int
|
||||
lockCnt *LockCnt
|
||||
base
|
||||
}
|
||||
|
||||
func (s *segment) Lock(optionFuncList ...OptionFunc) error {
|
||||
defer s.AddRLockCnt(s.lockCnt)
|
||||
o := s.ParseOption(optionFuncList...)
|
||||
return s.lockTable[util.GetHashIDMod(o.flag, s.segmentCnt)].Lock()
|
||||
}
|
||||
|
||||
func (s *segment) Unlock(optionFuncList ...OptionFunc) error {
|
||||
defer s.DecreaseLockCnt(s.lockCnt)
|
||||
o := s.ParseOption(optionFuncList...)
|
||||
return s.lockTable[util.GetHashIDMod(o.flag, s.segmentCnt)].Unlock()
|
||||
}
|
||||
|
||||
func (s *segment) RLock(optionFuncList ...OptionFunc) error {
|
||||
defer s.AddRLockCnt(s.lockCnt)
|
||||
o := s.ParseOption(optionFuncList...)
|
||||
return s.lockTable[util.GetHashIDMod(o.flag, s.segmentCnt)].RLock()
|
||||
}
|
||||
|
||||
func (s *segment) RUnlock(optionFuncList ...OptionFunc) error {
|
||||
defer s.DecreaseRLockCnt(s.lockCnt)
|
||||
o := s.ParseOption(optionFuncList...)
|
||||
return s.lockTable[util.GetHashIDMod(o.flag, s.segmentCnt)].RUnlock()
|
||||
}
|
||||
|
||||
func (s *segment) GetLockCnt() *LockCnt {
|
||||
return s.lockCnt
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user