From 7e02ff36ad865a6be95ba6f73a894b3fb7aff538 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=BE=B7=E6=BB=A1?= Date: Wed, 24 Feb 2021 23:44:14 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=88=86=E6=AE=B5=E5=BC=8F?= =?UTF-8?q?=E7=9A=84=E9=94=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- easylock/abstruct.go | 8 +++---- easylock/error.go | 19 +++++++++++++++ easylock/lock.go | 13 +++++++---- easylock/lock_segment.go | 50 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 8 deletions(-) create mode 100644 easylock/error.go create mode 100644 easylock/lock_segment.go diff --git a/easylock/abstruct.go b/easylock/abstruct.go index 34ca973..bb48ddf 100644 --- a/easylock/abstruct.go +++ b/easylock/abstruct.go @@ -8,8 +8,8 @@ package easylock type EasyLock interface { - Lock() error - Unlock() error - RLock() error - RUnlock() error + Lock(flag string) error + Unlock(flag string) error + RLock(flag string) error + RUnlock(flag string) error } diff --git a/easylock/error.go b/easylock/error.go new file mode 100644 index 0000000..854c147 --- /dev/null +++ b/easylock/error.go @@ -0,0 +1,19 @@ +// Package easylock... +// +// Description : easylock... +// +// Author : go_developer@163.com<张德满> +// +// Date : 2021-02-24 10:55 下午 +package easylock + +import "github.com/pkg/errors" + +// segmentError ... +// +// Author : go_developer@163.com<张德满> +// +// Date : 1:44 下午 2021/2/24 +func segmentError() error { + return errors.New("segment需要大于0") +} diff --git a/easylock/lock.go b/easylock/lock.go index 9d22c0a..2ea0d6c 100644 --- a/easylock/lock.go +++ b/easylock/lock.go @@ -9,6 +9,11 @@ package easylock import "sync" +// NewLock获取普通锁实例,因为只有一把锁,flag没有意义,传空即可 +// +// Author : go_developer@163.com<张德满> +// +// Date : 10:42 下午 2021/2/24 func NewLock() EasyLock { return &lock{ sc: &sync.RWMutex{}, @@ -19,22 +24,22 @@ type lock struct { sc *sync.RWMutex } -func (l *lock) Lock() error { +func (l *lock) Lock(flag string) error { l.sc.Lock() return nil } -func (l *lock) Unlock() error { +func (l *lock) Unlock(flag string) error { l.sc.Unlock() return nil } -func (l *lock) RLock() error { +func (l *lock) RLock(flag string) error { l.sc.RLock() return nil } -func (l *lock) RUnlock() error { +func (l *lock) RUnlock(flag string) error { l.sc.Unlock() return nil } diff --git a/easylock/lock_segment.go b/easylock/lock_segment.go new file mode 100644 index 0000000..afa7f51 --- /dev/null +++ b/easylock/lock_segment.go @@ -0,0 +1,50 @@ +// Package easylock... +// +// Description : 分段的锁 +// +// Author : go_developer@163.com<张德满> +// +// Date : 2021-02-24 10:44 下午 +package easylock + +import "github.com/go-developer/gopkg/util" + +// NewSegment 获取分段锁 +// +// Author : go_developer@163.com<张德满> +// +// Date : 11:20 下午 2021/2/24 +func NewSegment(segmentCnt int) (EasyLock, error) { + if segmentCnt <= 0 { + return nil, segmentError() + } + s := &segment{ + lockTable: make([]EasyLock, segmentCnt), + segmentCnt: segmentCnt, + } + for i := 0; i < segmentCnt; i++ { + s.lockTable[i] = NewLock() + } + return s, nil +} + +type segment struct { + lockTable []EasyLock + segmentCnt int +} + +func (s *segment) Lock(flag string) error { + return s.lockTable[util.GetHashIDMod(flag, s.segmentCnt)].Lock(flag) +} + +func (s *segment) Unlock(flag string) error { + return s.lockTable[util.GetHashIDMod(flag, s.segmentCnt)].Unlock(flag) +} + +func (s *segment) RLock(flag string) error { + return s.lockTable[util.GetHashIDMod(flag, s.segmentCnt)].RLock(flag) +} + +func (s *segment) RUnlock(flag string) error { + return s.lockTable[util.GetHashIDMod(flag, s.segmentCnt)].RUnlock(flag) +}