| 1 |
'\" |
| 2 |
'\" Copyright (c) 1995-1996 Sun Microsystems, Inc. |
| 3 |
'\" |
| 4 |
'\" See the file "license.terms" for information on usage and redistribution |
| 5 |
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 6 |
'\" |
| 7 |
.TH vwait n 8.0 Tcl "Tcl Built-In Commands" |
| 8 |
.so man.macros |
| 9 |
.BS |
| 10 |
'\" Note: do not modify the .SH NAME line immediately below! |
| 11 |
.SH NAME |
| 12 |
vwait \- Process events until a variable is written |
| 13 |
.SH SYNOPSIS |
| 14 |
\fBvwait\fR \fIvarName\fR |
| 15 |
.BE |
| 16 |
.SH DESCRIPTION |
| 17 |
.PP |
| 18 |
This command enters the Tcl event loop to process events, blocking |
| 19 |
the application if no events are ready. It continues processing |
| 20 |
events until some event handler sets the value of the global variable |
| 21 |
\fIvarName\fR. Once \fIvarName\fR has been set, the \fBvwait\fR |
| 22 |
command will return as soon as the event handler that modified |
| 23 |
\fIvarName\fR completes. The \fIvarName\fR argument is always interpreted as |
| 24 |
a variable name with respect to the global namespace, but can refer to any |
| 25 |
namespace's variables if the fully-qualified name is given. |
| 26 |
.PP |
| 27 |
In some cases the \fBvwait\fR command may not return immediately |
| 28 |
after \fIvarName\fR is set. This happens if the event handler |
| 29 |
that sets \fIvarName\fR does not complete immediately. For example, |
| 30 |
if an event handler sets \fIvarName\fR and then itself calls |
| 31 |
\fBvwait\fR to wait for a different variable, then it may not return |
| 32 |
for a long time. During this time the top-level \fBvwait\fR is |
| 33 |
blocked waiting for the event handler to complete, so it cannot |
| 34 |
return either. (See the \fBNESTED VWAITS BY EXAMPLE\fR below.) |
| 35 |
.PP |
| 36 |
To be clear, \fImultiple \fBvwait\fI calls will nest and will not happen in |
| 37 |
parallel\fR. The outermost call to \fBvwait\fR will not return until all the |
| 38 |
inner ones do. It is recommended that code should never nest \fBvwait\fR |
| 39 |
calls (by avoiding putting them in event callbacks) but when that is not |
| 40 |
possible, care should be taken to add interlock variables to the code to |
| 41 |
prevent all reentrant calls to \fBvwait\fR that are not \fIstrictly\fR |
| 42 |
necessary. Be aware that the synchronous modes of operation of some Tcl |
| 43 |
packages (e.g.,\ \fBhttp\fR) use \fBvwait\fR internally; if using the event |
| 44 |
loop, it is best to use the asynchronous callback-based modes of operation of |
| 45 |
those packages where available. |
| 46 |
.SH EXAMPLES |
| 47 |
.PP |
| 48 |
Run the event-loop continually until some event calls \fBexit\fR. |
| 49 |
(You can use any variable not mentioned elsewhere, but the name |
| 50 |
\fIforever\fR reminds you at a glance of the intent.) |
| 51 |
.PP |
| 52 |
.CS |
| 53 |
\fBvwait\fR forever |
| 54 |
.CE |
| 55 |
.PP |
| 56 |
Wait five seconds for a connection to a server socket, otherwise |
| 57 |
close the socket and continue running the script: |
| 58 |
.PP |
| 59 |
.CS |
| 60 |
# Initialise the state |
| 61 |
after 5000 set state timeout |
| 62 |
set server [socket -server accept 12345] |
| 63 |
proc accept {args} { |
| 64 |
global state connectionInfo |
| 65 |
set state accepted |
| 66 |
set connectionInfo $args |
| 67 |
} |
| 68 |
|
| 69 |
# Wait for something to happen |
| 70 |
\fBvwait\fR state |
| 71 |
|
| 72 |
# Clean up events that could have happened |
| 73 |
close $server |
| 74 |
after cancel set state timeout |
| 75 |
|
| 76 |
# Do something based on how the vwait finished... |
| 77 |
switch $state { |
| 78 |
timeout { |
| 79 |
puts "no connection on port 12345" |
| 80 |
} |
| 81 |
accepted { |
| 82 |
puts "connection: $connectionInfo" |
| 83 |
puts [lindex $connectionInfo 0] "Hello there!" |
| 84 |
} |
| 85 |
} |
| 86 |
.CE |
| 87 |
.PP |
| 88 |
A command that will wait for some time delay by waiting for a namespace |
| 89 |
variable to be set. Includes an interlock to prevent nested waits. |
| 90 |
.PP |
| 91 |
.CS |
| 92 |
namespace eval example { |
| 93 |
variable v done |
| 94 |
proc wait {delay} { |
| 95 |
variable v |
| 96 |
if {$v ne "waiting"} { |
| 97 |
set v waiting |
| 98 |
after $delay [namespace code {set v done}] |
| 99 |
\fBvwait\fR [namespace which -variable v] |
| 100 |
} |
| 101 |
return $v |
| 102 |
} |
| 103 |
} |
| 104 |
.CE |
| 105 |
.PP |
| 106 |
When running inside a \fBcoroutine\fR, an alternative to using \fBvwait\fR is |
| 107 |
to \fByield\fR to an outer event loop and to get recommenced when the variable |
| 108 |
is set, or at an idle moment after that. |
| 109 |
.PP |
| 110 |
.CS |
| 111 |
coroutine task apply {{} { |
| 112 |
# simulate [after 1000] |
| 113 |
after 1000 [info coroutine] |
| 114 |
yield |
| 115 |
|
| 116 |
# schedule the setting of a global variable, as normal |
| 117 |
after 2000 {set var 1} |
| 118 |
|
| 119 |
# simulate [\fBvwait\fR var] |
| 120 |
proc updatedVar {task args} { |
| 121 |
after idle $task |
| 122 |
trace remove variable ::var write "updatedVar $task" |
| 123 |
} |
| 124 |
trace add variable ::var write "updatedVar [info coroutine]" |
| 125 |
yield |
| 126 |
}} |
| 127 |
.CE |
| 128 |
.SS "NESTED VWAITS BY EXAMPLE" |
| 129 |
.PP |
| 130 |
This example demonstrates what can happen when the \fBvwait\fR command is |
| 131 |
nested. The script will never finish because the waiting for the \fIa\fR |
| 132 |
variable never finishes; that \fBvwait\fR command is still waiting for a |
| 133 |
script scheduled with \fBafter\fR to complete, which just happens to be |
| 134 |
running an inner \fBvwait\fR (for \fIb\fR) even though the event that the |
| 135 |
outer \fBvwait\fR was waiting for (the setting of \fIa\fR) has occurred. |
| 136 |
.PP |
| 137 |
.CS |
| 138 |
after 500 { |
| 139 |
puts "waiting for b" |
| 140 |
\fBvwait\fR b |
| 141 |
puts "b was set" |
| 142 |
} |
| 143 |
after 1000 { |
| 144 |
puts "setting a" |
| 145 |
set a 10 |
| 146 |
} |
| 147 |
puts "waiting for a" |
| 148 |
\fBvwait\fR a |
| 149 |
puts "a was set" |
| 150 |
puts "setting b" |
| 151 |
set b 42 |
| 152 |
.CE |
| 153 |
.PP |
| 154 |
If you run the above code, you get this output: |
| 155 |
.PP |
| 156 |
.CS |
| 157 |
waiting for a |
| 158 |
waiting for b |
| 159 |
setting a |
| 160 |
.CE |
| 161 |
.PP |
| 162 |
The script will never print |
| 163 |
.QW "a was set" |
| 164 |
until after it has printed |
| 165 |
.QW "b was set" |
| 166 |
because of the nesting of \fBvwait\fR commands, and yet \fIb\fR will not be |
| 167 |
set until after the outer \fBvwait\fR returns, so the script has deadlocked. |
| 168 |
The only ways to avoid this are to either structure the overall program in |
| 169 |
continuation-passing style or to use \fBcoroutine\fR to make the continuations |
| 170 |
implicit. The first of these options would be written as: |
| 171 |
.PP |
| 172 |
.CS |
| 173 |
after 500 { |
| 174 |
puts "waiting for b" |
| 175 |
trace add variable b write {apply {args { |
| 176 |
global a b |
| 177 |
trace remove variable ::b write \e |
| 178 |
[lrange [info level 0] 0 1] |
| 179 |
puts "b was set" |
| 180 |
set ::done ok |
| 181 |
}}} |
| 182 |
} |
| 183 |
after 1000 { |
| 184 |
puts "setting a" |
| 185 |
set a 10 |
| 186 |
} |
| 187 |
puts "waiting for a" |
| 188 |
trace add variable a write {apply {args { |
| 189 |
global a b |
| 190 |
trace remove variable a write [lrange [info level 0] 0 1] |
| 191 |
puts "a was set" |
| 192 |
puts "setting b" |
| 193 |
set b 42 |
| 194 |
}}} |
| 195 |
\fBvwait\fR done |
| 196 |
.CE |
| 197 |
.PP |
| 198 |
The second option, with \fBcoroutine\fR and some helper procedures, is done |
| 199 |
like this: |
| 200 |
.PP |
| 201 |
.CS |
| 202 |
# A coroutine-based wait-for-variable command |
| 203 |
proc waitvar globalVar { |
| 204 |
trace add variable ::$globalVar write \e |
| 205 |
[list apply {{v c args} { |
| 206 |
trace remove variable $v write \e |
| 207 |
[lrange [info level 0] 0 3] |
| 208 |
after 0 $c |
| 209 |
}} ::$globalVar [info coroutine]] |
| 210 |
yield |
| 211 |
} |
| 212 |
# A coroutine-based wait-for-some-time command |
| 213 |
proc waittime ms { |
| 214 |
after $ms [info coroutine] |
| 215 |
yield |
| 216 |
} |
| 217 |
|
| 218 |
coroutine task-1 eval { |
| 219 |
puts "waiting for a" |
| 220 |
waitvar a |
| 221 |
puts "a was set" |
| 222 |
puts "setting b" |
| 223 |
set b 42 |
| 224 |
} |
| 225 |
coroutine task-2 eval { |
| 226 |
waittime 500 |
| 227 |
puts "waiting for b" |
| 228 |
waitvar b |
| 229 |
puts "b was set" |
| 230 |
set done ok |
| 231 |
} |
| 232 |
coroutine task-3 eval { |
| 233 |
waittime 1000 |
| 234 |
puts "setting a" |
| 235 |
set a 10 |
| 236 |
} |
| 237 |
\fBvwait\fR done |
| 238 |
.CE |
| 239 |
.SH "SEE ALSO" |
| 240 |
global(n), update(n) |
| 241 |
.SH KEYWORDS |
| 242 |
asynchronous I/O, event, variable, wait |
| 243 |
'\" Local Variables: |
| 244 |
'\" mode: nroff |
| 245 |
'\" fill-column: 78 |
| 246 |
'\" End: |