增加锁计数

This commit is contained in:
白茶清欢 2021-04-01 16:58:34 +08:00
parent 7553846941
commit 5312092ad6
4 changed files with 77 additions and 1 deletions

View File

@ -21,4 +21,6 @@ type EasyLock interface {
RLock(optionFuncList ...OptionFunc) error RLock(optionFuncList ...OptionFunc) error
// RUnlock ... // RUnlock ...
RUnlock(optionFuncList ...OptionFunc) error RUnlock(optionFuncList ...OptionFunc) error
// GetLockCnt 获取锁总数
GetLockCnt() *LockCnt
} }

View File

@ -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 { type base struct {
} }
@ -44,3 +54,39 @@ func (b *base) ParseOption(optionFuncList ...OptionFunc) *option {
} }
return o 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--
}

View File

@ -17,29 +17,44 @@ import "sync"
func NewLock() EasyLock { func NewLock() EasyLock {
return &lock{ return &lock{
sc: &sync.RWMutex{}, sc: &sync.RWMutex{},
lockCnt: &LockCnt{
Write: 0,
Read: 0,
},
} }
} }
type lock struct { type lock struct {
sc *sync.RWMutex sc *sync.RWMutex
lockCnt *LockCnt
base
} }
func (l *lock) Lock(optionFuncList ...OptionFunc) error { func (l *lock) Lock(optionFuncList ...OptionFunc) error {
defer l.AddLockCnt(l.lockCnt)
l.sc.Lock() l.sc.Lock()
return nil return nil
} }
func (l *lock) Unlock(optionFuncList ...OptionFunc) error { func (l *lock) Unlock(optionFuncList ...OptionFunc) error {
defer l.DecreaseLockCnt(l.lockCnt)
l.sc.Unlock() l.sc.Unlock()
return nil return nil
} }
func (l *lock) RLock(optionFuncList ...OptionFunc) error { func (l *lock) RLock(optionFuncList ...OptionFunc) error {
defer l.AddRLockCnt(l.lockCnt)
l.sc.RLock() l.sc.RLock()
return nil return nil
} }
func (l *lock) RUnlock(optionFuncList ...OptionFunc) error { func (l *lock) RUnlock(optionFuncList ...OptionFunc) error {
defer l.DecreaseRLockCnt(l.lockCnt)
l.sc.Unlock() l.sc.Unlock()
return nil return nil
} }
func (l *lock) GetLockCnt() *LockCnt {
return l.lockCnt
}

View File

@ -21,6 +21,10 @@ func NewSegment(segmentCnt int) (EasyLock, error) {
s := &segment{ s := &segment{
lockTable: make([]EasyLock, segmentCnt), lockTable: make([]EasyLock, segmentCnt),
segmentCnt: segmentCnt, segmentCnt: segmentCnt,
lockCnt: &LockCnt{
Write: 0,
Read: 0,
},
} }
for i := 0; i < segmentCnt; i++ { for i := 0; i < segmentCnt; i++ {
s.lockTable[i] = NewLock() s.lockTable[i] = NewLock()
@ -31,25 +35,34 @@ func NewSegment(segmentCnt int) (EasyLock, error) {
type segment struct { type segment struct {
lockTable []EasyLock lockTable []EasyLock
segmentCnt int segmentCnt int
lockCnt *LockCnt
base base
} }
func (s *segment) Lock(optionFuncList ...OptionFunc) error { func (s *segment) Lock(optionFuncList ...OptionFunc) error {
defer s.AddRLockCnt(s.lockCnt)
o := s.ParseOption(optionFuncList...) o := s.ParseOption(optionFuncList...)
return s.lockTable[util.GetHashIDMod(o.flag, s.segmentCnt)].Lock() return s.lockTable[util.GetHashIDMod(o.flag, s.segmentCnt)].Lock()
} }
func (s *segment) Unlock(optionFuncList ...OptionFunc) error { func (s *segment) Unlock(optionFuncList ...OptionFunc) error {
defer s.DecreaseLockCnt(s.lockCnt)
o := s.ParseOption(optionFuncList...) o := s.ParseOption(optionFuncList...)
return s.lockTable[util.GetHashIDMod(o.flag, s.segmentCnt)].Unlock() return s.lockTable[util.GetHashIDMod(o.flag, s.segmentCnt)].Unlock()
} }
func (s *segment) RLock(optionFuncList ...OptionFunc) error { func (s *segment) RLock(optionFuncList ...OptionFunc) error {
defer s.AddRLockCnt(s.lockCnt)
o := s.ParseOption(optionFuncList...) o := s.ParseOption(optionFuncList...)
return s.lockTable[util.GetHashIDMod(o.flag, s.segmentCnt)].RLock() return s.lockTable[util.GetHashIDMod(o.flag, s.segmentCnt)].RLock()
} }
func (s *segment) RUnlock(optionFuncList ...OptionFunc) error { func (s *segment) RUnlock(optionFuncList ...OptionFunc) error {
defer s.DecreaseRLockCnt(s.lockCnt)
o := s.ParseOption(optionFuncList...) o := s.ParseOption(optionFuncList...)
return s.lockTable[util.GetHashIDMod(o.flag, s.segmentCnt)].RUnlock() return s.lockTable[util.GetHashIDMod(o.flag, s.segmentCnt)].RUnlock()
} }
func (s *segment) GetLockCnt() *LockCnt {
return s.lockCnt
}