<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"
=&gt; 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>
