fiber

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Fiber

type Fiber struct {
	// Data is an arbitrary user-data slot for attaching per-fiber state.
	Data any
	// contains filtered or unexported fields
}

Fiber is a cooperative unit of execution managed by a Scheduler. Each fiber runs in its own goroutine but only one fiber is logically active at a time. Fibers yield control explicitly via Block or SwitchToNew.

type Scheduler

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

Scheduler implements cooperative (non-preemptive) scheduling of fibers. At most one fiber is active at any time; fibers yield control by calling Block or SwitchToNew. Blocking operations run concurrently in their fiber's goroutine, allowing I/O parallelism while maintaining single-threaded logical execution for non-blocking code.

func NewScheduler

func NewScheduler() *Scheduler

NewScheduler creates a new fiber scheduler.

func (*Scheduler) Block

func (s *Scheduler) Block(ctx context.Context, fn func(ctx context.Context))

Block suspends the active fiber, allowing other fibers to run, while fn executes concurrently in this fiber's goroutine. When fn returns, the fiber is re-enqueued and waits to be re-scheduled. The context passed to fn is derived from ctx and can be cancelled via CancelBlocked.

func (*Scheduler) CancelBlocked

func (s *Scheduler) CancelBlocked()

CancelBlocked cancels the context of every currently blocked fiber. This causes the context passed to each Block callback to be cancelled, which should cause the callback to return promptly.

func (*Scheduler) ForEachFiber

func (s *Scheduler) ForEachFiber(h func(fiber *Fiber))

ForEachFiber calls h for every fiber in the scheduler: the active fiber (if any), all ready fibers, and all blocked fibers.

func (*Scheduler) NewFiber

func (s *Scheduler) NewFiber(fn func()) *Fiber

NewFiber creates a new fiber that will execute fn when started. The fiber is not started until it is passed to SwitchToNew or Run.

func (*Scheduler) Run

func (s *Scheduler) Run(main *Fiber)

Run starts the scheduler loop with the given fiber as the initial fiber. It blocks until all fibers (active, ready, and blocked) have terminated.

func (*Scheduler) SwitchToNew

func (s *Scheduler) SwitchToNew(f *Fiber)

SwitchToNew parks the active fiber in the ready queue and immediately starts f. The caller resumes when the scheduler re-schedules it (FIFO).

Jump to

Keyboard shortcuts

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