snapshots: Handle as separate components
This commit is contained in:
parent
698f04e13e
commit
3b112f1b8e
27 changed files with 960 additions and 789 deletions
44
pkg/rlock/rlock.go
Normal file
44
pkg/rlock/rlock.go
Normal 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()
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue