From 5312092ad68ba460bda662d73a9a8037def7b891 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=BE=B7=E6=BB=A1?= Date: Thu, 1 Apr 2021 16:58:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=94=81=E8=AE=A1=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- easylock/abstruct.go | 2 ++ easylock/define.go | 46 ++++++++++++++++++++++++++++++++++++++++ easylock/lock.go | 17 ++++++++++++++- easylock/lock_segment.go | 13 ++++++++++++ 4 files changed, 77 insertions(+), 1 deletion(-) diff --git a/easylock/abstruct.go b/easylock/abstruct.go index 9710fd3..33471af 100644 --- a/easylock/abstruct.go +++ b/easylock/abstruct.go @@ -21,4 +21,6 @@ type EasyLock interface { RLock(optionFuncList ...OptionFunc) error // RUnlock ... RUnlock(optionFuncList ...OptionFunc) error + // GetLockCnt 获取锁总数 + GetLockCnt() *LockCnt } diff --git a/easylock/define.go b/easylock/define.go index 367c47f..ec3ba77 100644 --- a/easylock/define.go +++ b/easylock/define.go @@ -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-- +} diff --git a/easylock/lock.go b/easylock/lock.go index 5535882..25c5978 100644 --- a/easylock/lock.go +++ b/easylock/lock.go @@ -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 + 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 +} diff --git a/easylock/lock_segment.go b/easylock/lock_segment.go index 9a440e6..724d376 100644 --- a/easylock/lock_segment.go +++ b/easylock/lock_segment.go @@ -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 +}