loop

package
v0.0.0-...-953790c Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2018 License: MPL-2.0, BSD-3-Clause Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrClosed = errors.New("The loop is closed and no longer accepting tasks")

ErrClosed represents the error returned when we try to add or ready a task on a closed loop.

Functions

This section is empty.

Types

type Loop

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

Loop encapsulates the event loop's state. This includes the vm on which the loop operates, a monotonically incrementing event id, a map of tasks that aren't ready yet, keyed by their ID, a channel of tasks that are ready to finalise on the VM, and a boolean that indicates if the loop is still accepting tasks. The channel holding the tasks pending finalising can be buffered or unbuffered.

Warning: id must be the first field in this struct as it's accessed atomically. Otherwise, on ARM and x86-32 it will panic. More information: https://golang.org/pkg/sync/atomic/#pkg-note-BUG.

func New

func New(vm *vm.VM) *Loop

New creates a new Loop with an unbuffered ready queue on a specific VM.

func NewWithBacklog

func NewWithBacklog(vm *vm.VM, backlog int) *Loop

NewWithBacklog creates a new Loop on a specific VM, giving it a buffered queue, the capacity of which being specified by the backlog argument.

func (*Loop) Add

func (l *Loop) Add(t Task) error

Add puts a task into the loop. This signals to the loop that this task is doing something outside of the JavaScript environment, and that at some point, it will become ready for finalising.

func (*Loop) AddAndExecute

func (l *Loop) AddAndExecute(t Task) error

AddAndExecute combines Add and Ready for immediate execution.

func (*Loop) Eval

func (l *Loop) Eval(s interface{}) error

Eval executes some code in the VM associated with the loop and returns an error if that execution fails.

func (*Loop) Ready

func (l *Loop) Ready(t Task) error

Ready signals to the loop that a task is ready to be finalised. This might block if the "ready channel" in the loop is at capacity.

func (*Loop) Remove

func (l *Loop) Remove(t Task)

Remove takes a task out of the loop. This should not be called if a task has already become ready for finalising. Warranty void if constraint is broken.

func (*Loop) Run

func (l *Loop) Run(ctx context.Context) error

Run handles the task scheduling and finalisation. It runs infinitely waiting for new tasks.

func (*Loop) VM

func (l *Loop) VM() *vm.VM

VM gets the JavaScript interpreter associated with the loop.

type Task

type Task interface {
	SetID(id int64)
	GetID() int64
	Execute(vm *vm.VM, l *Loop) error
	Cancel()
}

Task represents something that the event loop can schedule and run.

Task describes two operations that will almost always be boilerplate, SetID and GetID. They exist so that the event loop can identify tasks after they're added.

Execute is called when a task has been pulled from the "ready" queue.

Cancel is called when a task is removed from the loop without being finalised.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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