Documentation
¶
Index ¶
- Constants
- func Fastlog2(x float64) float64
- func Fastrand() uint32
- func GetG() unsafe.Pointer
- func Readgstatus(gp unsafe.Pointer) uint32
- func Select(streams ...Selectable) (data any, ok bool)
- type List
- type SelectFactory
- type Selectable
- type Selection
- type Slot
- type ThreadParker
- type ZenQ
- func (self *ZenQ[T]) Close() (alreadyClosedForWrites bool)
- func (self *ZenQ[T]) CloseAsync()
- func (self *ZenQ[T]) Dump()
- func (self *ZenQ[T]) EnqueueSelector(sel *Selection)
- func (self *ZenQ[T]) IsClosed() (closed bool)
- func (self *ZenQ[T]) Read() (data T, queueOpen bool)
- func (self *ZenQ[T]) ReadFromBackLog() (data any, ok bool)
- func (self *ZenQ[T]) Reset()
- func (self *ZenQ[T]) Signal() (sig uint8)
- func (self *ZenQ[T]) Write(value T) (queueClosedForWrites bool)
Constants ¶
const ( // Both reads and writes are possible StateOpen = iota // No further writes can be performed and you can only read upto the last committed write in this state StateClosedForWrites // Neither reads nor writes are possible, queue is fully exhausted StateFullyClosed )
ZenQ global state enums
const ( // Open for being selected SelectionOpen = iota // Running state SelectionRunning )
ZenQ selector state enums
const ( SlotEmpty = iota SlotBusy SlotCommitted SlotClosed )
ZenQ Slot state enums
Variables ¶
This section is empty.
Functions ¶
func Readgstatus ¶
func Select ¶
func Select(streams ...Selectable) (data any, ok bool)
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
Types ¶
type List ¶
type List struct {
// contains filtered or unexported fields
}
List is a lock-free linked list theory -> https://www.cs.rochester.edu/u/scott/papers/1996_PODC_queues.pdf pseudocode -> https://www.cs.rochester.edu/research/synchronization/pseudocode/queues.html
type SelectFactory ¶
type SelectFactory struct {
// contains filtered or unexported fields
}
type Selectable ¶
type Selectable interface {
IsClosed() bool
EnqueueSelector(*Selection)
ReadFromBackLog() (data any, ok bool)
Signal() uint8
}
Selectable is an interface for getting selected among many others
type Selection ¶
type Selection struct {
ThreadPtr *unsafe.Pointer
Data any
// contains filtered or unexported fields
}
Selection is an object shared by a selector and its children ZenQs This object is used for selection notification
func (*Selection) AllQueuesClosed ¶
AllQueuesClosed returns whether all the queues present in selection are closed or not
func (*Selection) DecrementReferenceCount ¶
func (sel *Selection) DecrementReferenceCount()
DecrementReferenceCount decrements the reference count by 1 and puts the object back into the pool if it reaches 0
func (*Selection) IncrementReferenceCount ¶
func (sel *Selection) IncrementReferenceCount()
IncrementReferenceCount does exactly what it says
func (*Selection) SignalQueueClosure ¶
SignalQueueClosure signals the closure of one ZenQ to the selector thread it returns if all queues were closed or not in which case the calling thread must goready() the selector thread
type Slot ¶
type Slot[T any] struct { State uint32 WriteParker *ThreadParker[T] Item T }
type ThreadParker ¶
type ThreadParker[T any] struct { // contains filtered or unexported fields }
ThreadParker is a data-structure used for sleeping and waking up goroutines on user call useful for saving up resources by parking excess goroutines and pre-empt them when required with minimal latency overhead Uses the same lock-free linked list implementation as in `list.go`
func NewThreadParker ¶
func NewThreadParker[T any](n unsafe.Pointer) *ThreadParker[T]
NewThreadParker returns a new thread parker.
func (*ThreadParker[T]) Park ¶
func (tp *ThreadParker[T]) Park(nextNode unsafe.Pointer)
Park parks the current calling goroutine This keeps only one parked goroutine in state at all times the parked goroutine is called with minimal overhead via goready() due to both being in userland This ensures there is no thundering herd https://en.wikipedia.org/wiki/Thundering_herd_problem
func (*ThreadParker[T]) Ready ¶
func (tp *ThreadParker[T]) Ready() (data T, ok bool, freeable *parkSpot[T])
Ready calls one parked goroutine from the queue if available
type ZenQ ¶
type ZenQ[T any] struct { SelectFactory // contains filtered or unexported fields }
ZenQ is the CPU cache optimized ringbuffer implementation
func (*ZenQ[T]) Close ¶
func (self *ZenQ[T]) Close() (alreadyClosedForWrites bool)
Close closes the ZenQ for further writes You can only read uptill the last committed write after closing This function will be blocking in case the queue is full ZenQ is closed from a writer goroutine by design, hence it should always be called from a writer goroutine and never from a reader goroutine which might cause the reader to get blocked and hence deadlock It returns if the queue was already closed for writes or not
func (*ZenQ[T]) CloseAsync ¶
func (self *ZenQ[T]) CloseAsync()
CloseAsync closes the channel asynchronously Useful when an user wants to close the channel from a reader end without blocking the thread
func (*ZenQ[T]) Dump ¶
func (self *ZenQ[T]) Dump()
Dump dumps the current queue state Unsafe to be called from multiple goroutines
func (*ZenQ[T]) EnqueueSelector ¶
func (self *ZenQ[T]) EnqueueSelector(sel *Selection)
EnqueueSelector pushes a calling selector to this ZenQ's selector waitlist
func (*ZenQ[T]) IsClosed ¶
func (self *ZenQ[T]) IsClosed() (closed bool)
IsClosed returns whether the zenq is closed for both reads and writes
func (*ZenQ[T]) Read ¶
func (self *ZenQ[T]) Read() (data T, queueOpen bool)
Read reads a value from the queue, you can once read once per object
func (*ZenQ[T]) ReadFromBackLog ¶
ReadFromBackLog tries to read a data from backlog if available
func (*ZenQ[T]) Reset ¶
func (self *ZenQ[T]) Reset()
Reset resets the queue state This also releases all parked goroutines if any and drains all committed writes