| 1 |
// Copyright 2025 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package hsync |
| 15 |
|
| 16 |
import ( |
| 17 |
"context" |
| 18 |
"sync" |
| 19 |
"sync/atomic" |
| 20 |
) |
| 21 |
|
| 22 |
// OnceMore is similar to sync.Once. |
| 23 |
// |
| 24 |
// Additional features are: |
| 25 |
// * it can be reset, so the action can be repeated if needed |
| 26 |
// * it has methods to check if it's done or in progress |
| 27 |
type OnceMore struct { |
| 28 |
_ doNotCopy |
| 29 |
done atomic.Bool |
| 30 |
mu sync.Mutex |
| 31 |
} |
| 32 |
|
| 33 |
func (t *OnceMore) Do(f func()) { |
| 34 |
if t.Done() { |
| 35 |
return |
| 36 |
} |
| 37 |
|
| 38 |
t.mu.Lock() |
| 39 |
defer t.mu.Unlock() |
| 40 |
|
| 41 |
// Double check |
| 42 |
if t.Done() { |
| 43 |
return |
| 44 |
} |
| 45 |
|
| 46 |
defer t.done.Store(true) |
| 47 |
f() |
| 48 |
} |
| 49 |
|
| 50 |
func (t *OnceMore) Done() bool { |
| 51 |
return t.done.Load() |
| 52 |
} |
| 53 |
|
| 54 |
func (t *OnceMore) Reset() { |
| 55 |
t.mu.Lock() |
| 56 |
t.done.Store(false) |
| 57 |
t.mu.Unlock() |
| 58 |
} |
| 59 |
|
| 60 |
type ValueResetter[T any] struct { |
| 61 |
reset func() |
| 62 |
f func(context.Context) T |
| 63 |
} |
| 64 |
|
| 65 |
func (v *ValueResetter[T]) Value(ctx context.Context) T { |
| 66 |
return v.f(ctx) |
| 67 |
} |
| 68 |
|
| 69 |
func (v *ValueResetter[T]) Reset() { |
| 70 |
v.reset() |
| 71 |
} |
| 72 |
|
| 73 |
// OnceMoreValue returns a function that invokes f only once and returns the value |
| 74 |
// returned by f. The returned function may be called concurrently. |
| 75 |
// |
| 76 |
// If f panics, the returned function will panic with the same value on every call. |
| 77 |
func OnceMoreValue[T any](f func(context.Context) T) ValueResetter[T] { |
| 78 |
v := struct { |
| 79 |
f func(context.Context) T |
| 80 |
once OnceMore |
| 81 |
ok bool |
| 82 |
p any |
| 83 |
result T |
| 84 |
}{ |
| 85 |
f: f, |
| 86 |
} |
| 87 |
ff := func(ctx context.Context) T { |
| 88 |
v.once.Do(func() { |
| 89 |
v.ok = false |
| 90 |
defer func() { |
| 91 |
v.p = recover() |
| 92 |
if !v.ok { |
| 93 |
panic(v.p) |
| 94 |
} |
| 95 |
}() |
| 96 |
v.result = v.f(ctx) |
| 97 |
v.ok = true |
| 98 |
}) |
| 99 |
if !v.ok { |
| 100 |
panic(v.p) |
| 101 |
} |
| 102 |
return v.result |
| 103 |
} |
| 104 |
|
| 105 |
return ValueResetter[T]{ |
| 106 |
reset: v.once.Reset, |
| 107 |
f: ff, |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
type FuncResetter struct { |
| 112 |
f func(context.Context) error |
| 113 |
reset func() |
| 114 |
} |
| 115 |
|
| 116 |
func (v *FuncResetter) Do(ctx context.Context) error { |
| 117 |
return v.f(ctx) |
| 118 |
} |
| 119 |
|
| 120 |
func (v *FuncResetter) Reset() { |
| 121 |
v.reset() |
| 122 |
} |
| 123 |
|
| 124 |
func OnceMoreFunc(f func(context.Context) error) FuncResetter { |
| 125 |
v := struct { |
| 126 |
f func(context.Context) error |
| 127 |
once OnceMore |
| 128 |
ok bool |
| 129 |
err error |
| 130 |
p any |
| 131 |
}{ |
| 132 |
f: f, |
| 133 |
} |
| 134 |
ff := func(ctx context.Context) error { |
| 135 |
v.once.Do(func() { |
| 136 |
v.ok = false |
| 137 |
defer func() { |
| 138 |
v.p = recover() |
| 139 |
if !v.ok { |
| 140 |
panic(v.p) |
| 141 |
} |
| 142 |
}() |
| 143 |
v.err = v.f(ctx) |
| 144 |
v.ok = true |
| 145 |
}) |
| 146 |
if !v.ok { |
| 147 |
panic(v.p) |
| 148 |
} |
| 149 |
return v.err |
| 150 |
} |
| 151 |
|
| 152 |
return FuncResetter{ |
| 153 |
f: ff, |
| 154 |
reset: v.once.Reset, |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
type doNotCopy struct{} |
| 159 |
|
| 160 |
// Lock is a no-op used by -copylocks checker from `go vet`. |
| 161 |
func (*doNotCopy) Lock() {} |
| 162 |
func (*doNotCopy) Unlock() {} |