util/ctxlock: remove AssertLocked usage from the example

Updates #12614

Signed-off-by: Nick Khyl <nickk@tailscale.com>
This commit is contained in:
Nick Khyl 2025-05-01 18:27:13 -05:00
parent a414b760a2
commit 7df612309e
No known key found for this signature in database

View File

@ -18,25 +18,21 @@ type Resource struct {
func (r *Resource) GetFoo(ctx ctxlock.Context) string {
defer ctxlock.Lock(ctx, &r.mu).Unlock() // Lock the mutex if not already held.
syncs.AssertLocked(&r.mu) // Panic if mu is still unlocked.
return r.foo
}
func (r *Resource) SetFoo(ctx ctxlock.Context, foo string) {
defer ctxlock.Lock(ctx, &r.mu).Unlock()
syncs.AssertLocked(&r.mu)
r.foo = foo
}
func (r *Resource) GetBar(ctx ctxlock.Context) string {
defer ctxlock.Lock(ctx, &r.mu).Unlock()
syncs.AssertLocked(&r.mu)
return r.bar
}
func (r *Resource) SetBar(ctx ctxlock.Context, bar string) {
defer ctxlock.Lock(ctx, &r.mu).Unlock()
syncs.AssertLocked(&r.mu)
r.bar = bar
}
@ -44,7 +40,6 @@ func (r *Resource) WithLock(ctx ctxlock.Context, f func(ctx ctxlock.Context)) {
// Lock the mutex if not already held, and get a new context.
ctx = ctxlock.Lock(ctx, &r.mu)
defer ctx.Unlock()
syncs.AssertLocked(&r.mu)
f(ctx) // Call the callback with the new context.
}