contextcond

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 27, 2023 License: BSD-3-Clause Imports: 4 Imported by: 0

README

contextcond

Go Reference

This library provides a sync.Cond that has a WaitContext() method.

WaitContext(context.Context) waits for either the condvar to wake it, or the context to be cancelled. It returns nil if the condvar woke it, or ctx.Err() if the context was cancelled.

Benchmark

contextcond is slower than sync.Cond, so you should only use it if you need WaitContext().

$ go test -bench .
goos: linux
goarch: amd64
pkg: github.com/Jille/contextcond
cpu: Intel(R) Xeon(R) CPU E5-2650 0 @ 2.00GHz
BenchmarkCond1-8          560541              1968 ns/op
BenchmarkCond2-8          196060              7049 ns/op
BenchmarkCond4-8           95750             12685 ns/op
BenchmarkCond8-8           49628             25710 ns/op
BenchmarkCond16-8          34155             37504 ns/op
BenchmarkCond32-8          14602             82923 ns/op

Compared to sync.Cond:

$ go test -bench . /usr/lib/go-1.19/src/sync/cond_test.go
goos: linux
goarch: amd64
cpu: Intel(R) Xeon(R) CPU E5-2650 0 @ 2.00GHz
BenchmarkCond1-8         1509849               782.1 ns/op
BenchmarkCond2-8          447687              2589 ns/op
BenchmarkCond4-8          251739              5226 ns/op
BenchmarkCond8-8          118968             11194 ns/op
BenchmarkCond16-8          44931             24098 ns/op
BenchmarkCond32-8          28124             40405 ns/op

Documentation

Overview

Package contextcond provides a sync.Cond that has a WaitContext method.

Internally it's implemented using channels and mutexes.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cond

type Cond struct {
	// L is held while observing or changing the condition
	L sync.Locker
	// contains filtered or unexported fields
}

Cond is like sync.Cond with a WaitContext method. Unlike sync.Cond, Cond must be initialized with NewCond.

func NewCond

func NewCond(l sync.Locker) *Cond

NewCond returns a new Cond with Locker l.

func (*Cond) Broadcast

func (c *Cond) Broadcast()

Broadcast wakes all goroutines waiting on c.

It is allowed but not required for the caller to hold c.L during the call.

func (*Cond) Signal

func (c *Cond) Signal()

Signal wakes one goroutine waiting on c, if there is any.

It is allowed but not required for the caller to hold c.L during the call.

Signal() does not affect goroutine scheduling priority; if other goroutines are attempting to lock c.L, they may be awoken before a "waiting" goroutine.

func (*Cond) Wait

func (c *Cond) Wait()

Wait atomically unlocks c.L and suspends execution of the calling goroutine. After later resuming execution, Wait locks c.L before returning. Unlike in other systems, Wait cannot return unless awoken by Broadcast or Signal. Because c.L is not locked while Wait is waiting, the caller typically cannot assume that the condition is true when Wait returns. Instead, the caller should Wait in a loop.

func (*Cond) WaitContext

func (c *Cond) WaitContext(ctx context.Context) error

WaitContext is like wait, but returns a non-nil error iff the context was cancelled.

Jump to

Keyboard shortcuts

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