From 2894aeadaa41208130392a38f467705f303eea33 Mon Sep 17 00:00:00 2001 From: Anish Mukherjee Date: Fri, 12 Aug 2022 17:42:45 +0530 Subject: [PATCH] update comments --- selector.go | 3 ++- zenq.go | 12 +++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/selector.go b/selector.go index 0f0d694..97d6ea4 100644 --- a/selector.go +++ b/selector.go @@ -44,8 +44,8 @@ type Selectable interface { } // Select selects a single element out of multiple ZenQs -// the second parameter tells if all ZenQs were closed or not before reading, in which case the data returned is nil // A maximum of 127 ZenQs can be selected from at a time owing to the size of int8 type +// `nil` is returned if all streams are closed or if a stream gets closed during the selection process func Select(streams ...Selectable) (data any) { numStreams := int8(len(streams) - 1) filter: @@ -61,6 +61,7 @@ filter: } } if numStreams < 0 { + data = nil return } diff --git a/zenq.go b/zenq.go index 63b9649..623ad84 100644 --- a/zenq.go +++ b/zenq.go @@ -83,15 +83,15 @@ type ( ZenQ[T any] struct { // The padding members 0 to 4 below are here to ensure each item is on a separate cache line. // This prevents false sharing and hence improves performance. - _p0 cacheLinePadding + _ cacheLinePadding writerIndex atomic.Uint32 - _p1 [constants.CacheLinePadSize - unsafe.Sizeof(atomic.Uint32{})]byte + _ [constants.CacheLinePadSize - unsafe.Sizeof(atomic.Uint32{})]byte readerIndex atomic.Uint32 - _p2 [constants.CacheLinePadSize - unsafe.Sizeof(atomic.Uint32{})]byte + _ [constants.CacheLinePadSize - unsafe.Sizeof(atomic.Uint32{})]byte metaQ - _p3 [constants.CacheLinePadSize - unsafe.Sizeof(metaQ{})]byte + _ [constants.CacheLinePadSize - unsafe.Sizeof(metaQ{})]byte selectFactory[T] - _p4 [constants.CacheLinePadSize - unsafe.Sizeof(selectFactory[T]{})]byte + _ [constants.CacheLinePadSize - unsafe.Sizeof(selectFactory[T]{})]byte } ) @@ -147,6 +147,7 @@ direct_send: // direct send to selector *sel.Data = value } else { + // send nil from closed channel *sel.Data = nil } // notify selector @@ -342,6 +343,7 @@ func (self *ZenQ[T]) selectSender() { // write to the selector *sel.Data = data } else { + // send nil from closed channel *sel.Data = nil } // notify selector