snapshots: Handle as separate components

This commit is contained in:
Tom Wiesing 2022-10-02 18:17:47 +02:00
parent 698f04e13e
commit 3b112f1b8e
No known key found for this signature in database
27 changed files with 960 additions and 789 deletions

44
pkg/rlock/rlock.go Normal file
View file

@ -0,0 +1,44 @@
package rlock
import (
"sync"
"time"
)
type RLock struct {
m sync.Mutex // m is held internally
held bool
holder int
counter uint64
}
func (rm *RLock) Lock(id int) {
for {
rm.m.Lock()
if !rm.held {
rm.held = true
rm.holder = id
break
} else if rm.held && rm.holder == id {
break
} else {
rm.m.Unlock()
time.Sleep(time.Millisecond)
continue
}
}
rm.counter++
rm.m.Unlock()
}
func (rm *RLock) Unlock() {
rm.m.Lock()
rm.counter--
if rm.counter == 0 {
rm.held = false
rm.holder = 0
}
rm.m.Unlock()
}