guarded

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package guarded makes locking discipline structural: a Guarded[T] owns both the mutex and the value it protects, so the value is reachable only through With/Get — there is no way to touch it outside the lock. The convention "fields below mu are guarded by mu" becomes a type.

Authored in G++ and distributed as generated Go — consumers never need the gpp toolchain.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Guarded

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

Guarded is a mutex-owning cell. The zero value is usable when T's zero value is.

func New

func New[T any](v T) *Guarded[T]

New constructs a Guarded holding v.

func (*Guarded[T]) Get

func (g *Guarded[T]) Get() T

Get returns a copy of the value taken under the lock.

func (*Guarded[T]) Set

func (g *Guarded[T]) Set(v T)

Set replaces the value under the lock.

func (*Guarded[T]) With

func (g *Guarded[T]) With(f func(*T))

With runs f over the value under the lock. f receives a pointer so it can mutate in place; the pointer must not escape f.

type RWGuarded

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

RWGuarded is Guarded with reader/writer locking: RWith serializes against With/Set but not against other RWith calls.

func NewRW

func NewRW[T any](v T) *RWGuarded[T]

NewRW constructs an RWGuarded holding v.

func (*RWGuarded[T]) Get

func (g *RWGuarded[T]) Get() T

Get returns a copy of the value taken under the read lock.

func (*RWGuarded[T]) RWith

func (g *RWGuarded[T]) RWith(f func(T))

RWith runs f over the value under the read lock. f must not mutate through aliases it retains; the value must be treated as read-only.

func (*RWGuarded[T]) Set

func (g *RWGuarded[T]) Set(v T)

Set replaces the value under the write lock.

func (*RWGuarded[T]) With

func (g *RWGuarded[T]) With(f func(*T))

With runs f over the value under the write lock.

Jump to

Keyboard shortcuts

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