@@ -0,0 +1,35 @@
|
||||
package dtypes
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
|
||||
"github.com/filecoin-project/go-address"
|
||||
)
|
||||
|
||||
type MpoolLocker struct {
|
||||
m map[address.Address]chan struct{}
|
||||
lk sync.Mutex
|
||||
}
|
||||
|
||||
func (ml *MpoolLocker) TakeLock(ctx context.Context, a address.Address) (func(), error) {
|
||||
ml.lk.Lock()
|
||||
if ml.m == nil {
|
||||
ml.m = make(map[address.Address]chan struct{})
|
||||
}
|
||||
lk, ok := ml.m[a]
|
||||
if !ok {
|
||||
lk = make(chan struct{}, 1)
|
||||
ml.m[a] = lk
|
||||
}
|
||||
ml.lk.Unlock()
|
||||
|
||||
select {
|
||||
case lk <- struct{}{}:
|
||||
case <-ctx.Done():
|
||||
return nil, ctx.Err()
|
||||
}
|
||||
return func() {
|
||||
<-lk
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user