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-22 20:21:17 -06:00
|
|
|
// Assumed that thread is already locked for signal
|
2023-07-24 16:04:56 -06:00
|
|
|
func (thread *Thread) Process(context *StateContext, signal GraphSignal) error {
|
2023-07-24 17:07:27 -06:00
|
|
|
context.Graph.Log.Logf("signal", "THREAD_PROCESS: %s", thread.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-23 17:57:47 -06:00
|
|
|
err = UseStates(context, thread, NewLockInfo(thread, []string{"parent"}), func(context *StateContext) error {
|
2023-07-24 16:04:56 -06:00
|
|
|
if thread.Parent != nil {
|
|
|
|
return Signal(context, thread.Parent, thread, 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-23 21:14:15 -06:00
|
|
|
err = UseStates(context, thread, NewLockInfo(thread, []string{"children"}), func(context *StateContext) error {
|
2023-07-24 16:04:56 -06:00
|
|
|
for _, info := range(thread.Children) {
|
|
|
|
err := Signal(context, info.Child, thread, 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())
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-07-04 18:45:23 -06:00
|
|
|
}
|
2023-07-22 20:21:17 -06:00
|
|
|
|
2023-07-24 16:04:56 -06:00
|
|
|
thread.Chan <- signal
|
2023-07-24 17:07:27 -06:00
|
|
|
return thread.Lockable.Process(context, signal)
|
2023-06-23 20:56:09 -06:00
|
|
|
}
|
|
|
|
|
2023-07-09 15:59:41 -06:00
|
|
|
// Requires thread and childs thread to be locked for write
|
2023-07-24 16:04:56 -06:00
|
|
|
func UnlinkThreads(ctx * Context, node ThreadNode, child_node ThreadNode) error {
|
|
|
|
thread := node.ThreadHandle()
|
|
|
|
child := child_node.ThreadHandle()
|
|
|
|
_, is_child := thread.Children[child_node.ID()]
|
|
|
|
if is_child == false {
|
2023-07-03 16:37:54 -06:00
|
|
|
return fmt.Errorf("UNLINK_THREADS_ERR: %s is not a child of %s", child.ID(), thread.ID())
|
|
|
|
}
|
2023-07-03 16:03:56 -06:00
|
|
|
|
2023-07-24 16:04:56 -06:00
|
|
|
child.Parent = nil
|
|
|
|
delete(thread.Children, child.ID())
|
2023-07-03 16:37:54 -06:00
|
|
|
|
|
|
|
return nil
|
2023-07-03 16:03:56 -06:00
|
|
|
}
|
|
|
|
|
2023-07-24 16:04:56 -06:00
|
|
|
func checkIfChild(context *StateContext, target ThreadNode, cur ThreadNode) bool {
|
|
|
|
for _, info := range(cur.ThreadHandle().Children) {
|
|
|
|
if info.Child.ID() == target.ID() {
|
2023-06-23 20:56:09 -06:00
|
|
|
return true
|
|
|
|
}
|
2023-06-28 00:48:49 -06:00
|
|
|
is_child := false
|
2023-07-23 17:57:47 -06:00
|
|
|
UpdateStates(context, cur, NewLockMap(
|
2023-07-24 16:04:56 -06:00
|
|
|
NewLockInfo(info.Child, []string{"children"}),
|
2023-07-23 17:57:47 -06:00
|
|
|
), func(context *StateContext) error {
|
2023-07-24 16:04:56 -06:00
|
|
|
is_child = checkIfChild(context, target, info.Child)
|
2023-06-28 00:48:49 -06:00
|
|
|
return nil
|
2023-06-23 20:56:09 -06:00
|
|
|
})
|
|
|
|
if is_child {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
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-24 16:04:56 -06:00
|
|
|
func LinkThreads(context *StateContext, princ Node, thread_node ThreadNode, info ChildInfo) error {
|
|
|
|
if context == nil || thread_node == nil || info.Child == nil {
|
2023-06-23 20:56:09 -06:00
|
|
|
return fmt.Errorf("invalid input")
|
|
|
|
}
|
2023-07-24 16:04:56 -06:00
|
|
|
thread := thread_node.ThreadHandle()
|
|
|
|
child := info.Child.ThreadHandle()
|
2023-07-24 20:27:02 -06:00
|
|
|
child_node := info.Child
|
2023-06-23 20:56:09 -06:00
|
|
|
|
|
|
|
if thread.ID() == child.ID() {
|
|
|
|
return fmt.Errorf("Will not link %s as a child of itself", thread.ID())
|
|
|
|
}
|
|
|
|
|
2023-07-23 17:57:47 -06:00
|
|
|
return UpdateStates(context, princ, LockMap{
|
2023-07-24 20:27:02 -06:00
|
|
|
child.ID(): LockInfo{Node: child_node, Resources: []string{"parent"}},
|
|
|
|
thread.ID(): LockInfo{Node: thread_node, Resources: []string{"children"}},
|
2023-07-23 17:57:47 -06:00
|
|
|
}, func(context *StateContext) error {
|
2023-07-24 16:04:56 -06:00
|
|
|
if child.Parent != nil {
|
2023-07-22 20:21:17 -06:00
|
|
|
return fmt.Errorf("EVENT_LINK_ERR: %s already has a parent, cannot link as child", child.ID())
|
|
|
|
}
|
2023-06-23 20:56:09 -06:00
|
|
|
|
2023-07-22 20:21:17 -06:00
|
|
|
if checkIfChild(context, thread, child) == true {
|
|
|
|
return fmt.Errorf("EVENT_LINK_ERR: %s is a child of %s so cannot add as parent", thread.ID(), child.ID())
|
|
|
|
}
|
2023-06-23 20:56:09 -06:00
|
|
|
|
2023-07-22 20:21:17 -06:00
|
|
|
if checkIfChild(context, child, thread) == true {
|
|
|
|
return fmt.Errorf("EVENT_LINK_ERR: %s is already a parent of %s so will not add again", thread.ID(), child.ID())
|
|
|
|
}
|
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-24 16:04:56 -06:00
|
|
|
thread.Children[child.ID()] = info
|
|
|
|
child.Parent = thread_node
|
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-24 16:04:56 -06:00
|
|
|
type ThreadAction func(*Context, ThreadNode)(string, error)
|
2023-07-09 15:59:41 -06:00
|
|
|
type ThreadActions map[string]ThreadAction
|
2023-07-24 16:04:56 -06:00
|
|
|
type ThreadHandler func(*Context, ThreadNode, 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
|
|
|
|
|
|
|
// 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"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewParentThreadInfo(start bool, start_action string, restore_action string) ParentThreadInfo {
|
|
|
|
return ParentThreadInfo{
|
|
|
|
Start: start,
|
|
|
|
StartAction: start_action,
|
|
|
|
RestoreAction: restore_action,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-24 16:04:56 -06:00
|
|
|
type ChildInfo struct {
|
|
|
|
Child ThreadNode
|
|
|
|
Infos map[InfoType]interface{}
|
|
|
|
}
|
2023-07-09 15:59:41 -06:00
|
|
|
|
2023-07-24 16:04:56 -06:00
|
|
|
func NewChildInfo(child ThreadNode, infos map[InfoType]interface{}) ChildInfo {
|
|
|
|
if infos == nil {
|
|
|
|
infos = map[InfoType]interface{}{}
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
type ThreadNode interface {
|
|
|
|
LockableNode
|
|
|
|
ThreadHandle() *Thread
|
|
|
|
}
|
|
|
|
|
|
|
|
type Thread struct {
|
|
|
|
Lockable
|
|
|
|
|
|
|
|
Actions ThreadActions
|
|
|
|
Handlers ThreadHandlers
|
|
|
|
|
|
|
|
TimeoutChan <-chan time.Time
|
|
|
|
Chan chan GraphSignal
|
|
|
|
ChildWaits sync.WaitGroup
|
|
|
|
Active bool
|
|
|
|
ActiveLock sync.Mutex
|
|
|
|
|
|
|
|
StateName string
|
|
|
|
Parent ThreadNode
|
|
|
|
Children map[NodeID]ChildInfo
|
|
|
|
InfoTypes []InfoType
|
2023-07-24 17:49:26 -06:00
|
|
|
ActionQueue []QueuedAction
|
|
|
|
NextAction *QueuedAction
|
|
|
|
}
|
2023-07-09 15:59:41 -06:00
|
|
|
|
2023-07-24 17:49:26 -06:00
|
|
|
func (thread *Thread) QueueAction(end time.Time, action string) {
|
|
|
|
thread.ActionQueue = append(thread.ActionQueue, QueuedAction{end, action})
|
|
|
|
thread.NextAction, thread.TimeoutChan = thread.SoonestAction()
|
2023-07-09 15:59:41 -06:00
|
|
|
}
|
|
|
|
|
2023-07-24 19:00:44 -06:00
|
|
|
func (thread *Thread) ClearActionQueue() {
|
|
|
|
thread.ActionQueue = []QueuedAction{}
|
|
|
|
thread.NextAction = nil
|
|
|
|
thread.TimeoutChan = nil
|
|
|
|
}
|
|
|
|
|
2023-07-24 16:04:56 -06:00
|
|
|
func (thread *Thread) ThreadHandle() *Thread {
|
|
|
|
return thread
|
|
|
|
}
|
|
|
|
|
|
|
|
func (thread *Thread) Type() NodeType {
|
2023-07-24 17:07:27 -06:00
|
|
|
return NodeType("thread")
|
2023-07-09 16:03:42 -06:00
|
|
|
}
|
|
|
|
|
2023-07-24 16:04:56 -06:00
|
|
|
func (thread *Thread) Serialize() ([]byte, error) {
|
|
|
|
thread_json := NewThreadJSON(thread)
|
2023-07-09 15:59:41 -06:00
|
|
|
return json.MarshalIndent(&thread_json, "", " ")
|
|
|
|
}
|
|
|
|
|
2023-07-24 16:04:56 -06:00
|
|
|
func (thread *Thread) ChildList() []ThreadNode {
|
|
|
|
ret := make([]ThreadNode, len(thread.Children))
|
|
|
|
i := 0
|
|
|
|
for _, info := range(thread.Children) {
|
|
|
|
ret[i] = info.Child
|
|
|
|
i += 1
|
|
|
|
}
|
|
|
|
return ret
|
2023-07-09 15:59:41 -06:00
|
|
|
}
|
|
|
|
|
2023-07-24 16:04:56 -06:00
|
|
|
type ThreadJSON struct {
|
2023-07-25 00:19:39 -06:00
|
|
|
LockableJSON
|
2023-07-21 00:06:11 -06:00
|
|
|
Parent string `json:"parent"`
|
2023-07-24 16:04:56 -06:00
|
|
|
Children map[string]map[string]interface{} `json:"children"`
|
2023-07-24 17:49:26 -06:00
|
|
|
ActionQueue []QueuedAction `json:"action_queue"`
|
2023-07-09 15:59:41 -06:00
|
|
|
StateName string `json:"state_name"`
|
2023-07-25 00:19:39 -06:00
|
|
|
InfoTypes []InfoType `json:"info_types"`
|
2023-07-09 15:59:41 -06:00
|
|
|
}
|
|
|
|
|
2023-07-24 16:04:56 -06:00
|
|
|
func NewThreadJSON(thread *Thread) ThreadJSON {
|
|
|
|
children := map[string]map[string]interface{}{}
|
|
|
|
for id, info := range(thread.Children) {
|
|
|
|
tmp := map[string]interface{}{}
|
|
|
|
for name, i := range(info.Infos) {
|
|
|
|
tmp[name.String()] = i
|
|
|
|
}
|
|
|
|
children[id.String()] = tmp
|
2023-07-09 15:59:41 -06:00
|
|
|
}
|
|
|
|
|
2023-07-21 00:06:11 -06:00
|
|
|
parent_id := ""
|
2023-07-24 16:04:56 -06:00
|
|
|
if thread.Parent != nil {
|
|
|
|
parent_id = thread.Parent.ID().String()
|
2023-07-09 15:59:41 -06:00
|
|
|
}
|
|
|
|
|
2023-07-24 16:04:56 -06:00
|
|
|
lockable_json := NewLockableJSON(&thread.Lockable)
|
2023-07-09 15:59:41 -06:00
|
|
|
|
2023-07-24 16:04:56 -06:00
|
|
|
return ThreadJSON{
|
2023-07-09 15:59:41 -06:00
|
|
|
Parent: parent_id,
|
|
|
|
Children: children,
|
2023-07-24 17:49:26 -06:00
|
|
|
ActionQueue: thread.ActionQueue,
|
2023-07-24 16:04:56 -06:00
|
|
|
StateName: thread.StateName,
|
|
|
|
LockableJSON: lockable_json,
|
2023-07-25 00:19:39 -06:00
|
|
|
InfoTypes: thread.InfoTypes,
|
2023-07-09 15:59:41 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-25 00:19:39 -06:00
|
|
|
var LoadThread = LoadJSONNode(func(id NodeID, j ThreadJSON) (Node, error) {
|
|
|
|
thread := NewThread(id, j.Name, j.StateName, j.InfoTypes, BaseThreadActions, BaseThreadHandlers)
|
2023-07-09 15:59:41 -06:00
|
|
|
return &thread, nil
|
2023-07-25 00:19:39 -06:00
|
|
|
}, RestoreThread)
|
2023-07-09 15:59:41 -06:00
|
|
|
|
2023-07-24 17:49:26 -06:00
|
|
|
func (thread *Thread) SoonestAction() (*QueuedAction, <-chan time.Time) {
|
|
|
|
var soonest_action *QueuedAction
|
|
|
|
var soonest_time time.Time
|
|
|
|
for _, action := range(thread.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-25 00:19:39 -06:00
|
|
|
func RestoreThread(ctx *Context, thread ThreadNode, j ThreadJSON, nodes NodeMap) error {
|
|
|
|
thread_ptr := thread.ThreadHandle()
|
|
|
|
|
|
|
|
thread_ptr.ActionQueue = j.ActionQueue
|
|
|
|
thread_ptr.NextAction, thread_ptr.TimeoutChan = thread_ptr.SoonestAction()
|
2023-07-09 15:59:41 -06:00
|
|
|
|
2023-07-21 00:06:11 -06:00
|
|
|
if j.Parent != "" {
|
|
|
|
parent_id, err := ParseID(j.Parent)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p, err := LoadNodeRecurse(ctx, parent_id, nodes)
|
2023-07-09 15:59:41 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-24 16:04:56 -06:00
|
|
|
p_t, ok := p.(ThreadNode)
|
2023-07-09 15:59:41 -06:00
|
|
|
if ok == false {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-25 00:19:39 -06:00
|
|
|
thread_ptr.Parent = p_t
|
2023-07-09 15:59:41 -06:00
|
|
|
}
|
|
|
|
|
2023-07-19 20:03:13 -06:00
|
|
|
for id_str, info_raw := range(j.Children) {
|
|
|
|
id, err := ParseID(id_str)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-24 16:04:56 -06:00
|
|
|
|
2023-07-09 15:59:41 -06:00
|
|
|
child_node, err := LoadNodeRecurse(ctx, id, nodes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-24 16:04:56 -06:00
|
|
|
|
|
|
|
child_t, ok := child_node.(ThreadNode)
|
2023-07-09 15:59:41 -06:00
|
|
|
if ok == false {
|
|
|
|
return fmt.Errorf("%+v is not a Thread as expected", child_node)
|
|
|
|
}
|
|
|
|
|
2023-07-24 16:04:56 -06:00
|
|
|
parsed_info, err := DeserializeChildInfo(ctx, info_raw)
|
2023-07-11 16:39:47 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-07-09 15:59:41 -06:00
|
|
|
}
|
|
|
|
|
2023-07-25 00:19:39 -06:00
|
|
|
thread_ptr.Children[id] = ChildInfo{child_t, parsed_info}
|
2023-07-24 16:04:56 -06:00
|
|
|
}
|
|
|
|
|
2023-07-25 00:19:39 -06:00
|
|
|
return RestoreLockable(ctx, thread, j.LockableJSON, nodes)
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func DeserializeChildInfo(ctx *Context, infos_raw map[string]interface{}) (map[InfoType]interface{}, error) {
|
|
|
|
ret := map[InfoType]interface{}{}
|
|
|
|
for type_str, info_raw := range(infos_raw) {
|
|
|
|
info_type := InfoType(type_str)
|
|
|
|
deserializer, exists := deserializers[info_type]
|
|
|
|
if exists == false {
|
|
|
|
return nil, fmt.Errorf("No deserializer for %s", info_type)
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
ret[info_type], err = deserializer(info_raw)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-07-09 15:59:41 -06:00
|
|
|
}
|
|
|
|
|
2023-07-24 16:04:56 -06:00
|
|
|
return ret, nil
|
2023-07-09 15:59:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
const THREAD_SIGNAL_BUFFER_SIZE = 128
|
2023-07-24 16:04:56 -06:00
|
|
|
func NewThread(id NodeID, name string, state_name string, info_types []InfoType, actions ThreadActions, handlers ThreadHandlers) Thread {
|
|
|
|
return Thread{
|
|
|
|
Lockable: NewLockable(id, name),
|
|
|
|
InfoTypes: info_types,
|
|
|
|
StateName: state_name,
|
|
|
|
Chan: make(chan GraphSignal, THREAD_SIGNAL_BUFFER_SIZE),
|
|
|
|
Children: map[NodeID]ChildInfo{},
|
|
|
|
Actions: actions,
|
|
|
|
Handlers: handlers,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (thread *Thread) SetActive(active bool) error {
|
|
|
|
thread.ActiveLock.Lock()
|
|
|
|
defer thread.ActiveLock.Unlock()
|
|
|
|
if thread.Active == true && active == true {
|
|
|
|
return fmt.Errorf("%s is active, cannot set active", thread.ID())
|
|
|
|
} else if thread.Active == false && active == false {
|
|
|
|
return fmt.Errorf("%s is already inactive, canot set inactive", thread.ID())
|
2023-07-09 15:59:41 -06:00
|
|
|
}
|
2023-07-24 16:04:56 -06:00
|
|
|
thread.Active = active
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (thread *Thread) SetState(state string) error {
|
|
|
|
thread.StateName = state
|
|
|
|
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-24 16:04:56 -06:00
|
|
|
func FindChild(context *StateContext, princ Node, node ThreadNode, id NodeID) ThreadNode {
|
|
|
|
if node == nil {
|
2023-06-23 20:56:09 -06:00
|
|
|
panic("cannot recurse through nil")
|
|
|
|
}
|
2023-07-24 16:04:56 -06:00
|
|
|
thread := node.ThreadHandle()
|
2023-06-23 20:56:09 -06:00
|
|
|
if id == thread.ID() {
|
|
|
|
return thread
|
|
|
|
}
|
|
|
|
|
2023-07-24 16:04:56 -06:00
|
|
|
for _, info := range thread.Children {
|
|
|
|
var result ThreadNode
|
|
|
|
UseStates(context, princ, NewLockInfo(info.Child, []string{"children"}), func(context *StateContext) error {
|
|
|
|
result = FindChild(context, princ, info.Child, id)
|
2023-06-28 00:48:49 -06:00
|
|
|
return nil
|
2023-06-23 20:56:09 -06:00
|
|
|
})
|
|
|
|
if result != nil {
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-24 16:04:56 -06:00
|
|
|
func ChildGo(ctx * Context, thread *Thread, child ThreadNode, first_action string) {
|
|
|
|
thread.ChildWaits.Add(1)
|
|
|
|
go func(child ThreadNode) {
|
2023-07-03 13:14:48 -06:00
|
|
|
ctx.Log.Logf("thread", "THREAD_START_CHILD: %s from %s", thread.ID(), child.ID())
|
2023-07-24 16:04:56 -06:00
|
|
|
defer thread.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-24 17:07:27 -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-03 13:14:48 -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-24 16:04:56 -06:00
|
|
|
func ThreadLoop(ctx * Context, node ThreadNode, first_action string) error {
|
2023-07-06 16:57:51 -06:00
|
|
|
// Start the thread, error if double-started
|
2023-07-24 16:04:56 -06:00
|
|
|
thread := node.ThreadHandle()
|
2023-07-06 16:57:51 -06:00
|
|
|
ctx.Log.Logf("thread", "THREAD_LOOP_START: %s - %s", thread.ID(), first_action)
|
2023-07-09 15:59:41 -06:00
|
|
|
err := thread.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-24 16:04:56 -06:00
|
|
|
action, exists := thread.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-06-26 21:20:04 -06:00
|
|
|
ctx.Log.Logf("thread", "THREAD_ACTION: %s - %s", thread.ID(), next_action)
|
2023-07-24 16:04:56 -06:00
|
|
|
next_action, err = action(ctx, node)
|
2023-06-23 20:56:09 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-09 15:59:41 -06:00
|
|
|
err = thread.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-06 16:57:51 -06:00
|
|
|
ctx.Log.Logf("thread", "THREAD_LOOP_DONE: %s", thread.ID())
|
2023-06-23 20:56:09 -06:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-24 16:04:56 -06:00
|
|
|
func ThreadChildLinked(ctx *Context, node ThreadNode, signal GraphSignal) (string, error) {
|
|
|
|
thread := node.ThreadHandle()
|
2023-07-23 23:01:45 -06:00
|
|
|
ctx.Log.Logf("thread", "THREAD_CHILD_LINKED: %+v", signal)
|
|
|
|
context := NewWriteContext(ctx)
|
2023-07-24 20:41:58 -06:00
|
|
|
err := UpdateStates(context, node, NewLockMap(
|
|
|
|
NewLockInfo(node, []string{"children"}),
|
2023-07-23 23:01:45 -06:00
|
|
|
), func(context *StateContext) error {
|
|
|
|
sig, ok := signal.(IDSignal)
|
|
|
|
if ok == false {
|
|
|
|
ctx.Log.Logf("thread", "THREAD_NODE_LINKED_BAD_CAST")
|
|
|
|
return nil
|
|
|
|
}
|
2023-07-24 16:04:56 -06:00
|
|
|
info, exists := thread.Children[sig.ID]
|
|
|
|
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 {
|
|
|
|
ChildGo(ctx, thread, 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
|
|
|
|
func ThreadStartChild(ctx *Context, node ThreadNode, signal GraphSignal) (string, error) {
|
2023-07-23 23:01:45 -06:00
|
|
|
sig, ok := signal.(StartChildSignal)
|
|
|
|
if ok == false {
|
|
|
|
return "wait", nil
|
|
|
|
}
|
|
|
|
|
2023-07-24 16:04:56 -06:00
|
|
|
thread := node.ThreadHandle()
|
2023-07-23 23:01:45 -06:00
|
|
|
|
2023-07-23 17:57:47 -06:00
|
|
|
context := NewWriteContext(ctx)
|
2023-07-24 20:21:14 -06:00
|
|
|
return "wait", UpdateStates(context, node, NewLockInfo(node, []string{"children"}), func(context *StateContext) error {
|
2023-07-24 16:04:56 -06:00
|
|
|
info, exists:= thread.Children[sig.ID]
|
|
|
|
if exists == false {
|
|
|
|
return fmt.Errorf("%s is not a child of %s", sig.ID, thread.ID())
|
2023-07-21 14:05:39 -06:00
|
|
|
}
|
2023-07-24 20:24:17 -06:00
|
|
|
return UpdateStates(context, node, NewLockInfo(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
|
|
|
|
ChildGo(ctx, thread, 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-24 16:04:56 -06:00
|
|
|
func ThreadRestore(ctx * Context, node ThreadNode, start bool) error {
|
|
|
|
thread := node.ThreadHandle()
|
2023-07-23 17:57:47 -06:00
|
|
|
context := NewWriteContext(ctx)
|
2023-07-24 20:21:14 -06:00
|
|
|
return UpdateStates(context, node, NewLockInfo(node, []string{"children"}), func(context *StateContext) error {
|
|
|
|
return UpdateStates(context, node, LockList(thread.ChildList(), []string{"start"}), func(context *StateContext) error {
|
2023-07-24 16:04:56 -06:00
|
|
|
for _, info := range(thread.Children) {
|
|
|
|
parent_info := info.Infos["parent"].(*ParentThreadInfo)
|
|
|
|
if parent_info.Start == true && info.Child.ThreadHandle().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-24 16:04:56 -06:00
|
|
|
ChildGo(ctx, thread, info.Child, parent_info.StartAction)
|
2023-07-23 23:01:45 -06:00
|
|
|
} else {
|
2023-07-24 16:04:56 -06:00
|
|
|
ChildGo(ctx, thread, 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
|
|
|
|
func ThreadStart(ctx * Context, node ThreadNode) (string, error) {
|
|
|
|
thread := node.ThreadHandle()
|
2023-07-23 17:57:47 -06:00
|
|
|
context := NewWriteContext(ctx)
|
2023-07-24 20:41:58 -06:00
|
|
|
err := UpdateStates(context, node, NewLockInfo(node, []string{"state"}), func(context *StateContext) error {
|
2023-07-24 20:37:41 -06:00
|
|
|
err := LockLockables(context, map[NodeID]LockableNode{node.ID(): node}, node)
|
2023-07-23 17:57:47 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-07-06 16:57:51 -06:00
|
|
|
}
|
2023-07-09 15:59:41 -06:00
|
|
|
return thread.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-24 20:37:41 -06:00
|
|
|
return "wait", Signal(context, node, node, NewStatusSignal("started", node.ID()))
|
2023-07-06 16:57:51 -06:00
|
|
|
}
|
|
|
|
|
2023-07-24 16:04:56 -06:00
|
|
|
func ThreadWait(ctx * Context, node ThreadNode) (string, error) {
|
|
|
|
thread := node.ThreadHandle()
|
2023-07-24 18:44:09 -06:00
|
|
|
ctx.Log.Logf("thread", "THREAD_WAIT: %s - %+v", thread.ID(), thread.ActionQueue)
|
2023-06-24 19:48:59 -06:00
|
|
|
for {
|
|
|
|
select {
|
2023-07-24 16:04:56 -06:00
|
|
|
case signal := <- thread.Chan:
|
2023-07-22 20:21:17 -06:00
|
|
|
ctx.Log.Logf("thread", "THREAD_SIGNAL: %s %+v", thread.ID(), signal)
|
2023-07-24 16:04:56 -06:00
|
|
|
signal_fn, exists := thread.Handlers[signal.Type()]
|
2023-06-24 19:48:59 -06:00
|
|
|
if exists == true {
|
|
|
|
ctx.Log.Logf("thread", "THREAD_HANDLER: %s - %s", thread.ID(), signal.Type())
|
2023-07-24 18:47:24 -06:00
|
|
|
return signal_fn(ctx, node, signal)
|
2023-06-25 21:00:00 -06:00
|
|
|
} else {
|
|
|
|
ctx.Log.Logf("thread", "THREAD_NOHANDLER: %s - %s", thread.ID(), signal.Type())
|
2023-06-24 19:48:59 -06:00
|
|
|
}
|
2023-07-24 16:04:56 -06:00
|
|
|
case <- thread.TimeoutChan:
|
2023-07-02 12:14:04 -06:00
|
|
|
timeout_action := ""
|
2023-07-23 17:57:47 -06:00
|
|
|
context := NewWriteContext(ctx)
|
2023-07-24 20:41:58 -06:00
|
|
|
err := UpdateStates(context, node, NewLockMap(NewLockInfo(node, []string{"timeout"})), func(context *StateContext) error {
|
2023-07-24 17:49:26 -06:00
|
|
|
timeout_action = thread.NextAction.Action
|
|
|
|
thread.NextAction, thread.TimeoutChan = thread.SoonestAction()
|
2023-07-02 12:14:04 -06:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
ctx.Log.Logf("thread", "THREAD_TIMEOUT_ERR: %s - %e", thread.ID(), err)
|
|
|
|
}
|
|
|
|
ctx.Log.Logf("thread", "THREAD_TIMEOUT %s - NEXT_STATE: %s", thread.ID(), timeout_action)
|
|
|
|
return timeout_action, nil
|
2023-06-24 19:48:59 -06:00
|
|
|
}
|
2023-06-23 20:56:09 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-24 16:04:56 -06:00
|
|
|
func ThreadFinish(ctx *Context, node ThreadNode) (string, error) {
|
|
|
|
thread := node.ThreadHandle()
|
2023-07-23 17:57:47 -06:00
|
|
|
context := NewWriteContext(ctx)
|
2023-07-24 20:41:58 -06:00
|
|
|
return "", UpdateStates(context, node, NewLockInfo(node, []string{"state"}), func(context *StateContext) error {
|
2023-07-23 17:57:47 -06:00
|
|
|
err := thread.SetState("finished")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-24 20:41:58 -06:00
|
|
|
return UnlockLockables(context, map[NodeID]LockableNode{node.ID(): node}, node)
|
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-24 16:04:56 -06:00
|
|
|
func ThreadAbort(ctx * Context, node ThreadNode, signal GraphSignal) (string, error) {
|
2023-07-23 17:57:47 -06:00
|
|
|
context := NewReadContext(ctx)
|
2023-07-24 20:41:58 -06:00
|
|
|
err := Signal(context, node, node, NewStatusSignal("aborted", node.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-24 16:04:56 -06:00
|
|
|
func ThreadStop(ctx * Context, node ThreadNode, signal GraphSignal) (string, error) {
|
2023-07-23 17:57:47 -06:00
|
|
|
context := NewReadContext(ctx)
|
2023-07-24 20:41:58 -06:00
|
|
|
err := Signal(context, node, node, NewStatusSignal("stopped", node.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
|
|
|
}
|