]> 127.0.0.1 Git - part/.git/commitdiff
update v0.24.7
authorqydysky <qydysky@foxmail.com>
Sat, 8 Apr 2023 15:05:48 +0000 (23:05 +0800)
committerqydysky <qydysky@foxmail.com>
Sat, 8 Apr 2023 15:05:48 +0000 (23:05 +0800)
funcCtrl/FuncCtrl.go
funcCtrl/FuncCtrl_test.go

index 53c68c84630f0bb5d1c575d7dffd061ea3ffe93e..ae9e41357704edf12b0525ced89192aab5233dfc 100644 (file)
@@ -26,6 +26,7 @@ type FlashFunc struct {
        b atomic.Uintptr
        l sync.Mutex
        c *context.CancelFunc
+       f *func()
 }
 
 func (t *FlashFunc) Flash() (current uintptr) {
@@ -53,6 +54,17 @@ func (t *FlashFunc) FlashWithContext() (c context.Context) {
        return
 }
 
+func (t *FlashFunc) FlashWithCallback(f func()) {
+       t.l.Lock()
+       defer t.l.Unlock()
+
+       if t.f != nil {
+               (*t.f)()
+               t.f = nil
+       }
+       t.f = &f
+}
+
 // 新的等待旧的
 type BlockFunc struct {
        sync.Mutex
index eb7d4259f15c9395d248f6cbb9d5ed9af0e360bc..0ef03c72a58b4e41084ad2a5d043a5c38a6b2433 100644 (file)
@@ -61,6 +61,23 @@ func Test_FlashFunc2(t *testing.T) {
        }
 }
 
+func Test_FlashFunc3(t *testing.T) {
+       var cc = make(chan int, 2)
+       var b FlashFunc
+       var a = func(i int) {
+               b.FlashWithCallback(func() {
+                       cc <- i
+               })
+       }
+       go a(1)
+       go a(2)
+       go a(3)
+       time.Sleep(time.Second)
+       if len(cc) != 2 && <-cc != 1 && <-cc != 2 {
+               t.Fatal(len(cc))
+       }
+}
+
 func Test_BlockFunc(t *testing.T) {
        var c = make(chan int, 2)
        var b BlockFunc