Added optional timeout on Signalsource::waitfor
[tcl-tlc.git] / scripts / signalsource.itcl
blobe99b0e93c90593a23754b0821ec581d0e22feda6
1 # vim: ft=tcl foldmethod=marker foldmarker=<<<,>>> ts=4 shiftwidth=4
3 class tlc::Signalsource {
4 constructor {} {}
5 destructor {}
7 public {
8 method signal_ref {signal}
9 method signal_state {signal}
10 method waitfor {signal {timeout 0}}
11 method signals_available {}
14 protected {
15 variable signals
18 private {
19 variable signotify
20 variable afters
22 method signotify {signame newstate}
27 body tlc::Signalsource::constructor {} { #<<<1
28 array set signals {}
29 array set signotify {}
30 array set afters {}
34 body tlc::Signalsource::destructor {} { #<<<1
35 tlc::try {
36 foreach afterid [array names afters] {
37 after cancel $afterid
38 array unset afters $afterid
40 } onerr {
41 default {STDMSG}
46 body tlc::Signalsource::signal_ref {signal} { #<<<1
47 if {![info exists signals($signal)]} {
48 error "Invalid signal ($signal)"
50 return $signals($signal)
54 body tlc::Signalsource::signal_state {signal} { #<<<1
55 if {![info exists signals($signal)]} {
56 error "Invalid signal ($signal)"
58 return [$signals($signal) state]
62 body tlc::Signalsource::waitfor {signame {timeout 0}} { #<<<1
63 if {![info exists signals($signame)]} {
64 error "Invalid signal: ($signame)"
66 if {![$signals($signame) state]} {
67 $signals($signame) attach_output [code $this signotify $signame]
68 if {$timeout != 0} {
69 set afterid [after $timeout [list set [scope signotify($signame)] 0]]
70 set afters($afterid) 1
72 vwait [scope signotify($signame)]
73 if {[info exists afterid]} {
74 after cancel $afterid
75 array unset afters $afterid
76 unset afterid
78 set res $signotify($signame)
79 array unset signotify($signame)
80 if {$res != 1} {
81 error "Timeout waiting for signal \"$signame\"" "" \
82 [list timeout $signame]
84 #puts stderr "${this}::waitfor got ($signame) true, proceeding"
86 return
90 body tlc::Signalsource::signotify {signame newstate} { #<<<1
91 if {$newstate} {
92 set signotify($signame) 1
97 body tlc::Signalsource::signals_available {} { #<<<1
98 set build {}
99 foreach signame [lsort -dictionary [array names signals]] {
100 lappend build $signame $signals($signame)
102 return $build