From 4b64fb1ef2eb5609cdae8b596ab536a0830cb57b Mon Sep 17 00:00:00 2001 From: Noah Metz Date: Mon, 3 Jul 2023 17:13:00 -0600 Subject: [PATCH] Removed state argument from LockLockable and UnlockLockable --- lockable.go | 14 ++++++++------ lockable_test.go | 28 ++++++++++++---------------- thread.go | 4 ++-- 3 files changed, 22 insertions(+), 24 deletions(-) diff --git a/lockable.go b/lockable.go index 38f6357..968d656 100644 --- a/lockable.go +++ b/lockable.go @@ -364,7 +364,7 @@ func checkIfRequirement(ctx * GraphContext, r_id NodeID, cur LockableState, cur_ return false } -func LockLockables(ctx * GraphContext, to_lock []Lockable, holder Lockable, holder_state LockableState, nodes NodeMap) error { +func LockLockables(ctx * GraphContext, to_lock []Lockable, holder Lockable , nodes NodeMap) error { if to_lock == nil { return fmt.Errorf("LOCKABLE_LOCK_ERR: no list provided") } @@ -376,6 +376,7 @@ func LockLockables(ctx * GraphContext, to_lock []Lockable, holder Lockable, hold if holder == nil { return fmt.Errorf("LOCKABLE_LOCK_ERR: nil cannot hold locks") } + holder_state := holder.State().(LockableState) // Called with no requirements to lock, success if len(to_lock) == 0 { @@ -428,7 +429,7 @@ func LockLockables(ctx * GraphContext, to_lock []Lockable, holder Lockable, hold // a) in this case, we're holding every state mutex up to the resource being locked // and all the owners passing a lock, so we can start to change state // 2) req has children, and we will recurse(checking that locking is allowed) until we reach a leaf and can release the locks as we change state. The call will either return nil if state has changed, on an error if no state has changed - err := LockLockables(ctx, req_state.Requirements(), req, req_state, nodes) + err := LockLockables(ctx, req_state.Requirements(), req, nodes) if err != nil { return err } @@ -438,7 +439,7 @@ func LockLockables(ctx * GraphContext, to_lock []Lockable, holder Lockable, hold if owner_state.AllowedToTakeLock(holder.ID(), req.ID()) == false { return fmt.Errorf("LOCKABLE_LOCK_ERR: %s is not allowed to take %s's lock from %s", holder.ID(), req.ID(), owner.ID()) } - err := LockLockables(ctx, req_state.Requirements(), req, req_state, nodes) + err := LockLockables(ctx, req_state.Requirements(), req, nodes) return err }) if err != nil { @@ -446,7 +447,7 @@ func LockLockables(ctx * GraphContext, to_lock []Lockable, holder Lockable, hold } } } else { - err := LockLockables(ctx, req_state.Requirements(), req, req_state, nodes) + err := LockLockables(ctx, req_state.Requirements(), req, nodes) if err != nil { return err } @@ -475,7 +476,7 @@ func LockLockables(ctx * GraphContext, to_lock []Lockable, holder Lockable, hold return err } -func UnlockLockables(ctx * GraphContext, to_unlock []Lockable, holder Lockable, holder_state LockableState, nodes NodeMap) error { +func UnlockLockables(ctx * GraphContext, to_unlock []Lockable, holder Lockable, nodes NodeMap) error { if to_unlock == nil { return fmt.Errorf("LOCKABLE_UNLOCK_ERR: no list provided") } @@ -487,6 +488,7 @@ func UnlockLockables(ctx * GraphContext, to_unlock []Lockable, holder Lockable, if holder == nil { return fmt.Errorf("LOCKABLE_UNLOCK_ERR: nil cannot hold locks") } + holder_state := holder.State().(LockableState) // Called with no requirements to lock, success if len(to_unlock) == 0 { @@ -527,7 +529,7 @@ func UnlockLockables(ctx * GraphContext, to_unlock []Lockable, holder Lockable, return err } - err = UnlockLockables(ctx, req_state.Requirements(), req, req_state, nodes) + err = UnlockLockables(ctx, req_state.Requirements(), req, nodes) if err != nil { return err } diff --git a/lockable_test.go b/lockable_test.go index 9db7dac..a9faae3 100644 --- a/lockable_test.go +++ b/lockable_test.go @@ -63,7 +63,7 @@ func TestLockableSelfLock(t * testing.T) { fatalErr(t, err) err = UpdateStates(ctx, []GraphNode{r1}, func(nodes NodeMap) error { - return LockLockables(ctx, []Lockable{r1}, r1, nil, nodes) + return LockLockables(ctx, []Lockable{r1}, r1, nodes) }) fatalErr(t, err) @@ -77,7 +77,7 @@ func TestLockableSelfLock(t * testing.T) { fatalErr(t, err) err = UpdateStates(ctx, []GraphNode{r1}, func(nodes NodeMap) error { - return UnlockLockables(ctx, []Lockable{r1}, r1, nil, nodes) + return UnlockLockables(ctx, []Lockable{r1}, r1, nodes) }) fatalErr(t, err) @@ -105,7 +105,7 @@ func TestLockableSelfLockTiered(t * testing.T) { fatalErr(t, err) err = UpdateStates(ctx, []GraphNode{r3}, func(nodes NodeMap) error { - return LockLockables(ctx, []Lockable{r3}, r3, nil, nodes) + return LockLockables(ctx, []Lockable{r3}, r3, nodes) }) fatalErr(t, err) @@ -124,7 +124,7 @@ func TestLockableSelfLockTiered(t * testing.T) { fatalErr(t, err) err = UpdateStates(ctx, []GraphNode{r3}, func(nodes NodeMap) error { - return UnlockLockables(ctx, []Lockable{r3}, r3, nil, nodes) + return UnlockLockables(ctx, []Lockable{r3}, r3, nodes) }) fatalErr(t, err) @@ -159,8 +159,7 @@ func TestLockableLockOther(t * testing.T) { fatalErr(t, err) err = UpdateStates(ctx, []GraphNode{r1, r2}, func(nodes NodeMap) (error) { - node_state := r2.State().(LockableState) - err := LockLockables(ctx, []Lockable{r1}, r2, node_state, nodes) + err := LockLockables(ctx, []Lockable{r1}, r2, nodes) fatalErr(t, err) return nil }) @@ -177,8 +176,7 @@ func TestLockableLockOther(t * testing.T) { fatalErr(t, err) err = UpdateStates(ctx, []GraphNode{r2}, func(nodes NodeMap) (error) { - node_state := r2.State().(LockableState) - err := UnlockLockables(ctx, []Lockable{r1}, r2, node_state, nodes) + err := UnlockLockables(ctx, []Lockable{r1}, r2, nodes) fatalErr(t, err) return nil }) @@ -206,13 +204,12 @@ func TestLockableLockSimpleConflict(t * testing.T) { fatalErr(t, err) err = UpdateStates(ctx, []GraphNode{r1}, func(nodes NodeMap) error { - return LockLockables(ctx, []Lockable{r1}, r1, nil, nodes) + return LockLockables(ctx, []Lockable{r1}, r1, nodes) }) fatalErr(t, err) err = UpdateStates(ctx, []GraphNode{r2}, func(nodes NodeMap) (error) { - node_state := r2.State().(LockableState) - err := LockLockables(ctx, []Lockable{r1}, r2, node_state, nodes) + err := LockLockables(ctx, []Lockable{r1}, r2, nodes) if err == nil { t.Fatal("r2 took r1's lock from itself") } @@ -232,7 +229,7 @@ func TestLockableLockSimpleConflict(t * testing.T) { fatalErr(t, err) err = UpdateStates(ctx, []GraphNode{r1}, func(nodes NodeMap) error { - return UnlockLockables(ctx, []Lockable{r1}, r1, nil, nodes) + return UnlockLockables(ctx, []Lockable{r1}, r1, nodes) }) fatalErr(t, err) @@ -261,12 +258,12 @@ func TestLockableLockTieredConflict(t * testing.T) { fatalErr(t, err) err = UpdateStates(ctx, []GraphNode{r2}, func(nodes NodeMap) error { - return LockLockables(ctx, []Lockable{r2}, r2, nil, nodes) + return LockLockables(ctx, []Lockable{r2}, r2, nodes) }) fatalErr(t, err) err = UpdateStates(ctx, []GraphNode{r3}, func(nodes NodeMap) error { - return LockLockables(ctx, []Lockable{r3}, r3, nil, nodes) + return LockLockables(ctx, []Lockable{r3}, r3, nodes) }) if err == nil { t.Fatal("Locked r3 which depends on r1 while r2 which depends on r1 is already locked") @@ -375,8 +372,7 @@ func TestLockableDBLoad(t * testing.T){ fatalErr(t, err) l6, err := NewSimpleLockable(ctx, "Test Lockable 6", []Lockable{}) err = UpdateStates(ctx, []GraphNode{l6, l3}, func(nodes NodeMap) error { - l6_state := l6.State().(LockableState) - err := LockLockables(ctx, []Lockable{l3}, l6, l6_state, nodes) + err := LockLockables(ctx, []Lockable{l3}, l6, nodes) return err }) fatalErr(t, err) diff --git a/thread.go b/thread.go index 4ba8071..e364d9e 100644 --- a/thread.go +++ b/thread.go @@ -450,7 +450,7 @@ func RunThread(ctx * GraphContext, thread Thread, first_action string) error { owner_id = thread_state.Owner().ID() } if owner_id != thread.ID() { - return LockLockables(ctx, []Lockable{thread}, thread, nil, nodes) + return LockLockables(ctx, []Lockable{thread}, thread, nodes) } return nil }) @@ -500,7 +500,7 @@ func RunThread(ctx * GraphContext, thread Thread, first_action string) error { } err = UpdateStates(ctx, []GraphNode{thread}, func(nodes NodeMap) (error) { - return UnlockLockables(ctx, []Lockable{thread}, thread, nil, nodes) + return UnlockLockables(ctx, []Lockable{thread}, thread, nodes) }) if err != nil { ctx.Log.Logf("thread", "THREAD_RUN_UNLOCK_ERR: %e", err)