mirror of
https://github.com/tailscale/tailscale.git
synced 2025-06-12 18:58:36 +00:00

Also add additional tests to verify that the unchecked implementation is allocation-free. Updates #12614 Signed-off-by: Nick Khyl <nickk@tailscale.com>
36 lines
738 B
Go
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)}
|
|
}
|