gitee.com/quant1x/gox@v1.21.2/api/channel.go (about) 1 package api 2 3 import ( 4 "reflect" 5 "unsafe" 6 ) 7 8 // ChanIsClosed 判断channel是否关闭 9 func ChanIsClosed(ch any) bool { 10 if reflect.TypeOf(ch).Kind() != reflect.Chan { 11 panic("only channels!") 12 } 13 cptr := *(*uintptr)(unsafe.Pointer( 14 unsafe.Pointer(uintptr(unsafe.Pointer(&ch)) + unsafe.Sizeof(uint(0))), 15 )) 16 // this function will return true if chan.closed > 0 17 // see hchan on https://github.com/golang/go/blob/master/src/runtime/chan.go 18 // type hchan struct { 19 // qcount uint // total data in the queue 20 // dataqsiz uint // size of the circular queue 21 // buf unsafe.Pointer // points to an array of dataqsiz elements 22 // elemsize uint16 23 // closed uint32 24 // ** 25 cptr += unsafe.Sizeof(uint(0)) * 2 26 cptr += unsafe.Sizeof(unsafe.Pointer(uintptr(0))) 27 cptr += unsafe.Sizeof(uint16(0)) 28 return *(*uint32)(unsafe.Pointer(cptr)) > 0 29 }