2023-06-23 20:56:09 -06:00
|
|
|
package graphvent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
2023-06-26 21:20:04 -06:00
|
|
|
"sync"
|
2023-06-23 20:56:09 -06:00
|
|
|
"errors"
|
|
|
|
"encoding/json"
|
|
|
|
)
|
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
type ThreadExt struct {
|
|
|
|
Actions ThreadActions
|
|
|
|
Handlers ThreadHandlers
|
|
|
|
|
|
|
|
SignalChan chan GraphSignal
|
|
|
|
TimeoutChan <-chan time.Time
|
|
|
|
|
|
|
|
ChildWaits sync.WaitGroup
|
|
|
|
|
|
|
|
ActiveLock sync.Mutex
|
|
|
|
Active bool
|
|
|
|
StateName string
|
|
|
|
|
|
|
|
Parent *Node
|
|
|
|
Children map[NodeID]ChildInfo
|
|
|
|
|
|
|
|
ActionQueue []QueuedAction
|
|
|
|
NextAction *QueuedAction
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ext *ThreadExt) Serialize() ([]byte, error) {
|
|
|
|
return nil, fmt.Errorf("NOT_IMPLEMENTED")
|
|
|
|
}
|
|
|
|
|
|
|
|
const ThreadExtType = ExtType("THREAD")
|
|
|
|
func (ext *ThreadExt) Type() ExtType {
|
|
|
|
return ThreadExtType
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ext *ThreadExt) ChildList() []*Node {
|
|
|
|
ret := make([]*Node, len(ext.Children))
|
|
|
|
i := 0
|
|
|
|
for _, info := range(ext.Children) {
|
|
|
|
ret[i] = info.Child
|
|
|
|
i += 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2023-07-22 20:21:17 -06:00
|
|
|
// Assumed that thread is already locked for signal
|
2023-07-25 21:43:15 -06:00
|
|
|
func (ext *ThreadExt) Process(context *StateContext, node *Node, signal GraphSignal) error {
|
|
|
|
context.Graph.Log.Logf("signal", "THREAD_PROCESS: %s", node.ID)
|
2023-07-22 20:21:17 -06:00
|
|
|
|
2023-07-24 17:07:27 -06:00
|
|
|
var err error
|
2023-07-22 20:21:17 -06:00
|
|
|
switch signal.Direction() {
|
|
|
|
case Up:
|
2023-07-25 21:43:15 -06:00
|
|
|
err = UseStates(context, node, NewACLInfo(node, []string{"parent"}), func(context *StateContext) error {
|
|
|
|
if ext.Parent != nil {
|
|
|
|
return Signal(context, ext.Parent, node, signal)
|
2023-07-22 20:21:17 -06:00
|
|
|
} else {
|
2023-07-04 18:45:23 -06:00
|
|
|
return nil
|
2023-07-22 20:21:17 -06:00
|
|
|
}
|
|
|
|
})
|
|
|
|
case Down:
|
2023-07-25 21:43:15 -06:00
|
|
|
err = UseStates(context, node, NewACLInfo(node, []string{"children"}), func(context *StateContext) error {
|
|
|
|
for _, info := range(ext.Children) {
|
|
|
|
err := Signal(context, info.Child, node, signal)
|
2023-07-22 20:21:17 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-06-23 21:21:14 -06:00
|
|
|
}
|
2023-07-04 18:45:23 -06:00
|
|
|
return nil
|
|
|
|
})
|
2023-07-22 20:21:17 -06:00
|
|
|
case Direct:
|
|
|
|
err = nil
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("Invalid signal direction %d", signal.Direction())
|
|
|
|
}
|
2023-07-25 21:43:15 -06:00
|
|
|
ext.SignalChan <- signal
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func UnlinkThreads(context *StateContext, principal *Node, thread *Node, child *Node) error {
|
|
|
|
thread_ext, err := GetExt[*ThreadExt](thread)
|
2023-07-22 20:21:17 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-07-04 18:45:23 -06:00
|
|
|
}
|
2023-07-22 20:21:17 -06:00
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
child_ext, err := GetExt[*ThreadExt](child)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-07-03 16:37:54 -06:00
|
|
|
}
|
2023-07-03 16:03:56 -06:00
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
return UpdateStates(context, principal, ACLMap{
|
|
|
|
thread.ID: ACLInfo{thread, []string{"children"}},
|
|
|
|
child.ID: ACLInfo{child, []string{"parent"}},
|
|
|
|
}, func(context *StateContext) error {
|
|
|
|
_, is_child := thread_ext.Children[child.ID]
|
|
|
|
if is_child == false {
|
|
|
|
return fmt.Errorf("UNLINK_THREADS_ERR: %s is not a child of %s", child.ID, thread.ID)
|
|
|
|
}
|
2023-07-03 16:37:54 -06:00
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
delete(thread_ext.Children, child.ID)
|
|
|
|
child_ext.Parent = nil
|
|
|
|
return nil
|
|
|
|
})
|
2023-07-03 16:03:56 -06:00
|
|
|
}
|
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
func checkIfChild(context *StateContext, id NodeID, cur *ThreadExt) (bool, error) {
|
|
|
|
for _, info := range(cur.Children) {
|
|
|
|
child := info.Child
|
|
|
|
if child.ID == id {
|
|
|
|
return true, nil
|
2023-06-23 20:56:09 -06:00
|
|
|
}
|
2023-07-25 21:43:15 -06:00
|
|
|
|
|
|
|
child_ext, err := GetExt[*ThreadExt](child)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var is_child bool
|
|
|
|
err = UpdateStates(context, child, NewACLInfo(child, []string{"children"}), func(context *StateContext) error {
|
|
|
|
is_child, err = checkIfChild(context, id, child_ext)
|
|
|
|
return err
|
2023-06-23 20:56:09 -06:00
|
|
|
})
|
2023-07-25 21:43:15 -06:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2023-06-23 20:56:09 -06:00
|
|
|
if is_child {
|
2023-07-25 21:43:15 -06:00
|
|
|
return true, nil
|
2023-06-23 20:56:09 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
return false, nil
|
2023-06-23 20:56:09 -06:00
|
|
|
}
|
|
|
|
|
2023-07-23 17:57:47 -06:00
|
|
|
// Links child to parent with info as the associated info
|
|
|
|
// Continues the write context with princ, getting children for thread and parent for child
|
2023-07-25 21:43:15 -06:00
|
|
|
func LinkThreads(context *StateContext, principal *Node, thread *Node, info ChildInfo) error {
|
|
|
|
if context == nil || principal == nil || thread == nil || info.Child == nil {
|
2023-06-23 20:56:09 -06:00
|
|
|
return fmt.Errorf("invalid input")
|
|
|
|
}
|
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
child := info.Child
|
|
|
|
if thread.ID == child.ID {
|
|
|
|
return fmt.Errorf("Will not link %s as a child of itself", thread.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
thread_ext, err := GetExt[*ThreadExt](thread)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
child_ext, err := GetExt[*ThreadExt](thread)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-06-23 20:56:09 -06:00
|
|
|
}
|
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
return UpdateStates(context, principal, ACLMap{
|
|
|
|
child.ID: ACLInfo{Node: child, Resources: []string{"parent"}},
|
|
|
|
thread.ID: ACLInfo{Node: thread, Resources: []string{"children"}},
|
2023-07-23 17:57:47 -06:00
|
|
|
}, func(context *StateContext) error {
|
2023-07-25 21:43:15 -06:00
|
|
|
if child_ext.Parent != nil {
|
|
|
|
return fmt.Errorf("EVENT_LINK_ERR: %s already has a parent, cannot link as child", child.ID)
|
2023-07-22 20:21:17 -06:00
|
|
|
}
|
2023-06-23 20:56:09 -06:00
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
is_child, err := checkIfChild(context, thread.ID, child_ext)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if is_child == true {
|
|
|
|
return fmt.Errorf("EVENT_LINK_ERR: %s is a child of %s so cannot add as parent", thread.ID, child.ID)
|
2023-07-22 20:21:17 -06:00
|
|
|
}
|
2023-06-23 20:56:09 -06:00
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
is_child, err = checkIfChild(context, child.ID, thread_ext)
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
} else if is_child == true {
|
|
|
|
return fmt.Errorf("EVENT_LINK_ERR: %s is already a parent of %s so will not add again", thread.ID, child.ID)
|
2023-07-22 20:21:17 -06:00
|
|
|
}
|
2023-06-23 20:56:09 -06:00
|
|
|
|
2023-07-24 16:04:56 -06:00
|
|
|
// TODO check for info types
|
2023-06-23 20:56:09 -06:00
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
thread_ext.Children[child.ID] = info
|
|
|
|
child_ext.Parent = thread
|
2023-06-23 20:56:09 -06:00
|
|
|
|
2023-07-22 20:21:17 -06:00
|
|
|
return nil
|
|
|
|
})
|
2023-06-23 20:56:09 -06:00
|
|
|
}
|
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
type ThreadAction func(*Context, *Node, *ThreadExt)(string, error)
|
2023-07-09 15:59:41 -06:00
|
|
|
type ThreadActions map[string]ThreadAction
|
2023-07-25 21:43:15 -06:00
|
|
|
type ThreadHandler func(*Context, *Node, *ThreadExt, GraphSignal)(string, error)
|
2023-07-09 15:59:41 -06:00
|
|
|
type ThreadHandlers map[string]ThreadHandler
|
|
|
|
|
2023-07-24 16:04:56 -06:00
|
|
|
type InfoType string
|
|
|
|
func (t InfoType) String() string {
|
|
|
|
return string(t)
|
2023-07-12 22:31:13 -06:00
|
|
|
}
|
2023-07-11 16:54:09 -06:00
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
type ThreadInfo interface {
|
|
|
|
Serializable[InfoType]
|
|
|
|
}
|
|
|
|
|
2023-07-11 16:54:09 -06:00
|
|
|
// Data required by a parent thread to restore it's children
|
|
|
|
type ParentThreadInfo struct {
|
|
|
|
Start bool `json:"start"`
|
|
|
|
StartAction string `json:"start_action"`
|
|
|
|
RestoreAction string `json:"restore_action"`
|
|
|
|
}
|
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
const ParentThreadInfoType = InfoType("PARENT")
|
|
|
|
func (info *ParentThreadInfo) Type() InfoType {
|
|
|
|
return ParentThreadInfoType
|
|
|
|
}
|
|
|
|
|
|
|
|
func (info *ParentThreadInfo) Serialize() ([]byte, error) {
|
|
|
|
return json.MarshalIndent(info, "", " ")
|
2023-07-11 16:54:09 -06:00
|
|
|
}
|
|
|
|
|
2023-07-24 16:04:56 -06:00
|
|
|
type ChildInfo struct {
|
2023-07-25 21:43:15 -06:00
|
|
|
Child *Node
|
|
|
|
Infos map[InfoType]ThreadInfo
|
2023-07-24 16:04:56 -06:00
|
|
|
}
|
2023-07-09 15:59:41 -06:00
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
func NewChildInfo(child *Node, infos map[InfoType]ThreadInfo) ChildInfo {
|
2023-07-24 16:04:56 -06:00
|
|
|
if infos == nil {
|
2023-07-25 21:43:15 -06:00
|
|
|
infos = map[InfoType]ThreadInfo{}
|
2023-07-24 16:04:56 -06:00
|
|
|
}
|
2023-07-09 15:59:41 -06:00
|
|
|
|
2023-07-24 16:04:56 -06:00
|
|
|
return ChildInfo{
|
|
|
|
Child: child,
|
|
|
|
Infos: infos,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type QueuedAction struct {
|
2023-07-24 17:49:26 -06:00
|
|
|
Timeout time.Time `json:"time"`
|
|
|
|
Action string `json:"action"`
|
2023-07-24 16:04:56 -06:00
|
|
|
}
|
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
func (ext *ThreadExt) QueueAction(end time.Time, action string) {
|
|
|
|
ext.ActionQueue = append(ext.ActionQueue, QueuedAction{end, action})
|
|
|
|
ext.NextAction, ext.TimeoutChan = ext.SoonestAction()
|
2023-07-09 15:59:41 -06:00
|
|
|
}
|
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
func (ext *ThreadExt) ClearActionQueue() {
|
|
|
|
ext.ActionQueue = []QueuedAction{}
|
|
|
|
ext.NextAction = nil
|
|
|
|
ext.TimeoutChan = nil
|
2023-07-09 15:59:41 -06:00
|
|
|
}
|
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
func (ext *ThreadExt) SoonestAction() (*QueuedAction, <-chan time.Time) {
|
2023-07-24 17:49:26 -06:00
|
|
|
var soonest_action *QueuedAction
|
|
|
|
var soonest_time time.Time
|
2023-07-25 21:43:15 -06:00
|
|
|
for _, action := range(ext.ActionQueue) {
|
2023-07-24 18:42:51 -06:00
|
|
|
if action.Timeout.Compare(soonest_time) == -1 || soonest_action == nil {
|
2023-07-24 17:49:26 -06:00
|
|
|
soonest_action = &action
|
2023-07-24 18:42:51 -06:00
|
|
|
soonest_time = action.Timeout
|
2023-07-24 17:49:26 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if soonest_action != nil {
|
|
|
|
return soonest_action, time.After(time.Until(soonest_action.Timeout))
|
|
|
|
} else {
|
|
|
|
return nil, nil
|
2023-07-10 01:07:56 -06:00
|
|
|
}
|
2023-07-24 17:49:26 -06:00
|
|
|
}
|
|
|
|
|
2023-07-24 16:04:56 -06:00
|
|
|
var deserializers = map[InfoType]func(interface{})(interface{}, error) {
|
2023-07-24 17:07:27 -06:00
|
|
|
"parent": func(raw interface{})(interface{}, error) {
|
|
|
|
m, ok := raw.(map[string]interface{})
|
|
|
|
if ok == false {
|
|
|
|
return nil, fmt.Errorf("Failed to cast parent info to map")
|
|
|
|
}
|
|
|
|
start, ok := m["start"].(bool)
|
|
|
|
if ok == false {
|
|
|
|
return nil, fmt.Errorf("Failed to get start from parent info")
|
|
|
|
}
|
|
|
|
start_action, ok := m["start_action"].(string)
|
|
|
|
if ok == false {
|
|
|
|
return nil, fmt.Errorf("Failed to get start_action from parent info")
|
|
|
|
}
|
|
|
|
restore_action, ok := m["restore_action"].(string)
|
|
|
|
if ok == false {
|
|
|
|
return nil, fmt.Errorf("Failed to get restore_action from parent info")
|
|
|
|
}
|
2023-07-24 16:04:56 -06:00
|
|
|
|
2023-07-24 17:07:27 -06:00
|
|
|
return &ParentThreadInfo{
|
|
|
|
Start: start,
|
|
|
|
StartAction: start_action,
|
|
|
|
RestoreAction: restore_action,
|
|
|
|
}, nil
|
|
|
|
},
|
2023-07-24 16:04:56 -06:00
|
|
|
}
|
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
func NewThreadExt(buffer int, name string, state_name string, actions ThreadActions, handlers ThreadHandlers) ThreadExt {
|
|
|
|
return ThreadExt{
|
2023-07-24 16:04:56 -06:00
|
|
|
Actions: actions,
|
|
|
|
Handlers: handlers,
|
2023-07-25 21:43:15 -06:00
|
|
|
SignalChan: make(chan GraphSignal, buffer),
|
|
|
|
TimeoutChan: nil,
|
|
|
|
Active: false,
|
|
|
|
StateName: state_name,
|
|
|
|
Parent: nil,
|
|
|
|
Children: map[NodeID]ChildInfo{},
|
|
|
|
ActionQueue: []QueuedAction{},
|
|
|
|
NextAction: nil,
|
2023-07-24 16:04:56 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
func (ext *ThreadExt) SetActive(active bool) error {
|
|
|
|
ext.ActiveLock.Lock()
|
|
|
|
defer ext.ActiveLock.Unlock()
|
|
|
|
if ext.Active == true && active == true {
|
|
|
|
return fmt.Errorf("alreday active, cannot set active")
|
|
|
|
} else if ext.Active == false && active == false {
|
|
|
|
return fmt.Errorf("already inactive, canot set inactive")
|
2023-07-09 15:59:41 -06:00
|
|
|
}
|
2023-07-25 21:43:15 -06:00
|
|
|
ext.Active = active
|
2023-07-24 16:04:56 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
func (ext *ThreadExt) SetState(state string) error {
|
|
|
|
ext.StateName = state
|
2023-07-24 16:04:56 -06:00
|
|
|
return nil
|
2023-06-23 20:56:09 -06:00
|
|
|
}
|
|
|
|
|
2023-07-22 20:21:17 -06:00
|
|
|
// Requires the read permission of threads children
|
2023-07-25 21:43:15 -06:00
|
|
|
func FindChild(context *StateContext, principal *Node, thread *Node, id NodeID) (*Node, error) {
|
|
|
|
if thread == nil {
|
2023-06-23 20:56:09 -06:00
|
|
|
panic("cannot recurse through nil")
|
|
|
|
}
|
2023-07-25 21:43:15 -06:00
|
|
|
|
|
|
|
if id == thread.ID {
|
|
|
|
return thread, nil
|
2023-06-23 20:56:09 -06:00
|
|
|
}
|
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
thread_ext, err := GetExt[*ThreadExt](thread)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2023-06-23 20:56:09 -06:00
|
|
|
}
|
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
var found *Node = nil
|
|
|
|
err = UseStates(context, principal, NewACLInfo(thread, []string{"children"}), func(context *StateContext) error {
|
|
|
|
for _, info := range(thread_ext.Children) {
|
|
|
|
found, err = FindChild(context, principal, info.Child, id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if found != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return found, err
|
2023-06-23 20:56:09 -06:00
|
|
|
}
|
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
func ChildGo(ctx * Context, thread_ext *ThreadExt, child *Node, first_action string) {
|
|
|
|
thread_ext.ChildWaits.Add(1)
|
|
|
|
go func(child *Node) {
|
|
|
|
defer thread_ext.ChildWaits.Done()
|
2023-07-06 16:57:51 -06:00
|
|
|
err := ThreadLoop(ctx, child, first_action)
|
2023-06-26 21:20:04 -06:00
|
|
|
if err != nil {
|
2023-07-25 21:43:15 -06:00
|
|
|
ctx.Log.Logf("thread", "THREAD_CHILD_RUN_ERR: %s %s", child.ID, err)
|
2023-06-26 21:20:04 -06:00
|
|
|
} else {
|
2023-07-25 21:43:15 -06:00
|
|
|
ctx.Log.Logf("thread", "THREAD_CHILD_RUN_DONE: %s", child.ID)
|
2023-06-26 21:20:04 -06:00
|
|
|
}
|
|
|
|
}(child)
|
|
|
|
}
|
|
|
|
|
2023-07-23 17:57:47 -06:00
|
|
|
// Main Loop for Threads, starts a write context, so cannot be called from a write or read context
|
2023-07-25 21:43:15 -06:00
|
|
|
func ThreadLoop(ctx * Context, thread *Node, first_action string) error {
|
|
|
|
thread_ext, err := GetExt[*ThreadExt](thread)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Log.Logf("thread", "THREAD_LOOP_START: %s - %s", thread.ID, first_action)
|
|
|
|
|
|
|
|
err = thread_ext.SetActive(true)
|
2023-06-28 00:48:49 -06:00
|
|
|
if err != nil {
|
2023-07-06 16:57:51 -06:00
|
|
|
ctx.Log.Logf("thread", "THREAD_LOOP_START_ERR: %e", err)
|
2023-06-28 00:48:49 -06:00
|
|
|
return err
|
|
|
|
}
|
2023-07-02 12:47:45 -06:00
|
|
|
next_action := first_action
|
2023-06-23 20:56:09 -06:00
|
|
|
for next_action != "" {
|
2023-07-25 21:43:15 -06:00
|
|
|
action, exists := thread_ext.Actions[next_action]
|
2023-06-23 20:56:09 -06:00
|
|
|
if exists == false {
|
|
|
|
error_str := fmt.Sprintf("%s is not a valid action", next_action)
|
|
|
|
return errors.New(error_str)
|
|
|
|
}
|
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
ctx.Log.Logf("thread", "THREAD_ACTION: %s - %s", thread.ID, next_action)
|
|
|
|
next_action, err = action(ctx, thread, thread_ext)
|
2023-06-23 20:56:09 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
err = thread_ext.SetActive(false)
|
2023-06-26 21:20:04 -06:00
|
|
|
if err != nil {
|
2023-07-06 16:57:51 -06:00
|
|
|
ctx.Log.Logf("thread", "THREAD_LOOP_STOP_ERR: %e", err)
|
2023-06-26 21:20:04 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
ctx.Log.Logf("thread", "THREAD_LOOP_DONE: %s", thread.ID)
|
2023-06-23 20:56:09 -06:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
func ThreadChildLinked(ctx *Context, thread *Node, thread_ext *ThreadExt, signal GraphSignal) (string, error) {
|
|
|
|
ctx.Log.Logf("thread", "THREAD_CHILD_LINKED: %s - %+v", thread.ID, signal)
|
2023-07-23 23:01:45 -06:00
|
|
|
context := NewWriteContext(ctx)
|
2023-07-25 21:43:15 -06:00
|
|
|
err := UpdateStates(context, thread, NewACLInfo(thread, []string{"children"}), func(context *StateContext) error {
|
2023-07-23 23:01:45 -06:00
|
|
|
sig, ok := signal.(IDSignal)
|
|
|
|
if ok == false {
|
|
|
|
ctx.Log.Logf("thread", "THREAD_NODE_LINKED_BAD_CAST")
|
|
|
|
return nil
|
|
|
|
}
|
2023-07-25 21:43:15 -06:00
|
|
|
info, exists := thread_ext.Children[sig.ID]
|
2023-07-24 16:04:56 -06:00
|
|
|
if exists == false {
|
2023-07-23 23:01:45 -06:00
|
|
|
ctx.Log.Logf("thread", "THREAD_NODE_LINKED: %s is not a child of %s", sig.ID)
|
|
|
|
return nil
|
|
|
|
}
|
2023-07-24 16:04:56 -06:00
|
|
|
parent_info, exists := info.Infos["parent"].(*ParentThreadInfo)
|
|
|
|
if exists == false {
|
|
|
|
panic("ran ThreadChildLinked from a thread that doesn't require 'parent' child info. library party foul")
|
2023-07-23 23:01:45 -06:00
|
|
|
}
|
2023-07-24 16:04:56 -06:00
|
|
|
|
|
|
|
if parent_info.Start == true {
|
2023-07-25 21:43:15 -06:00
|
|
|
ChildGo(ctx, thread_ext, info.Child, parent_info.StartAction)
|
2023-07-23 23:01:45 -06:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
}
|
|
|
|
return "wait", nil
|
|
|
|
}
|
|
|
|
|
2023-07-24 16:04:56 -06:00
|
|
|
// Helper function to start a child from a thread during a signal handler
|
|
|
|
// Starts a write context, so cannot be called from either a write or read context
|
2023-07-25 21:43:15 -06:00
|
|
|
func ThreadStartChild(ctx *Context, thread *Node, thread_ext *ThreadExt, signal GraphSignal) (string, error) {
|
2023-07-23 23:01:45 -06:00
|
|
|
sig, ok := signal.(StartChildSignal)
|
|
|
|
if ok == false {
|
|
|
|
return "wait", nil
|
|
|
|
}
|
|
|
|
|
2023-07-23 17:57:47 -06:00
|
|
|
context := NewWriteContext(ctx)
|
2023-07-25 21:43:15 -06:00
|
|
|
return "wait", UpdateStates(context, thread, NewACLInfo(thread, []string{"children"}), func(context *StateContext) error {
|
|
|
|
info, exists:= thread_ext.Children[sig.ID]
|
2023-07-24 16:04:56 -06:00
|
|
|
if exists == false {
|
2023-07-25 21:43:15 -06:00
|
|
|
return fmt.Errorf("%s is not a child of %s", sig.ID, thread.ID)
|
2023-07-21 14:05:39 -06:00
|
|
|
}
|
2023-07-25 21:43:15 -06:00
|
|
|
return UpdateStates(context, thread, NewACLInfo(info.Child, []string{"start"}), func(context *StateContext) error {
|
2023-07-21 14:05:39 -06:00
|
|
|
|
2023-07-24 16:04:56 -06:00
|
|
|
parent_info, exists := info.Infos["parent"].(*ParentThreadInfo)
|
|
|
|
if exists == false {
|
|
|
|
return fmt.Errorf("Called ThreadStartChild from a thread that doesn't require parent child info")
|
|
|
|
}
|
|
|
|
parent_info.Start = true
|
2023-07-25 21:43:15 -06:00
|
|
|
ChildGo(ctx, thread_ext, info.Child, sig.Action)
|
2023-07-21 14:05:39 -06:00
|
|
|
|
2023-07-22 20:21:17 -06:00
|
|
|
return nil
|
|
|
|
})
|
2023-07-21 14:05:39 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-07-23 17:57:47 -06:00
|
|
|
// Helper function to restore threads that should be running from a parents restore action
|
|
|
|
// Starts a write context, so cannot be called from either a write or read context
|
2023-07-25 21:43:15 -06:00
|
|
|
func ThreadRestore(ctx * Context, thread *Node, thread_ext *ThreadExt, start bool) error {
|
2023-07-23 17:57:47 -06:00
|
|
|
context := NewWriteContext(ctx)
|
2023-07-25 21:43:15 -06:00
|
|
|
return UpdateStates(context, thread, NewACLInfo(thread, []string{"children"}), func(context *StateContext) error {
|
|
|
|
return UpdateStates(context, thread, ACLList(thread_ext.ChildList(), []string{"start", "state"}), func(context *StateContext) error {
|
|
|
|
for _, info := range(thread_ext.Children) {
|
|
|
|
child_ext, err := GetExt[*ThreadExt](info.Child)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-24 16:04:56 -06:00
|
|
|
parent_info := info.Infos["parent"].(*ParentThreadInfo)
|
2023-07-25 21:43:15 -06:00
|
|
|
if parent_info.Start == true && child_ext.StateName != "finished" {
|
|
|
|
ctx.Log.Logf("thread", "THREAD_RESTORED: %s -> %s", thread.ID, info.Child.ID)
|
2023-07-23 23:01:45 -06:00
|
|
|
if start == true {
|
2023-07-25 21:43:15 -06:00
|
|
|
ChildGo(ctx, thread_ext, info.Child, parent_info.StartAction)
|
2023-07-23 23:01:45 -06:00
|
|
|
} else {
|
2023-07-25 21:43:15 -06:00
|
|
|
ChildGo(ctx, thread_ext, info.Child, parent_info.RestoreAction)
|
2023-07-23 23:01:45 -06:00
|
|
|
}
|
2023-07-23 19:04:04 -06:00
|
|
|
}
|
2023-07-11 16:54:09 -06:00
|
|
|
}
|
2023-07-23 19:04:04 -06:00
|
|
|
return nil
|
|
|
|
})
|
2023-07-11 16:54:09 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-07-23 17:57:47 -06:00
|
|
|
// Helper function to be called during a threads start action, sets the thread state to started
|
|
|
|
// Starts a write context, so cannot be called from either a write or read context
|
2023-07-24 16:04:56 -06:00
|
|
|
// Returns "wait", nil on success, so the first return value can be ignored safely
|
2023-07-25 21:43:15 -06:00
|
|
|
func ThreadStart(ctx * Context, thread *Node, thread_ext *ThreadExt) (string, error) {
|
2023-07-23 17:57:47 -06:00
|
|
|
context := NewWriteContext(ctx)
|
2023-07-25 21:43:15 -06:00
|
|
|
err := UpdateStates(context, thread, NewACLInfo(thread, []string{"state"}), func(context *StateContext) error {
|
|
|
|
err := LockLockables(context, map[NodeID]*Node{thread.ID: thread}, thread)
|
2023-07-23 17:57:47 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-07-06 16:57:51 -06:00
|
|
|
}
|
2023-07-25 21:43:15 -06:00
|
|
|
return thread_ext.SetState("started")
|
2023-07-06 16:57:51 -06:00
|
|
|
})
|
2023-07-24 18:39:33 -06:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
context = NewReadContext(ctx)
|
2023-07-25 21:43:15 -06:00
|
|
|
return "wait", Signal(context, thread, thread, NewStatusSignal("started", thread.ID))
|
2023-07-06 16:57:51 -06:00
|
|
|
}
|
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
func ThreadWait(ctx * Context, thread *Node, thread_ext *ThreadExt) (string, error) {
|
|
|
|
ctx.Log.Logf("thread", "THREAD_WAIT: %s - %+v", thread.ID, thread_ext.ActionQueue)
|
2023-06-24 19:48:59 -06:00
|
|
|
for {
|
|
|
|
select {
|
2023-07-25 21:43:15 -06:00
|
|
|
case signal := <- thread_ext.SignalChan:
|
|
|
|
ctx.Log.Logf("thread", "THREAD_SIGNAL: %s %+v", thread.ID, signal)
|
|
|
|
signal_fn, exists := thread_ext.Handlers[signal.Type()]
|
2023-06-24 19:48:59 -06:00
|
|
|
if exists == true {
|
2023-07-25 21:43:15 -06:00
|
|
|
ctx.Log.Logf("thread", "THREAD_HANDLER: %s - %s", thread.ID, signal.Type())
|
|
|
|
return signal_fn(ctx, thread, thread_ext, signal)
|
2023-06-25 21:00:00 -06:00
|
|
|
} else {
|
2023-07-25 21:43:15 -06:00
|
|
|
ctx.Log.Logf("thread", "THREAD_NOHANDLER: %s - %s", thread.ID, signal.Type())
|
2023-06-24 19:48:59 -06:00
|
|
|
}
|
2023-07-25 21:43:15 -06:00
|
|
|
case <- thread_ext.TimeoutChan:
|
2023-07-02 12:14:04 -06:00
|
|
|
timeout_action := ""
|
2023-07-23 17:57:47 -06:00
|
|
|
context := NewWriteContext(ctx)
|
2023-07-25 21:43:15 -06:00
|
|
|
err := UpdateStates(context, thread, NewACLMap(NewACLInfo(thread, []string{"timeout"})), func(context *StateContext) error {
|
|
|
|
timeout_action = thread_ext.NextAction.Action
|
|
|
|
thread_ext.NextAction, thread_ext.TimeoutChan = thread_ext.SoonestAction()
|
2023-07-02 12:14:04 -06:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-07-25 21:43:15 -06:00
|
|
|
ctx.Log.Logf("thread", "THREAD_TIMEOUT_ERR: %s - %e", thread.ID, err)
|
2023-07-02 12:14:04 -06:00
|
|
|
}
|
2023-07-25 21:43:15 -06:00
|
|
|
ctx.Log.Logf("thread", "THREAD_TIMEOUT %s - NEXT_STATE: %s", thread.ID, timeout_action)
|
2023-07-02 12:14:04 -06:00
|
|
|
return timeout_action, nil
|
2023-06-24 19:48:59 -06:00
|
|
|
}
|
2023-06-23 20:56:09 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
func ThreadFinish(ctx *Context, thread *Node, thread_ext *ThreadExt) (string, error) {
|
2023-07-23 17:57:47 -06:00
|
|
|
context := NewWriteContext(ctx)
|
2023-07-25 21:43:15 -06:00
|
|
|
return "", UpdateStates(context, thread, NewACLInfo(thread, []string{"state"}), func(context *StateContext) error {
|
|
|
|
err := thread_ext.SetState("finished")
|
2023-07-23 17:57:47 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-25 21:43:15 -06:00
|
|
|
return UnlockLockables(context, map[NodeID]*Node{thread.ID: thread}, thread)
|
2023-07-23 17:57:47 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-07-22 20:21:17 -06:00
|
|
|
var ThreadAbortedError = errors.New("Thread aborted by signal")
|
2023-07-06 16:57:51 -06:00
|
|
|
|
2023-07-23 17:57:47 -06:00
|
|
|
// Default thread action function for "abort", sends a signal and returns a ThreadAbortedError
|
2023-07-25 21:43:15 -06:00
|
|
|
func ThreadAbort(ctx * Context, thread *Node, thread_ext *ThreadExt, signal GraphSignal) (string, error) {
|
2023-07-23 17:57:47 -06:00
|
|
|
context := NewReadContext(ctx)
|
2023-07-25 21:43:15 -06:00
|
|
|
err := Signal(context, thread, thread, NewStatusSignal("aborted", thread.ID))
|
2023-07-22 20:21:17 -06:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2023-07-23 19:04:04 -06:00
|
|
|
return "", ThreadAbortedError
|
2023-06-23 21:21:14 -06:00
|
|
|
}
|
|
|
|
|
2023-07-23 19:04:04 -06:00
|
|
|
// Default thread action for "stop", sends a signal and returns no error
|
2023-07-25 21:43:15 -06:00
|
|
|
func ThreadStop(ctx * Context, thread *Node, thread_ext *ThreadExt, signal GraphSignal) (string, error) {
|
2023-07-23 17:57:47 -06:00
|
|
|
context := NewReadContext(ctx)
|
2023-07-25 21:43:15 -06:00
|
|
|
err := Signal(context, thread, thread, NewStatusSignal("stopped", thread.ID))
|
2023-07-23 17:57:47 -06:00
|
|
|
return "finish", err
|
2023-06-23 20:56:09 -06:00
|
|
|
}
|
|
|
|
|
2023-07-23 17:57:47 -06:00
|
|
|
// Default thread actions
|
2023-07-02 12:14:04 -06:00
|
|
|
var BaseThreadActions = ThreadActions{
|
|
|
|
"wait": ThreadWait,
|
2023-07-24 16:04:56 -06:00
|
|
|
"start": ThreadStart,
|
|
|
|
"finish": ThreadFinish,
|
2023-07-02 12:14:04 -06:00
|
|
|
}
|
|
|
|
|
2023-07-23 17:57:47 -06:00
|
|
|
// Default thread signal handlers
|
2023-07-02 12:14:04 -06:00
|
|
|
var BaseThreadHandlers = ThreadHandlers{
|
|
|
|
"abort": ThreadAbort,
|
2023-07-23 19:04:04 -06:00
|
|
|
"stop": ThreadStop,
|
2023-07-02 12:14:04 -06:00
|
|
|
}
|