~aleteoryx/tclircc

ref: b0e45371a0985433472b08026fa7d2406c876260 tclircc/state.tcl -rw-r--r-- 1.1 KiB
b0e45371aleteoryx rpcbind who a month ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
    }
  }
}