Added UnlinkLockables and UnlinkThreads

graph-rework-2
noah metz 2023-07-03 16:37:54 -06:00
parent 64171c6c85
commit 5bdc06bf0f
3 changed files with 123 additions and 120 deletions

@ -560,9 +560,17 @@ func NewGQLThread(ctx * GraphContext, listen string, requirements []Lockable) (*
http_done: &sync.WaitGroup{},
}
err = LinkLockables(ctx, thread, requirements)
if len(requirements) > 0 {
req_nodes := make([]GraphNode, len(requirements))
for i, req := range(requirements) {
req_nodes[i] = req
}
err = UpdateStates(ctx, req_nodes, func(nodes NodeMap) error {
return LinkLockables(ctx, thread, requirements, nodes)
})
if err != nil {
return nil, err
}
}
return thread, nil
}

@ -195,9 +195,8 @@ func (state * BaseLockableState) RemoveRequirement(requirement Lockable) {
state.requirements = state.requirements[0:(req_len-1)]
}
// Requires lockable and requirement's states to be locked for write
func UnlinkLockables(ctx * GraphContext, lockable Lockable, requirement Lockable) error {
// Check if requirement is a requirement of lockable
err := UpdateStates(ctx, []GraphNode{lockable}, func(nodes NodeMap) error{
state := lockable.State().(LockableState)
var found GraphNode = nil
for _, req := range(state.Requirements()) {
@ -211,20 +210,15 @@ func UnlinkLockables(ctx * GraphContext, lockable Lockable, requirement Lockable
return fmt.Errorf("UNLINK_LOCKABLES_ERR: %s is not a requirement of %s", requirement.ID(), lockable.ID())
}
err := UpdateMoreStates(ctx, []GraphNode{found}, nodes, func(nodes NodeMap) error {
req_state := found.State().(LockableState)
req_state.RemoveDependency(lockable)
state.RemoveRequirement(requirement)
return nil
})
return err
})
return err
return nil
}
func LinkLockables(ctx * GraphContext, lockable Lockable, requirements []Lockable) error {
// Requires lockable and requirements to be locked for write, nodes passed because requirement check recursively locks
func LinkLockables(ctx * GraphContext, lockable Lockable, requirements []Lockable, nodes NodeMap) error {
if lockable == nil {
return fmt.Errorf("LOCKABLE_LINK_ERR: Will not link Lockables to nil as requirements")
}
@ -250,13 +244,6 @@ func LinkLockables(ctx * GraphContext, lockable Lockable, requirements []Lockabl
found[requirement.ID()] = true
}
gnodes := make([]GraphNode, len(requirements) + 1)
gnodes[0] = lockable
for i, node := range(requirements) {
gnodes[i+1] = node
}
err := UpdateStates(ctx, gnodes, func(nodes NodeMap) error {
// Check that all the requirements can be added
lockable_state := lockable.State().(LockableState)
// If the lockable is already locked, need to lock this resource as well before we can add it
@ -299,10 +286,6 @@ func LinkLockables(ctx * GraphContext, lockable Lockable, requirements []Lockabl
// Return no error
return nil
})
return err
}
func NewBaseLockableState(name string, _type string) BaseLockableState {
@ -711,11 +694,21 @@ func NewSimpleLockable(ctx * GraphContext, name string, requirements []Lockable)
if err != nil {
return nil, err
}
lockable_ptr := &lockable
err = LinkLockables(ctx, lockable_ptr, requirements)
if len(requirements) > 0 {
req_nodes := make([]GraphNode, len(requirements))
for i, req := range(requirements) {
req_nodes[i] = req
}
err = UpdateStates(ctx, req_nodes, func(nodes NodeMap) error {
return LinkLockables(ctx, lockable_ptr, requirements, nodes)
})
if err != nil {
return nil, err
}
}
return lockable_ptr, nil
}

@ -261,8 +261,8 @@ func (state * BaseThreadState) ChildInfo(child NodeID) ThreadInfo {
return state.child_info[child]
}
// Requires thread and childs state to be locked for write
func UnlinkThreads(ctx * GraphContext, thread Thread, child Thread) error {
err := UpdateStates(ctx, []GraphNode{thread}, func(nodes NodeMap) error{
state := thread.State().(ThreadState)
var found GraphNode = nil
for _, c := range(state.Children()) {
@ -276,15 +276,11 @@ func UnlinkThreads(ctx * GraphContext, thread Thread, child Thread) error {
return fmt.Errorf("UNLINK_THREADS_ERR: %s is not a child of %s", child.ID(), thread.ID())
}
err := UpdateMoreStates(ctx, []GraphNode{found}, nodes, func(nodes NodeMap) error {
child_state := found.State().(ThreadState)
child_state := child.State().(ThreadState)
child_state.SetParent(nil)
state.RemoveChild(child)
return nil
})
return err
})
return err
}
func (state * BaseThreadState) RemoveChild(child Thread) {
@ -348,6 +344,7 @@ func checkIfChild(ctx * GraphContext, thread_id NodeID, cur_state ThreadState, c
return false
}
// Requires thread and childs state to be locked for write
func LinkThreads(ctx * GraphContext, thread Thread, child Thread, info ThreadInfo) error {
if ctx == nil || thread == nil || child == nil {
return fmt.Errorf("invalid input")
@ -357,8 +354,6 @@ func LinkThreads(ctx * GraphContext, thread Thread, child Thread, info ThreadInf
return fmt.Errorf("Will not link %s as a child of itself", thread.ID())
}
err := UpdateStates(ctx, []GraphNode{thread, child}, func(nodes NodeMap) error {
thread_state := thread.State().(ThreadState)
child_state := child.State().(ThreadState)
@ -380,9 +375,6 @@ func LinkThreads(ctx * GraphContext, thread Thread, child Thread, info ThreadInf
}
child_state.SetParent(thread)
return nil
})
if err != nil {
return err
}
@ -701,9 +693,19 @@ func NewSimpleThread(ctx * GraphContext, name string, requirements []Lockable, a
thread_ptr := &thread
err = LinkLockables(ctx, thread_ptr, requirements)
if len(requirements) > 0 {
req_nodes := make([]GraphNode, len(requirements))
for i, req := range(requirements) {
req_nodes[i] = req
}
err = UpdateStates(ctx, req_nodes, func(nodes NodeMap) error {
return LinkLockables(ctx, thread_ptr, requirements, nodes)
})
if err != nil {
return nil, err
}
}
return thread_ptr, nil
}