"reflect"
g "github.com/anacrolix/generics"
+ "github.com/anacrolix/missinggo/v2/panicif"
"github.com/anacrolix/sync"
)
unlockActions []func()
uniqueActions map[any]struct{}
// Currently unlocking, defers should not occur?
- unlocking bool
+ allowDefers bool
}
func (me *lockWithDeferreds) Lock() {
me.internal.Lock()
+ panicif.True(me.allowDefers)
+ me.allowDefers = true
}
func (me *lockWithDeferreds) Unlock() {
- me.unlocking = true
+ panicif.False(me.allowDefers)
+ me.allowDefers = false
startLen := len(me.unlockActions)
var i int
for i = 0; i < len(me.unlockActions); i++ {
}
me.unlockActions = me.unlockActions[:0]
me.uniqueActions = nil
- me.unlocking = false
me.internal.Unlock()
}
// Not allowed after unlock has started.
func (me *lockWithDeferreds) Defer(action func()) {
- if me.unlocking {
- panic("defer called while unlocking")
- }
me.deferInner(action)
}
// Already guarded.
func (me *lockWithDeferreds) deferInner(action func()) {
+ panicif.False(me.allowDefers)
me.unlockActions = append(me.unlockActions, action)
}
// Protected from looping by once filter.
func (me *lockWithDeferreds) deferOnceInner(key any, action func()) {
+ panicif.False(me.allowDefers)
g.MakeMapIfNil(&me.uniqueActions)
if g.MapContains(me.uniqueActions, key) {
return