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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
<manpage cat="package" id="thread" title="Tcl Threading" version="2.5">
<!--
Copyright (c) 2002 Zoran Vasiljevic
See the file "license.terms" for information on usage and redistribution
of this file, and for a DISCLAIMER OF ALL WARRANTIES.
-->
<namesection>
<name>tsv</name>
<desc>Part of the Tcl threading extension allowing script-level manipulation
of data shared between threads in a simple, safe and protected fashion.</desc>
</namesection>
<synopsis>
<syntax>
<p>
<cmd>package require Thread ?2.5?</cmd></p>
<cmd>tsv::names</cmd> ?<m>pattern</m>?
<cmd>tsv::object</cmd> <m>array</m> <m>element</m>
<cmd>tsv::set</cmd> <m>array</m> <m>element</m> <m>value</m>
<cmd>tsv::get</cmd> <m>array</m> <m>element</m> ?<m>varname</m>?
<cmd>tsv::unset</cmd> <m>array</m> ?<m>element</m>?
<cmd>tsv::exists</cmd> <m>array</m> ?<m>element</m>?
<cmd>tsv::pop</cmd> <m>array</m> <m>element</m>
<cmd>tsv::move</cmd> <m>array</m> <m>old</m> <m>new</m>
<cmd>tsv::incr</cmd> <m>array</m> <m>element</m> ?<m>increment</m>?
<cmd>tsv::append</cmd> <m>array</m> <m>element</m> <m>value</m> ?<m>value</m> ...?
<cmd>tsv::lappend</cmd> <m>array</m> <m>element</m> <m>value</m> ?<m>value</m> ...?
<cmd>tsv::linsert</cmd> <m>array</m> <m>element</m> <m>index</m> <m>value</m> ?<m>value</m> ...?
<cmd>tsv::lreplace</cmd> <m>array</m> <m>element</m> <m>first</m> <m>last</m> ?<m>value</m> ...?
<cmd>tsv::llength</cmd> <m>array</m> <m>element</m>
<cmd>tsv::lindex</cmd> <m>array</m> <m>element</m> <m>index</m>
<cmd>tsv::lrange</cmd> <m>array</m> <m>element</m> <m>first</m> <m>last</m>
<cmd>tsv::lsearch</cmd> <m>array</m> <m>element</m> ?<m>mode</m>? <m>pattern</m>
<cmd>tsv::lpop</cmd> <m>array</m> <m>element</m> ?<m>index</m>?
<cmd>tsv::lpush</cmd> <m>array</m> <m>element</m> <m>value</m> ?<m>index</m>?
<cmd>tsv::lock</cmd> <m>arg</m> ?<m>arg ...</m>?
</syntax>
</synopsis>
<section>
<title>DESCRIPTION</title>
<p>
This section describes commands implementing thread shared variables. A thread
shared variable is very similar to a Tcl array but in contrast to a Tcl array
it is created in thread-shared memory and can be accessed from many threads at
the same time. Important feature of thread shared variable is that each access
to the variable is internaly protected by a mutex so script programmer does not have
to take care about locking the variable himself.</p>
<p>
Thread shared variables are not bound to any thread explicitly. That means that
when a thread which created any of thread shared variables exits, the variable
and associated memory is not unset/reclaimed. User has to explicitly unset the
variable to reclaim the memory consumed by the variable.</p>
</section>
<section>
<title>COMMANDS</title>
<commandlist>
<commanddef>
<command><cmd>tsv::names</cmd></command>
<desc>Returns names of shared variables matching optional <m>pattern</m> or all known
variables if pattern is ommited. The <cmd>tsv::names</cmd> returns an empty list on
process start. All thread shared variables are created by explicit user action.
</desc>
</commanddef>
<commanddef>
<command><cmd>tsv::object</cmd></command>
<desc>Creates object accessor command for the <m>element</m> in the given shared
<m>array</m>. Using this command, one can apply most of the other shared variable
commands as method functions of the element object command. The object command is
automatically deleted when the element which this command is pointing to is unset.
<example>
% tsv::set foo bar "A shared string"
% set string [tsv::object foo bar]
% $string append " appended"
=> A shared string appended</example>
</desc>
</commanddef>
<commanddef>
<command><cmd>tsv::set</cmd></command>
<desc>Sets the value of the <m>element</m> in the shared <m>array</m> to
<m>value</m> and returns the value. The <m>value</m> may be ommited, and
the command will return the current value of the <m>element</m>. If the
element cannot be found, error is triggered.
</desc>
</commanddef>
<commanddef>
<command><cmd>tsv::get</cmd></command>
<desc>Retrieves a value of the <m>element</m> located in the shared <m>array</m>.
The command triggers error if the <m>element</m> is not found. If the optional
<m>varname</m> is given, the value is stored in the named variable. In this case,
the command returns true (1) if <m>element</m> is found or false (0) if the
<m>element</m> is not found.
</desc>
</commanddef>
<commanddef>
<command><cmd>tsv::unset</cmd></command>
<desc>Deletes the <m>element</m> in the shared <m>array</m>. If the <m>element</m>
is not given, it deletes the whole <m>array</m>.
</desc>
</commanddef>
<commanddef>
<command><cmd>tsv::exists</cmd></command>
<desc>Checks wether the <m>element</m> exists in the shared <m>array</m>.
If the <m>element</m> is not given it tests the existence of the
<m>array</m> itself. Returns true (1) if the item exists,
false (0) if not.
</desc>
</commanddef>
<commanddef>
<command><cmd>tsv::pop</cmd></command>
<desc>Returns value of the <m>element</m> in the shared <m>array</m> variable
and unsets the <m>element</m> in one atomic operation.
</desc>
</commanddef>
<commanddef>
<command><cmd>tsv::move</cmd></command>
<desc>Renames the element <m>old</m> to <m>new</m> in the shared <m>array</m>.
This effectively performs an get/unset/set sequence of operations
but in one atomic step.
</desc>
</commanddef>
<commanddef>
<command><cmd>tsv::incr</cmd></command>
<desc>Similar to standard Tcl <cmd>incr</cmd> but increments the value of the
<m>element</m> in shared <m>array</m> instead of the Tcl variable.
</desc>
</commanddef>
<commanddef>
<command><cmd>tsv::append</cmd></command>
<desc>Similar to standard Tcl <cmd>append</cmd> but appends one or more values
to the <m>element</m> in the shared <m>array</m> instead of the Tcl variable.
</desc>
</commanddef>
<commanddef>
<command><cmd>tsv::lappend</cmd></command>
<desc>Similar to standard Tcl <cmd>lappend</cmd> but appends one or more values
to the list <m>element</m> in the shared <m>array</m> instead of the Tcl variable.
</desc>
</commanddef>
<commanddef>
<command><cmd>tsv::linsert</cmd></command>
<desc>Similar to standard Tcl <cmd>linsert</cmd> but inserts one or more values at the
<m>index</m> list position in the list <m>element</m> in the shared <m>array</m>
instead of the Tcl variable.
</desc>
</commanddef>
<commanddef>
<command><cmd>tsv::lreplace</cmd></command>
<desc>Similar to standard Tcl <cmd>lreplace</cmd> but replaces one or more values
from the list <m>element</m> in the shared <m>array</m> instead of the Tcl variable.
</desc>
</commanddef>
<commanddef>
<command><cmd>tsv::llength</cmd></command>
<desc>Similar to standard Tcl <cmd>llength</cmd> but returns length of the list
<m>element</m> in the shared <m>array</m> instead of the Tcl variable.
</desc>
</commanddef>
<commanddef>
<command><cmd>tsv::lindex</cmd></command>
<desc>Similar to standard Tcl <cmd>lindex</cmd> but returns value at the <m>index</m>
list position from the list <m>element</m> in the shared <m>array</m>
instead of the Tcl variable.
</desc>
</commanddef>
<commanddef>
<command><cmd>tsv::lrange</cmd></command>
<desc>Similar to standard Tcl <cmd>lrange</cmd> but returns values between <m>first</m>
and <m>last</m> list position from the list <m>element</m> in the shared <m>array</m>
instead of the Tcl variable.
</desc>
</commanddef>
<commanddef>
<command><cmd>tsv::lsearch</cmd></command>
<desc>Similar to standard Tcl <cmd>lsearch</cmd> but searches the list <m>element</m>
in the shared <m>array</m> instead of the Tcl variable.
</desc>
</commanddef>
<commanddef>
<command><cmd>tsv::lpop</cmd></command>
<desc>Splices out the value at the <m>index</m> list position from the list <m>element</m>
in the shared <m>array</m>. If <m>index</m> is not specified, it defaults to zero.
</desc>
</commanddef>
<commanddef>
<command><cmd>tsv::lpush</cmd></command>
<desc>Inserts the <m>value</m> at the <m>index</m> list position in the list <m>element</m>
in the shared <m>array</m>. If <m>index</m> is not specified, it defaults to zero.
</desc>
</commanddef>
<commanddef>
<command><cmd>tsv::lock</cmd></command>
<desc>This command concatenates passed arguments and evaluates the
resulting script under the internal mutex protection. During the
script evaluation, the entire shared array is locked. For shared
variable commands within the script, internal locking is disabled
so no deadlock can occur. It is also allowed to unset the shared
variable from within the script. The shared variable is automatically
created if it did not exists at the time of the first lock operation.
<example>
% tsv::lock foo {
# Atomically append two elements
tsv::lappend foo bar 1
tsv::lappend foo bar 2
puts stderr [tsv::array get foo]
tsv::unset foo
}
%</example>
</desc>
</commanddef>
<commanddef>
<command><cmd>tsv::array</cmd></command>
<desc>This command supports most of the options of the standard Tcl <cmd>array</cmd>
command like:
<commandlist>
<commanddef>
<command><cmd>tsv::array set</cmd></command>
<desc>Does the same as standard Tcl <samp>array set</samp>
</desc>
</commanddef>
<commanddef>
<command><cmd>tsv::array get</cmd></command>
<desc>Does the same as standard Tcl <samp>array get</samp>
</desc>
</commanddef>
<commanddef>
<command><cmd>tsv::array names</cmd></command>
<desc>Does the same as standard Tcl <samp>array names</samp>
</desc>
</commanddef>
<commanddef>
<command><cmd>tsv::array size</cmd></command>
<desc>Does the same as standard Tcl <samp>array size</samp>
</desc>
</commanddef>
<commanddef>
<command><cmd>tsv::array reset</cmd></command>
<desc>Does the same as standard Tcl <samp>array set</samp> but
it clears the array and sets new values atomically.
</desc>
</commanddef>
</commandlist>
</desc>
</commanddef>
</commandlist>
</section>
<section>
<title>DISCUSSION</title>
<p>The current implementation of thread shared variables allows easy and
convenient access to data to be shared between different threads.
Internally, the data is stored in Tcl objects and all package commands
operate on internal data representation, thus minimizing shimmering and
improving performance. Special care has been taken in assuring that all
object data is properly locked and copied when moving objects between threads.</p>
<p>
Due to the internal design of the Tcl core, there is no provision of full
integration of shared variables within the Tcl syntax, unfortunately. All
access to shared data must be performed with the supplied package commands.
Also, variable traces are not supported. But even so, benefits of easy,
simple and safe shared data manipulation outweights imposed limitations.</p>
</section>
<section>
<title>CREDITS</title>
<p>Thread shared variables are inspired by the nsv interface found in
AOLserver 3.+ highly scalable Web server from America Online.
</p>
</section>
<seealso>
<ref href="http://www.tcl.tk/doc/howto/thread_model.html">Guide to the Tcl threading model
</ref>
</seealso>
<keywords>
<keyword>threads</keyword>
<keyword>synchronization</keyword>
<keyword>locking</keyword>
<keyword>thread shared data</keyword>
</keywords>
</manpage>
|