package part
import (
+ "context"
"runtime"
"sync"
"sync/atomic"
// 新的替换旧的
type FlashFunc struct {
b atomic.Uintptr
+ c *context.CancelFunc
}
func (t *FlashFunc) Flash() (current uintptr) {
current = uintptr(unsafe.Pointer(&struct{}{}))
t.b.Store(current)
+ if t.c != nil {
+ (*t.c)()
+ t.c = nil
+ }
return
}
return t.b.Load() != current
}
+func (t *FlashFunc) FlashWithContext() (current uintptr, c context.Context) {
+ current = uintptr(unsafe.Pointer(&struct{}{}))
+ t.b.Store(current)
+ if t.c != nil {
+ (*t.c)()
+ t.c = nil
+ }
+ c, cancle := context.WithCancel(context.Background())
+ t.c = &cancle
+ return
+}
+
// 新的等待旧的
type BlockFunc struct {
sync.Mutex
}
}
+func Test_FlashFunc2(t *testing.T) {
+ var cc = make(chan int, 2)
+ var b FlashFunc
+ var a = func(i int) {
+ _, c := b.FlashWithContext()
+ <-c.Done()
+ 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