tailscale/util/ctxlock/state_unchecked.go
Nick Khyl 64e5da8024
util/ctxlock: rename ctxlock.Context to ctxlock.State
Also add additional tests to verify that the unchecked implementation
is allocation-free.

Updates #12614

Signed-off-by: Nick Khyl <nickk@tailscale.com>
2025-05-02 20:38:18 -05:00

36 lines
738 B
Go

// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause
// This file exports optimized implementation of the [State] that omits runtime checks.
// It is used when the build tag ts_omit_ctxlock_checks is set.
//go:build ts_omit_ctxlock_checks
package ctxlock
import (
"context"
"sync"
)
const Checked = false
type State struct {
unchecked
}
func None() State {
return State{}
}
func FromContext(parent context.Context) State {
return State{fromContextUnchecked(parent)}
}
func Lock[T context.Context](parent T, mu *sync.Mutex) State {
if parent, ok := any(parent).(State); ok {
return State{lockUnchecked(parent.unchecked, mu)}
}
return State{lockUnchecked(fromContextUnchecked(parent), mu)}
}