prioritywaitqueue

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2022 License: Apache-2.0, MIT Imports: 2 Imported by: 1

README

go-prioritywaitqueue

go-prioritywaitqueue implements a blocking queue for prioritised coordination of goroutine execution.

Install

go get github.com/rvagg/go-prioritywaitqueue

Usage

// global setup
var workerCmp prioritywaitqueue.ComparePriority[*worker] = func(a *worker, b *worker) bool {
	return a.id < b.id
}
queue := prioritywaitqueue.New(workerCmp)

// per-goroutine use
func (w *worker) doSomething() {
  done := w.queue.Wait(w) // will block until this goroutine is allowed to run
  defer done() // signal that the work has completed and another goroutine can run

  // ... work
}

License

Copyright © 2022 Rod Vagg

Licensed under either of

Documentation

Overview

Package prioritywaitqueue implements a blocking queue for prioritised coordination of goroutine execution.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ComparePriority

type ComparePriority[T interface{}] func(a T, b T) bool

ComparePriority should return true if a has a higher priority, and therefore should run, BEFORE b.

type PriorityWaitQueue

type PriorityWaitQueue[T interface{}] interface {
	// Wait is called with with a value that can be prioritised in comparison to
	// other values of the same type. Returns a "done" function that MUST be
	// called when the work to be performed. The call to Wait will block until
	// there are other running goroutines that have also called Wait and not yet
	// called their "done" function.
	//
	// It is up to the caller to handle context cancellation, goroutines should
	// check for themselves when Wait() returns and immediately call done() if
	// a context has cancelled in order to clean up all running goroutines.
	Wait(waitWith T) func()
}

PriorityWaitQueue is a blocking queue for coordinating goroutines, providing a gating mechanism such that only one goroutine may run at a time, where the goroutine allowed to run is chosen based on a priority comparison function.

func New

func New[T interface{}](cmp ComparePriority[T]) PriorityWaitQueue[T]

New creates a new PriorityWaitQueue with the provided ComparePriority function for type T.

Jump to

Keyboard shortcuts

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