Added update to lock/unlock for resource

graph-rework
noah metz 2023-04-08 15:51:42 -06:00
parent 2b562abe01
commit 3e5b52884f
2 changed files with 22 additions and 0 deletions

@ -149,6 +149,8 @@ type BaseResource struct {
// When the bottom of a tree is reached(no more children) go back up and set the lock state // When the bottom of a tree is reached(no more children) go back up and set the lock state
func (resource * BaseResource) Lock() error { func (resource * BaseResource) Lock() error {
var err error = nil var err error = nil
locked := false
resource.state_lock.Lock() resource.state_lock.Lock()
if resource.locked == true { if resource.locked == true {
err = errors.New("Resource already locked") err = errors.New("Resource already locked")
@ -163,15 +165,23 @@ func (resource * BaseResource) Lock() error {
} }
if all_children_locked == true { if all_children_locked == true {
resource.locked = true resource.locked = true
locked = true
} }
} }
resource.state_lock.Unlock() resource.state_lock.Unlock()
if locked == true {
resource.Update()
}
return err return err
} }
// Recurse through children, unlocking until no more children // Recurse through children, unlocking until no more children
func (resource * BaseResource) Unlock() error { func (resource * BaseResource) Unlock() error {
var err error = nil var err error = nil
unlocked := false
resource.state_lock.Lock() resource.state_lock.Lock()
if resource.locked == false { if resource.locked == false {
err = errors.New("Resource already unlocked") err = errors.New("Resource already unlocked")
@ -186,9 +196,15 @@ func (resource * BaseResource) Unlock() error {
} }
if all_children_unlocked == true{ if all_children_unlocked == true{
resource.locked = false resource.locked = false
unlocked = true
} }
} }
resource.state_lock.Unlock() resource.state_lock.Unlock()
if unlocked == true {
resource.Update()
}
return err return err
} }

@ -161,11 +161,14 @@ func TestLockResource(t * testing.T) {
t.Fatal("Failed to add initial tiered resources for test") t.Fatal("Failed to add initial tiered resources for test")
} }
r1_l := r1.UpdateChannel()
// Lock r3(so also r1&r2) // Lock r3(so also r1&r2)
err := r3.Lock() err := r3.Lock()
if err != nil { if err != nil {
t.Fatal("Failed to lock r3") t.Fatal("Failed to lock r3")
} }
(*graph_tester)(t).CheckForNil(r1_l)
err = r3.Lock() err = r3.Lock()
if err == nil { if err == nil {
@ -186,14 +189,17 @@ func TestLockResource(t * testing.T) {
if err != nil { if err != nil {
t.Fatal("Failed to unlock r3") t.Fatal("Failed to unlock r3")
} }
(*graph_tester)(t).CheckForNil(r1_l)
err = r4.Lock() err = r4.Lock()
if err != nil { if err != nil {
t.Fatal("Failed to lock r4 after unlocking r3") t.Fatal("Failed to lock r4 after unlocking r3")
} }
(*graph_tester)(t).CheckForNil(r1_l)
err = r4.Unlock() err = r4.Unlock()
if err != nil { if err != nil {
t.Fatal("Failed to unlock r4") t.Fatal("Failed to unlock r4")
} }
(*graph_tester)(t).CheckForNil(r1_l)
} }