async

package
v1.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 24, 2026 License: BSD-3-Clause Imports: 9 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package async provides unbounded channel and queue structures that are designed for caching unlimited number of a concrete type. For better performance, a given type should be less or euqal than 16 bytes.

The difference of an unbounded channel or queue is that unbounde channels can utilize select and channel semantics, whereas queue cannot. A user of this package should balance this tradeoff. For instance, an unbounded channel can provide zero waiting cost when trying to receiving an object when the receiving select statement has a default case, and a queue can only receive the object with a time amount of time, but depending on the number of queue item producer, the receiving time may increase accordingly.

Delicate dance: One must aware that an unbounded channel may lead to OOM when the consuming speed of the buffer is lower than the producing speed constantly. However, such a channel may be fairly used for event delivering if the consumer of the channel consumes the incoming forever, such as even processing.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EnsureMain

func EnsureMain(fn func())

EnsureMain is part of our thread transition and makes sure that the passed function runs on main. If the context is main or the transition has been disabled this will blindly run. Otherwise, an error will be logged and the function will be called on the main goroutine.

This will be removed later and should never be public

func EnsureNotMain

func EnsureNotMain(fn func())

EnsureNotMain is part of our thread transition and makes sure that the passed function runs off main. If the context is running on a goroutine or the transition has been disabled this will blindly run. Otherwise, an error will be logged and the function will be called on a new goroutine.

This will be removed later and should never be public

func IsMainGoroutine

func IsMainGoroutine() bool

IsMainGoroutine returns true if called from the main goroutine.

func SetMainGoroutine

func SetMainGoroutine()

SetMainGoroutine delegates to internal/thread.

Types

type CanvasObjectQueue

type CanvasObjectQueue struct {
	// contains filtered or unexported fields
}

CanvasObjectQueue implements lock-free FIFO freelist based queue.

Reference: https://dl.acm.org/citation.cfm?doid=248052.248106

func NewCanvasObjectQueue

func NewCanvasObjectQueue() *CanvasObjectQueue

NewCanvasObjectQueue returns a queue for caching values.

func (*CanvasObjectQueue) In

In puts the given value at the tail of the queue.

func (*CanvasObjectQueue) Len

func (q *CanvasObjectQueue) Len() uint64

Len returns the length of the queue.

func (*CanvasObjectQueue) Out

Out removes and returns the value at the head of the queue. It returns nil if the queue is empty. It may be called from one goroutine at a time: In from any number.

The item it takes the value from becomes the queue's head, and it lets go of the value: a head that kept it held the last object refreshed, so the last screen an idle app showed, until the next refresh (#1075).

type Map

type Map[K, V any] struct {
	sync.Map
}

Map is a generic wrapper around sync.Map.

func (*Map[K, V]) Clear

func (m *Map[K, V]) Clear()

Clear deletes all the entries, resulting in an empty Map.

func (*Map[K, V]) Delete

func (m *Map[K, V]) Delete(key K)

Delete deletes the value for a key.

func (*Map[K, V]) Len

func (m *Map[K, V]) Len() (count int)

Len returns the length of the map. It is O(n) over the number of items.

func (*Map[K, V]) Load

func (m *Map[K, V]) Load(key K) (value V, ok bool)

Load returns the value stored in the map for a key, or nil if no value is present. The ok result indicates whether value was found in the map.

func (*Map[K, V]) LoadAndDelete

func (m *Map[K, V]) LoadAndDelete(key K) (value V, loaded bool)

LoadAndDelete deletes the value for a key, returning the previous value if any. The loaded result reports whether the key was present.

func (*Map[K, V]) LoadOrStore

func (m *Map[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool)

LoadOrStore returns the existing value for the key if present. Otherwise, it stores and returns the given value. The loaded result is true if the value was loaded, false if stored.

func (*Map[K, V]) Range

func (m *Map[K, V]) Range(f func(key K, value V) bool)

Range calls f sequentially for each key and value present in the map. If f returns false, range stops the iteration.

func (*Map[K, V]) Store

func (m *Map[K, V]) Store(key K, value V)

Store sets the value for a key.

func (*Map[K, V]) Swap added in v1.3.0

func (m *Map[K, V]) Swap(key K, value V) (previous V, loaded bool)

Swap stores value for key and returns the previous value, if any. The loaded result reports whether the key was present.

type Pool

type Pool[T any] struct {

	// New specifies a function to generate
	// a value when Get would otherwise return the zero value of T.
	New func() T
	// contains filtered or unexported fields
}

Pool is the generic version of sync.Pool.

func (*Pool[T]) Get

func (p *Pool[T]) Get() T

Get selects an arbitrary item from the Pool, removes it from the Pool, and returns it to the caller.

func (*Pool[T]) Put

func (p *Pool[T]) Put(x T)

Put adds x to the pool.

type UnboundedChan

type UnboundedChan[T any] struct {
	// contains filtered or unexported fields
}

UnboundedChan is a channel with an unbounded buffer for caching Func objects. A channel must be closed via Close method.

func NewUnboundedChan

func NewUnboundedChan[T any]() *UnboundedChan[T]

NewUnboundedChan returns a unbounded channel with unlimited capacity.

func (*UnboundedChan[T]) Close

func (ch *UnboundedChan[T]) Close()

Close closes the channel.

func (*UnboundedChan[T]) In

func (ch *UnboundedChan[T]) In() chan<- T

In returns the send channel of the given channel, which can be used to send values to the channel.

func (*UnboundedChan[T]) Out

func (ch *UnboundedChan[T]) Out() <-chan T

Out returns the receive channel of the given channel, which can be used to receive values from the channel.

type UnboundedStructChan

type UnboundedStructChan struct {
	// contains filtered or unexported fields
}

UnboundedStructChan is a channel with an unbounded buffer for caching struct{} objects. This implementation is a specialized version that optimizes for struct{} objects than other types. A channel must be closed via Close method.

func NewUnboundedStructChan

func NewUnboundedStructChan() *UnboundedStructChan

NewUnboundedStructChan returns a unbounded channel with unlimited capacity.

func (*UnboundedStructChan) Close

func (ch *UnboundedStructChan) Close()

Close closes the channel.

func (*UnboundedStructChan) In

func (ch *UnboundedStructChan) In() chan<- struct{}

In returns a send-only channel that can be used to send values to the channel.

func (*UnboundedStructChan) Out

func (ch *UnboundedStructChan) Out() <-chan struct{}

Out returns a receive-only channel that can be used to receive values from the channel.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL