mirror of
https://github.com/fluencelabs/go-libp2p-kad-dht
synced 2025-04-24 22:32:13 +00:00
29 lines
354 B
Go
29 lines
354 B
Go
|
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")
|
||
|
}
|
||
|
}
|