made indexAll and retagAll atomic and faster
This commit is contained in:
parent
1fdacd8a93
commit
5649acf6a8
6 changed files with 131 additions and 64 deletions
32
pkg/db/mutex.go
Normal file
32
pkg/db/mutex.go
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
// 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)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue