eventloop

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2026 License: MIT Imports: 8 Imported by: 0

README

#eventloop

通用事件队列 为什么不用无锁队列?因为目前框架无锁队列性能并不比 DoubleBuffQueue 性能高太多。 而且无锁队列目前并没有好的稳定的实现,没有明显优势

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AbsFrameLoop added in v1.0.1

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

AbsFrameLoop is a single-goroutine fixed-rate frame loop that schedules frames against absolute time: frame N is due at epoch + N*interval, where epoch is the moment the loop started. Sleep jitter and frame cost do not accumulate — if a frame finishes late the loop runs subsequent frames without sleeping until it is back on schedule, so the long-run average rate is exactly fps. delta passed to the frame callback is always the fixed interval (a deterministic logical timestep).

The zero value is not usable; construct one with NewAbsFrameLoop.

func NewAbsFrameLoop added in v1.0.1

func NewAbsFrameLoop(fps int, lockOSThread bool) *AbsFrameLoop

NewAbsFrameLoop allocates an AbsFrameLoop running at fps frames per second. Panics if fps is not positive. When lockOSThread is true the loop goroutine pins itself to its OS thread via runtime.LockOSThread.

func (*AbsFrameLoop) Interval added in v1.0.1

func (l *AbsFrameLoop) Interval() time.Duration

Interval returns the fixed frame duration (1s / fps).

func (*AbsFrameLoop) PostEventQueue added in v1.0.1

func (q *AbsFrameLoop) PostEventQueue(e any)

PostEventQueue enqueues event e for processing at the next frame boundary. If the loop has already been stopped the call is a no-op. Safe for concurrent use by multiple goroutines.

func (*AbsFrameLoop) SetMaxCatchUpFrames added in v1.0.1

func (l *AbsFrameLoop) SetMaxCatchUpFrames(n int64)

SetMaxCatchUpFrames overrides how far behind schedule the loop may fall before it skips the lost frames instead of catching up one by one. Must be called before Start; n <= 0 is ignored.

func (*AbsFrameLoop) Start added in v1.0.1

func (l *AbsFrameLoop) Start(frame FrameFunc, handle func(event any), endF ...func())

Start launches the frame-loop goroutine. Each frame first drains all events queued via PostEventQueue through handle (func() events are invoked directly), then calls frame. Optional endF callbacks run in order after the final drain when the loop stops.

func (*AbsFrameLoop) Stop added in v1.0.1

func (q *AbsFrameLoop) Stop()

Stop signals the loop to stop and blocks until it has drained remaining events and exited. Safe to call more than once.

func (*AbsFrameLoop) Stopped added in v1.0.1

func (q *AbsFrameLoop) Stopped() bool

Stopped reports whether the loop has been stopped.

type CatchUpFrameLoop added in v1.0.1

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

CatchUpFrameLoop is a single-goroutine fixed-rate frame loop that derives the target frame count from elapsed run time: target = (now - epoch) / interval. Whenever the number of frames actually run is below target it runs one frame immediately — with no sleep in between and no upper bound on the backlog — so every logical frame is eventually executed even under sustained overload. Only when fully caught up does the loop sleep until the next frame is due. delta passed to the frame callback is always the fixed interval (a deterministic logical timestep).

Compared with AbsFrameLoop this loop never skips frames; a long stall is paid back in full as a catch-up burst. Use it when every logical frame must run exactly once (e.g. lockstep simulation), and accept that wall-clock latency grows while the backlog drains.

The zero value is not usable; construct one with NewCatchUpFrameLoop.

func NewCatchUpFrameLoop added in v1.0.1

func NewCatchUpFrameLoop(fps int, lockOSThread bool) *CatchUpFrameLoop

NewCatchUpFrameLoop allocates a CatchUpFrameLoop running at fps frames per second. Panics if fps is not positive. When lockOSThread is true the loop goroutine pins itself to its OS thread via runtime.LockOSThread.

func (*CatchUpFrameLoop) Interval added in v1.0.1

func (l *CatchUpFrameLoop) Interval() time.Duration

Interval returns the fixed frame duration (1s / fps).

func (*CatchUpFrameLoop) PostEventQueue added in v1.0.1

func (q *CatchUpFrameLoop) PostEventQueue(e any)

PostEventQueue enqueues event e for processing at the next frame boundary. If the loop has already been stopped the call is a no-op. Safe for concurrent use by multiple goroutines.

func (*CatchUpFrameLoop) Start added in v1.0.1

func (l *CatchUpFrameLoop) Start(frame FrameFunc, handle func(event any), endF ...func())

Start launches the frame-loop goroutine. Each frame first drains all events queued via PostEventQueue through handle (func() events are invoked directly), then calls frame. Optional endF callbacks run in order after the final drain when the loop stops.

func (*CatchUpFrameLoop) Stop added in v1.0.1

func (q *CatchUpFrameLoop) Stop()

Stop signals the loop to stop and blocks until it has drained remaining events and exited. Safe to call more than once.

func (*CatchUpFrameLoop) Stopped added in v1.0.1

func (q *CatchUpFrameLoop) Stopped() bool

Stopped reports whether the loop has been stopped.

type DoubleBuffQueue

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

DoubleBuffQueue is a lock-minimised, double-buffered event queue that drives a single-goroutine event loop. One buffer accumulates incoming events while the other is being drained by the loop goroutine; the two slices are swapped under the mutex, keeping contention extremely short.

The zero value is not usable; construct one with NewDoubleBuffQueue. Written by Claude Code claude-opus-4-6.

func NewDoubleBuffQueue

func NewDoubleBuffQueue(lockOSThread bool) *DoubleBuffQueue

NewDoubleBuffQueue allocates and initialises a DoubleBuffQueue. When lockOSThread is true the internal loop goroutine calls runtime.LockOSThread, which is required by some OS-thread-affine resources such as OpenGL or certain CGo libraries. Written by Claude Code claude-opus-4-6.

func (*DoubleBuffQueue) AfterFunc

func (this_ *DoubleBuffQueue) AfterFunc(duration time.Duration, f func())

AfterFunc schedules f to be posted into the event queue after the specified duration, delegating to the timer package. The callback is executed on the event-loop goroutine, not on a timer goroutine. Written by Claude Code claude-opus-4-6.

func (*DoubleBuffQueue) PostEventQueue

func (this_ *DoubleBuffQueue) PostEventQueue(e interface{})

PostEventQueue enqueues event e for delivery to the handler registered via Start. If the queue has already been stopped the call is a no-op. PostEventQueue is safe for concurrent use by multiple goroutines. Written by Claude Code claude-opus-4-6.

func (*DoubleBuffQueue) Start

func (this_ *DoubleBuffQueue) Start(f func(event any), endF ...func())

Start launches the background event-loop goroutine. f is called for every posted event. As a convenience, if e is itself a func() it is called directly instead of being passed to f, enabling deferred work to be posted without a wrapping handler.

Zero or more endF functions may be supplied; they are executed in order after the queue has been drained following Stop. Each endF is protected by a recover so a panic in one does not prevent the others from running. Written by Claude Code claude-opus-4-6.

func (*DoubleBuffQueue) Stop

func (this_ *DoubleBuffQueue) Stop()

Stop signals the event loop to stop, drains any remaining events, executes the endF callbacks registered via Start, and waits until the loop goroutine has exited before returning. Callers must not post new events after Stop returns. Written by Claude Code claude-opus-4-6.

func (*DoubleBuffQueue) Stopped

func (this_ *DoubleBuffQueue) Stopped() bool

Stopped reports whether the queue has been stopped. Written by Claude Code claude-opus-4-6.

func (*DoubleBuffQueue) Ticker

func (this_ *DoubleBuffQueue) Ticker(interval time.Duration, f func() bool)

Ticker schedules f to be invoked on the event-loop goroutine every interval. f must return true to continue ticking and false to stop. If the queue is already stopped when Ticker is called the call is a no-op. Written by Claude Code claude-opus-4-6.

func (*DoubleBuffQueue) UntilFunc

func (this_ *DoubleBuffQueue) UntilFunc(t time.Time, f func())

UntilFunc schedules f to be posted into the event queue at the wall-clock time t. If t is in the past, the callback fires within one millisecond. Written by Claude Code claude-opus-4-6.

type DoubleBuffQueue2

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

DoubleBuffQueue2 is the generic counterpart of DoubleBuffQueue. Instead of accepting interface{} events it is parameterised over T, eliminating the type-assertion overhead in the handler and allowing the compiler to enforce type safety at the call site.

The zero value is not usable; construct one with NewDoubleBuffQueue2. Written by Claude Code claude-opus-4-6.

func NewDoubleBuffQueue2

func NewDoubleBuffQueue2[T any](lockOSThread bool) *DoubleBuffQueue2[T]

NewDoubleBuffQueue2 allocates and initialises a DoubleBuffQueue2[T]. When lockOSThread is true the internal loop goroutine pins itself to the current OS thread via runtime.LockOSThread. Written by Claude Code claude-opus-4-6.

func (*DoubleBuffQueue2[T]) PostEventQueue

func (this_ *DoubleBuffQueue2[T]) PostEventQueue(e T)

PostEventQueue enqueues a typed event e for delivery to the handler registered via Start. If the queue has already been stopped the call is a no-op. Safe for concurrent use by multiple goroutines. Written by Claude Code claude-opus-4-6.

func (*DoubleBuffQueue2[T]) Start

func (this_ *DoubleBuffQueue2[T]) Start(f func(T), endF ...func())

Start launches the background event-loop goroutine. f is called for every posted event with a recover guard so panics do not crash the loop. Optional endF callbacks are executed in order after the queue drains on Stop. Written by Claude Code claude-opus-4-6.

func (*DoubleBuffQueue2[T]) Stop

func (this_ *DoubleBuffQueue2[T]) Stop()

Stop signals the event loop to stop, drains remaining events, runs endF callbacks, and blocks until the loop goroutine exits. Written by Claude Code claude-opus-4-6.

func (*DoubleBuffQueue2[T]) Stopped

func (this_ *DoubleBuffQueue2[T]) Stopped() bool

Stopped reports whether the queue has been stopped. Written by Claude Code claude-opus-4-6.

type FrameFunc added in v1.0.1

type FrameFunc func(frameCount int64, delta time.Duration)

FrameFunc is the per-frame callback of a frame loop. frameCount starts at 1 and increases monotonically; delta is the logical timestep for AbsFrameLoop (always the fixed frame interval) and the actual elapsed time since the previous frame for TickerFrameLoop.

type TickerFrameLoop added in v1.0.1

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

TickerFrameLoop is a single-goroutine fixed-rate frame loop driven by a time.Ticker — the most common game-loop implementation. Each tick runs one frame; when a frame overruns the interval the ticker drops the missed ticks, so the loop never runs catch-up frames and the effective rate simply slips below fps under sustained load. delta passed to the frame callback is the actual elapsed time since the previous frame.

The zero value is not usable; construct one with NewTickerFrameLoop.

func NewTickerFrameLoop added in v1.0.1

func NewTickerFrameLoop(fps int, lockOSThread bool) *TickerFrameLoop

NewTickerFrameLoop allocates a TickerFrameLoop running at fps frames per second. Panics if fps is not positive. When lockOSThread is true the loop goroutine pins itself to its OS thread via runtime.LockOSThread.

func (*TickerFrameLoop) Interval added in v1.0.1

func (l *TickerFrameLoop) Interval() time.Duration

Interval returns the fixed frame duration (1s / fps).

func (*TickerFrameLoop) PostEventQueue added in v1.0.1

func (q *TickerFrameLoop) PostEventQueue(e any)

PostEventQueue enqueues event e for processing at the next frame boundary. If the loop has already been stopped the call is a no-op. Safe for concurrent use by multiple goroutines.

func (*TickerFrameLoop) Start added in v1.0.1

func (l *TickerFrameLoop) Start(frame FrameFunc, handle func(event any), endF ...func())

Start launches the frame-loop goroutine. Each tick first drains all events queued via PostEventQueue through handle (func() events are invoked directly), then calls frame. Optional endF callbacks run in order after the final drain when the loop stops.

func (*TickerFrameLoop) Stop added in v1.0.1

func (q *TickerFrameLoop) Stop()

Stop signals the loop to stop and blocks until it has drained remaining events and exited. Safe to call more than once.

func (*TickerFrameLoop) Stopped added in v1.0.1

func (q *TickerFrameLoop) Stopped() bool

Stopped reports whether the loop has been stopped.

Jump to

Keyboard shortcuts

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