From 7553846941aa1172d8897184aeaa4d5d4754e1c6 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:28:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=87=E7=BA=A7easylock=E7=9A=84=E5=85=A5?= =?UTF-8?q?=E5=8F=82=E9=80=89=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- easylock/abstruct.go | 17 +++++++++++---- easylock/define.go | 46 ++++++++++++++++++++++++++++++++++++++++ easylock/lock.go | 8 +++---- easylock/lock_segment.go | 21 +++++++++++------- 4 files changed, 76 insertions(+), 16 deletions(-) create mode 100644 easylock/define.go diff --git a/easylock/abstruct.go b/easylock/abstruct.go index bb48ddf..9710fd3 100644 --- a/easylock/abstruct.go +++ b/easylock/abstruct.go @@ -7,9 +7,18 @@ // Date : 2021-02-24 6:29 下午 package easylock +// EasyLock ... +// +// Author : go_developer@163.com<张德满> +// +// Date : 4:18 下午 2021/4/1 type EasyLock interface { - Lock(flag string) error - Unlock(flag string) error - RLock(flag string) error - RUnlock(flag string) error + // Lock ... + Lock(optionFuncList ...OptionFunc) error + // Unlock ... + Unlock(optionFuncList ...OptionFunc) error + // RLock ... + RLock(optionFuncList ...OptionFunc) error + // RUnlock ... + RUnlock(optionFuncList ...OptionFunc) error } diff --git a/easylock/define.go b/easylock/define.go new file mode 100644 index 0000000..367c47f --- /dev/null +++ b/easylock/define.go @@ -0,0 +1,46 @@ +// Package easylock ... +// +// Description : easylock ... +// +// Author : go_developer@163.com<张德满> +// +// Date : 2021-04-01 4:13 下午 +package easylock + +type option struct { + flag string // 锁的标识 +} + +// Option 设置option选项 +// +// Author : go_developer@163.com<张德满> +// +// Date : 4:15 下午 2021/4/1 +type OptionFunc func(o *option) + +// WithFlag 设置flag +// +// Author : go_developer@163.com<张德满> +// +// Date : 4:17 下午 2021/4/1 +func WithFlag(flag string) OptionFunc { + return func(o *option) { + o.flag = flag + } +} + +type base struct { +} + +// ParseOption 解析option +// +// Author : go_developer@163.com<张德满> +// +// Date : 4:24 下午 2021/4/1 +func (b *base) ParseOption(optionFuncList ...OptionFunc) *option { + o := &option{} + for _, f := range optionFuncList { + f(o) + } + return o +} diff --git a/easylock/lock.go b/easylock/lock.go index 2ea0d6c..5535882 100644 --- a/easylock/lock.go +++ b/easylock/lock.go @@ -24,22 +24,22 @@ type lock struct { sc *sync.RWMutex } -func (l *lock) Lock(flag string) error { +func (l *lock) Lock(optionFuncList ...OptionFunc) error { l.sc.Lock() return nil } -func (l *lock) Unlock(flag string) error { +func (l *lock) Unlock(optionFuncList ...OptionFunc) error { l.sc.Unlock() return nil } -func (l *lock) RLock(flag string) error { +func (l *lock) RLock(optionFuncList ...OptionFunc) error { l.sc.RLock() return nil } -func (l *lock) RUnlock(flag string) error { +func (l *lock) RUnlock(optionFuncList ...OptionFunc) error { l.sc.Unlock() return nil } diff --git a/easylock/lock_segment.go b/easylock/lock_segment.go index afa7f51..9a440e6 100644 --- a/easylock/lock_segment.go +++ b/easylock/lock_segment.go @@ -31,20 +31,25 @@ func NewSegment(segmentCnt int) (EasyLock, error) { type segment struct { lockTable []EasyLock segmentCnt int + base } -func (s *segment) Lock(flag string) error { - return s.lockTable[util.GetHashIDMod(flag, s.segmentCnt)].Lock(flag) +func (s *segment) Lock(optionFuncList ...OptionFunc) error { + o := s.ParseOption(optionFuncList...) + return s.lockTable[util.GetHashIDMod(o.flag, s.segmentCnt)].Lock() } -func (s *segment) Unlock(flag string) error { - return s.lockTable[util.GetHashIDMod(flag, s.segmentCnt)].Unlock(flag) +func (s *segment) Unlock(optionFuncList ...OptionFunc) error { + o := s.ParseOption(optionFuncList...) + return s.lockTable[util.GetHashIDMod(o.flag, s.segmentCnt)].Unlock() } -func (s *segment) RLock(flag string) error { - return s.lockTable[util.GetHashIDMod(flag, s.segmentCnt)].RLock(flag) +func (s *segment) RLock(optionFuncList ...OptionFunc) error { + o := s.ParseOption(optionFuncList...) + return s.lockTable[util.GetHashIDMod(o.flag, s.segmentCnt)].RLock() } -func (s *segment) RUnlock(flag string) error { - return s.lockTable[util.GetHashIDMod(flag, s.segmentCnt)].RUnlock(flag) +func (s *segment) RUnlock(optionFuncList ...OptionFunc) error { + o := s.ParseOption(optionFuncList...) + return s.lockTable[util.GetHashIDMod(o.flag, s.segmentCnt)].RUnlock() }