go-libp2p-kad-dht/ctx_mutex.go

29 lines
354 B
Go
Raw Permalink Normal View History

package dht
import (
"context"
)
type ctxMutex chan struct{}
func newCtxMutex() ctxMutex {
return make(ctxMutex, 1)
}
func (m ctxMutex) Lock(ctx context.Context) error {
select {
case m <- struct{}{}:
return nil
case <-ctx.Done():
return ctx.Err()
}
}
func (m ctxMutex) Unlock() {
select {
case <-m:
default:
panic("not locked")
}
}