docstore/pkg/db/mutex.go

32 lines
605 B
Go

// Copyright (C) 2019 Marius Schellenberger
package db
import "sync/atomic"
type countMutex struct {
mu uint64
max uint64
c uint64
}
func (cm *countMutex) Lock() (uint64, uint64, bool) {
if atomic.CompareAndSwapUint64(&cm.mu, 0, 1) {
return 0, 0, true
}
return atomic.LoadUint64(&cm.max), atomic.LoadUint64(&cm.c), false
}
func (cm *countMutex) Set(max uint64) {
atomic.StoreUint64(&cm.max, max)
}
func (cm *countMutex) Inc() {
atomic.AddUint64(&cm.c, 1)
}
func (cm *countMutex) Unlock() {
atomic.StoreUint64(&cm.max, 0)
atomic.StoreUint64(&cm.c, 0)
atomic.StoreUint64(&cm.mu, 0)
}