Data: Data,
})
*/
- ul := m.lock.RLock(m.to...)
- defer ul(m.removeDisable)
+ defer m.lock.RLock(m.to...)(m.removeDisable)
for el := m.funcs.Front(); el != nil; el = el.Next() {
mi := el.Value.(*msgqItem)
func Test_msgq4(t *testing.T) {
// mq := New(30)
- mq := New() //out of list
+ mq := New(time.Second, time.Second) //out of list
- mun_c1 := make(chan bool, 100)
- mun_c2 := make(chan bool, 100)
- mun_c3 := make(chan bool, 100)
mq.Pull_tag(map[string]func(any) bool{
`A1`: func(data any) bool {
if v, ok := data.(string); !ok || v != `a11` {
t.Error(`1`)
}
- mun_c1 <- true
return false
},
`A2`: func(data any) bool {
if v, ok := data.(string); !ok || v != `a11` {
t.Error(`2`)
}
- mun_c2 <- true
return false
},
`Error`: func(data any) bool {
if v, ok := data.(string); !ok || v != `a11` {
t.Error(`2`)
}
- mun_c3 <- true
return false
},
`Error`: func(data any) bool {
var fin_turn = 0
time.Sleep(time.Second)
- for fin_turn < 5 {
+ for fin_turn < 20 {
go mq.Push_tag(`A1`, `a11`)
go mq.Push_tag(`A1`, `a11`)
go mq.Push_tag(`A1`, `a11`)
mq.Push_tag(`A1`, `a11`)
mq.Push_tag(`A2`, `a11`)
// mq.Push_tag(`A4`,`a11`)
- <-mun_c2
- <-mun_c1
// <-mun_c3
fin_turn += 1
}
"fmt"
"runtime"
"strings"
- "sync/atomic"
+ "sync"
"time"
)
-const (
- lock int32 = -1
- ulock int32 = 0
- rlock int32 = 1
-)
+// const (
+// lock int32 = -1
+// ulock int32 = 0
+// rlock int32 = 1
+// )
var (
ErrTimeoutToLock = errors.New("ErrTimeoutToLock")
)
type RWMutex struct {
- rlc atomic.Int32
+ rlc sync.RWMutex
PanicFunc func(any)
}
defer m.tof(to[0], ErrTimeoutToLock)()
}
- for m.rlc.Load() < rlock && !m.rlc.CompareAndSwap(ulock, rlock) {
- runtime.Gosched()
- }
-
- m.rlc.Add(1)
+ m.rlc.RLock()
return func(beforeUlock ...func()) {
if len(to) > 1 {
for i := 0; i < len(beforeUlock); i++ {
beforeUlock[i]()
}
- if m.rlc.Add(-1) == rlock {
- m.rlc.CompareAndSwap(rlock, ulock)
- }
+ m.rlc.RUnlock()
}
}
if len(to) > 0 {
defer m.tof(to[0], ErrTimeoutToLock)()
}
- for !m.rlc.CompareAndSwap(ulock, lock) {
- runtime.Gosched()
- }
+
+ m.rlc.Lock()
return func(beforeUlock ...func()) {
if len(to) > 1 {
for i := 0; i < len(beforeUlock); i++ {
beforeUlock[i]()
}
- m.rlc.Store(ulock)
+ m.rlc.Unlock()
}
}
import (
"errors"
- "fmt"
+ "sync"
"testing"
"time"
)
func check(l *RWMutex, r int32) {
- if i := l.rlc.Load(); i != r {
- panic(fmt.Errorf("%v %v", i, r))
- }
+ // if i := l.rlc.Load(); i != r {
+ // panic(fmt.Errorf("%v %v", i, r))
+ // }
}
func Test0(t *testing.T) {
// ulock rlock rlock
func Test1(t *testing.T) {
var l RWMutex
- check(&l, 0)
+ //check(&l, 0)
ul := l.RLock()
- check(&l, 2)
+ //check(&l, 2)
ul1 := l.RLock()
- check(&l, 3)
+ //check(&l, 3)
ul()
- check(&l, 2)
+ //check(&l, 2)
ul1()
- check(&l, 0)
+ //check(&l, 0)
}
func Test4(t *testing.T) {
func Test2(t *testing.T) {
var l RWMutex
ul := l.RLock()
- check(&l, 2)
+ //check(&l, 2)
time.AfterFunc(time.Second, func() {
- check(&l, 2)
+ //check(&l, 2)
ul()
})
c := time.Now()
ul1 := l.Lock()
- check(&l, -1)
+ //check(&l, -1)
if time.Since(c) < time.Second {
t.Fail()
}
ul1()
- check(&l, 0)
+ //check(&l, 0)
}
// ulock lock rlock
func Test3(t *testing.T) {
var l RWMutex
ul := l.Lock()
- check(&l, -1)
+ //check(&l, -1)
time.AfterFunc(time.Second, func() {
- check(&l, -1)
+ //check(&l, -1)
ul()
})
c := time.Now()
ul1 := l.RLock()
- check(&l, 2)
+ //check(&l, 2)
if time.Since(c) < time.Second {
t.Fail()
}
ul1()
- check(&l, 0)
+ //check(&l, 0)
}
func Test6(t *testing.T) {
}
}
+func Test9(t *testing.T) {
+ n := time.Now()
+ var l RWMutex
+ for i := 0; i < 1000; i++ {
+ go l.RLock(time.Second, time.Second)()
+ }
+ t.Log(time.Since(n))
+}
+
+func Test10(t *testing.T) {
+ n := time.Now()
+ var l sync.RWMutex
+ for i := 0; i < 300; i++ {
+ l.RLock()
+ go l.RUnlock()
+ }
+ t.Log(time.Since(n))
+}
+
func Panic_Test8(t *testing.T) {
var l RWMutex
ul := l.Lock(time.Second, time.Second)
// ulock rlock rlock
func Panic_Test4(t *testing.T) {
var l RWMutex
- check(&l, 0)
+ //check(&l, 0)
ul := l.RLock(time.Second, time.Second)
- check(&l, 1)
+ //check(&l, 1)
ul1 := l.RLock(time.Second, time.Second)
- check(&l, 2)
+ //check(&l, 2)
time.Sleep(time.Millisecond * 1500)
ul()
- check(&l, 1)
+ //check(&l, 1)
ul1()
- check(&l, 0)
+ //check(&l, 0)
time.Sleep(time.Second * 3)
}
func Panic_Test5(t *testing.T) {
var l RWMutex
ul := l.RLock()
- check(&l, 1)
+ //check(&l, 1)
time.AfterFunc(time.Millisecond*1500, func() {
- check(&l, 1)
+ //check(&l, 1)
ul()
})
c := time.Now()
ul1 := l.Lock(time.Second)
- check(&l, 0)
+ //check(&l, 0)
if time.Since(c) < time.Second {
t.Fail()
}
ul1()
- check(&l, 0)
+ //check(&l, 0)
}
/*
func BenchmarkRlock(b *testing.B) {
var lock1 RWMutex
for i := 0; i < b.N; i++ {
- lock1.Lock()()
+ lock1.RLock()()
}
}