shardvalue_tailscale.go 645 B

123456789101112131415161718192021222324
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. // TODO(raggi): update build tag after toolchain update
  4. //go:build tailscale_go
  5. package syncs
  6. import (
  7. "runtime"
  8. )
  9. //lint:ignore U1000 unused under tailscale_go builds.
  10. type shardValuePool struct{}
  11. // NewShardValue constructs a new ShardValue[T] with a shard per CPU.
  12. func NewShardValue[T any]() *ShardValue[T] {
  13. return &ShardValue[T]{shards: make([]T, runtime.NumCPU())}
  14. }
  15. // One yields a pointer to a single shard value with best-effort P-locality.
  16. func (sp *ShardValue[T]) One(f func(*T)) {
  17. f(&sp.shards[runtime.TailscaleCurrentP()%len(sp.shards)])
  18. }