From: qydysky Date: Fri, 7 Apr 2023 09:19:42 +0000 (+0000) Subject: add X-Git-Tag: v0.24.5 X-Git-Url: http://127.0.0.1:8081/?a=commitdiff_plain;h=7e4578900c63ec4d0b18650463c4cc3328bb65a5;p=part%2F.git add --- diff --git a/msgq/Msgq.go b/msgq/Msgq.go index 8054e51..a6b8e3e 100644 --- a/msgq/Msgq.go +++ b/msgq/Msgq.go @@ -91,6 +91,31 @@ func (m *Msgq) PushLock(msg any) { } } +func (m *Msgq) ClearAll() { + for m.someNeedRemove.Load() != 0 { + time.Sleep(time.Millisecond) + runtime.Gosched() + } + + m.lock.Lock() + defer m.lock.Unlock() + + var removes []*list.Element + + for el := m.funcs.Front(); el != nil; el = el.Next() { + m.someNeedRemove.Add(1) + removes = append(removes, el) + } + + if len(removes) != 0 { + m.someNeedRemove.Add(-int32(len(removes))) + for i := 0; i < len(removes); i++ { + m.funcs.Remove(removes[i]) + } + removes = nil + } +} + type Msgq_tag_data struct { Tag string Data any @@ -192,6 +217,10 @@ func (m *MsgType[T]) PushLock_tag(Tag string, Data T) { }) } +func (m *MsgType[T]) ClearAll() { + m.m.ClearAll() +} + func (m *MsgType[T]) Pull_tag_only(key string, f func(T) (disable bool)) { m.m.Register(func(data any) (disable bool) { if d, ok := data.(Msgq_tag_data); ok && d.Tag == key { diff --git a/msgq/Msgq_test.go b/msgq/Msgq_test.go index 7920b8d..4e2f991 100644 --- a/msgq/Msgq_test.go +++ b/msgq/Msgq_test.go @@ -194,6 +194,39 @@ func Test_msgq1(t *testing.T) { } } +func Test_msgq9(t *testing.T) { + var mq = NewType[int]() + var c = make(chan int, 10) + mq.Pull_tag_only(`c`, func(i int) (disable bool) { + c <- i + return false + }) + mq.PushLock_tag(`c`, 1) + mq.ClearAll() + mq.PushLock_tag(`c`, 2) + if len(c) != 1 || <-c != 1 { + t.Fatal() + } + + mq.Pull_tag(map[string]func(int) (disable bool){ + `c`: func(i int) (disable bool) { + c <- i + return false + }, + `s`: func(_ int) (disable bool) { + mq.ClearAll() + return false + }, + }) + + mq.PushLock_tag(`c`, 1) + mq.ClearAll() + mq.PushLock_tag(`c`, 2) + if len(c) != 1 || <-c != 1 { + t.Fatal() + } +} + func Test_msgq2(t *testing.T) { mq := New()