1
0
Fork 0
mirror of https://github.com/anyproto/any-sync.git synced 2025-06-11 10:18:08 +09:00
any-sync/commonspace/sync/guard.go
2024-06-09 00:53:38 +02:00

30 lines
449 B
Go

package sync
import "sync"
type guard struct {
mu sync.Mutex
taken map[string]struct{}
}
func newGuard() *guard {
return &guard{
taken: make(map[string]struct{}),
}
}
func (g *guard) TryTake(id string) bool {
g.mu.Lock()
defer g.mu.Unlock()
if _, exists := g.taken[id]; exists {
return false
}
g.taken[id] = struct{}{}
return true
}
func (g *guard) Release(id string) {
g.mu.Lock()
defer g.mu.Unlock()
delete(g.taken, id)
}