~aleteoryx/tclircc

ref: 63922942ea1eb76534810686b711a550e3745c57 tclircc/threads.tcl -rw-r--r-- 1.4 KiB
63922942aleteoryx plugin version comparison, improved manifest reading 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
namespace eval threads {
  variable threads [list main [thread::id]]
  variable log [logger::init tclircc::threads]

  variable t_ns_script {
    namespace eval ::t {
      variable ns
      proc update {} {
        variable ns
        thread::send -head [set ns(main)] threads::update
      }
      proc ns {name} {
        variable ns
        set ns($name)
      }
      proc debug {} {
        variable log
        parray t::ns
      }
    }
  }
  eval $t_ns_script

  proc manage {tid name} {
    variable threads
    variable log
    variable t_ns_script
    dict set threads $name $tid
    ${log}::debug "managing thread $tid as \"$name\""

    thread::send -head $tid $t_ns_script

    ::threads::update
  }
  proc unmanage {name} {
    variable threads
    variable log
    dict unset threads $name
    ${log}::debug "unmanaging thread $tid (\"$name\")"
    ::threads::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 t::ns; }
      dict for {n t} $threads {
        append payload {set t::ns(} $n {) } $t {; }
      }
      append payload {
        if {"on_threads_update" in [info procs on_threads_update]} \
          on_threads_update
      }
      thread::send -head $tid $payload
    }
    ::update
  }
}