完成分段式的锁
This commit is contained in:
parent
31e90d97b5
commit
7e02ff36ad
@ -8,8 +8,8 @@
|
|||||||
package easylock
|
package easylock
|
||||||
|
|
||||||
type EasyLock interface {
|
type EasyLock interface {
|
||||||
Lock() error
|
Lock(flag string) error
|
||||||
Unlock() error
|
Unlock(flag string) error
|
||||||
RLock() error
|
RLock(flag string) error
|
||||||
RUnlock() error
|
RUnlock(flag string) error
|
||||||
}
|
}
|
||||||
|
19
easylock/error.go
Normal file
19
easylock/error.go
Normal file
@ -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")
|
||||||
|
}
|
@ -9,6 +9,11 @@ package easylock
|
|||||||
|
|
||||||
import "sync"
|
import "sync"
|
||||||
|
|
||||||
|
// NewLock获取普通锁实例,因为只有一把锁,flag没有意义,传空即可
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<张德满>
|
||||||
|
//
|
||||||
|
// Date : 10:42 下午 2021/2/24
|
||||||
func NewLock() EasyLock {
|
func NewLock() EasyLock {
|
||||||
return &lock{
|
return &lock{
|
||||||
sc: &sync.RWMutex{},
|
sc: &sync.RWMutex{},
|
||||||
@ -19,22 +24,22 @@ type lock struct {
|
|||||||
sc *sync.RWMutex
|
sc *sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *lock) Lock() error {
|
func (l *lock) Lock(flag string) error {
|
||||||
l.sc.Lock()
|
l.sc.Lock()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *lock) Unlock() error {
|
func (l *lock) Unlock(flag string) error {
|
||||||
l.sc.Unlock()
|
l.sc.Unlock()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *lock) RLock() error {
|
func (l *lock) RLock(flag string) error {
|
||||||
l.sc.RLock()
|
l.sc.RLock()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *lock) RUnlock() error {
|
func (l *lock) RUnlock(flag string) error {
|
||||||
l.sc.Unlock()
|
l.sc.Unlock()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
50
easylock/lock_segment.go
Normal file
50
easylock/lock_segment.go
Normal file
@ -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)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user