namespace eval state {
variable threads [list main [thread::id]]
variable log [logger::init tclircc::state]
variable logt [logger::init tclircc::state::threads]
variable ns
proc manage_thread {tid name} {
variable threads
variable logt
dict set threads $name $tid
${logt}::debug "managing thread $tid as \"$name\""
thread::send -head $tid {
namespace eval state {
variable ns
proc update {} {
variable ns
thread::send -head [set ns(main)] state::update
}
}
}
::state::update
}
proc unmanage_thread {name} {
variable threads
variable logt
dict unset threads $name
${logt}::debug "unmanaging thread $tid (\"$name\")"
::state::update
}
proc update {} {
variable threads
dict for {name tid} $threads {
if ![thread::exists $tid] {dict unset $threads $name}
}
dict for {name tid} $threads {
set payload {array unset state::ns; }
dict for {n t} $threads {
append payload {set state::ns(} $n {) } $t {; }
}
thread::send -head $tid $payload
}
}
}