This commit is contained in:
ljw
2024-09-13 15:57:29 +08:00
commit c53df223d1
112 changed files with 14353 additions and 0 deletions

32
lib/lock/local.go Normal file
View File

@@ -0,0 +1,32 @@
package lock
import (
"sync"
)
type Local struct {
Locks *sync.Map
}
func (l *Local) Lock(key string) {
lock := l.GetLock(key)
lock.Lock()
}
func (l *Local) UnLock(key string) {
lock, ok := l.Locks.Load(key)
if ok {
lock.(*sync.Mutex).Unlock()
}
}
func (l *Local) GetLock(key string) *sync.Mutex {
lock, _ := l.Locks.LoadOrStore(key, &sync.Mutex{})
return lock.(*sync.Mutex)
}
func NewLocal() *Local {
return &Local{
Locks: &sync.Map{},
}
}

100
lib/lock/local_test.go Normal file
View File

@@ -0,0 +1,100 @@
package lock
import (
"fmt"
"sync"
"testing"
)
func TestLocal_GetLock(t *testing.T) {
l := NewLocal()
wg := sync.WaitGroup{}
wg.Add(3)
var l1 *sync.Mutex
var l2 *sync.Mutex
var l3 *sync.Mutex
i := 0
go func() {
l1 = l.GetLock("key")
fmt.Println("l1", l1, i)
l1.Lock()
fmt.Println("l1", i)
i++
l1.Unlock()
wg.Done()
}()
go func() {
l2 = l.GetLock("key")
fmt.Println("l2", l2, i)
l2.Lock()
fmt.Println("l2", i)
i++
l2.Unlock()
wg.Done()
}()
go func() {
l3 = l.GetLock("key")
fmt.Println("l3", l3, i)
l3.Lock()
fmt.Println("l3", i)
i++
l3.Unlock()
wg.Done()
}()
wg.Wait()
fmt.Println(l1, l2, l3)
fmt.Println(l1 == l2, l2 == l3)
fmt.Println(&sync.Mutex{} == &sync.Mutex{})
}
func TestLocal_Lock(t *testing.T) {
l := NewLocal()
wg := sync.WaitGroup{}
wg.Add(3)
i := 0
go func() {
l.Lock("key")
fmt.Println("l1", i)
i++
l.UnLock("key")
wg.Done()
}()
go func() {
l.Lock("key")
fmt.Println("l2", i)
i++
l.UnLock("key")
wg.Done()
}()
go func() {
l.Lock("key")
fmt.Println("l3", i)
i++
l.UnLock("key")
wg.Done()
}()
wg.Wait()
}
func TestSyncMap(t *testing.T) {
m := sync.Map{}
wg := sync.WaitGroup{}
wg.Add(3)
go func() {
v, ok := m.LoadOrStore("key", 1)
fmt.Println(1, v, ok)
wg.Done()
}()
go func() {
v, ok := m.LoadOrStore("key", 2)
fmt.Println(2, v, ok)
wg.Done()
}()
go func() {
v, ok := m.LoadOrStore("key", 3)
fmt.Println(3, v, ok)
wg.Done()
}()
wg.Wait()
}

9
lib/lock/lock.go Normal file
View File

@@ -0,0 +1,9 @@
package lock
import "sync"
type Locker interface {
GetLock(key string) *sync.Mutex
Lock(key string)
UnLock(key string)
}