<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE TIP SYSTEM "http://www.tcl.tk/cgi-bin/tct/tip/tipxml.dtd">
<!-- Converted at Thu May 17 03:57:57 GMT 2012 -->
<!-- TIP AutoGenerator - written by Donal K. Fellows -->

<TIP number='301'>
<header><title>Split Bidirectional Channels For Half-Close</title><author address="mailto:alexandre.ferrieux@gmail.com">Alexandre Ferrieux</author><status type='project' state='withdrawn' tclversion="8.6" vote='prior'>$Revision: 1.2 $</status><history></history><created day='11' month='dec' year='2006' /><obsoleted tip='332'/></header>
<abstract>This TIP proposes to introduce a <emph style="bold">chan split</emph> command allowing to access both sides of a bidirectional channel (r+ pipe, or socket) as separate Tcl channels, in order to be able to close them separately. This would give Tcl the same level of control that the OS enjoys on the lifetime of such bidirectional connections.</abstract>
<body><section title="Background">
<para>Bidirectional channels allow Tcl to make an efficient use of a &quot;filter process&quot;, by exchanging data back and forth over an abstract &quot;single&quot; channel.</para>
<para>However, this single channel abstraction comes with a too coarse-grained <emph style="bold">close</emph> primitive. Indeed, it closes both directions simultaneously, while it is often desirable to close &quot;gracefully&quot; the half-connection <emph style="italic">to</emph> the filter process, leaving the return path open. The effect of such a half-close is that the filter receives a bona fide EOF alone, without a nearly simultaneous SIGPIPE on its write end if it happens to be writing at that time. Moreover, if the filter is itself comprised of a pipeline of processes, some of which doing buffered I/O, then this graceful EOF may be the only way of flushing the chain and receiving back precious data.</para>
<para>This technique is supported by all modern OSes: for pipes there are actually two separate file descriptors/handles, and it suffices to close() the write side; for sockets, a single fd is used, but a specific syscall, shutdown(), brings back the ability to half-close. Hence it is fairly natural for a universal &quot;OS glove&quot; like Tcl to expose this universal feature.</para>
</section>
<section title="Proposed Change">
<para>This TIP proposes to add a <emph style="bold">chan split</emph> subcommand to <emph style="bold">chan</emph>, with the syntax:</para>
<quote><emph style="bold">chan split</emph> <emph style="italic">channel</emph></quote>
<para>The <emph style="italic">channel</emph> argument indicates the bidirectional channel to be split. The command returns a two element list, with the first element being the name of the channel that is open for reading only, and the second element being the name of the channel that is open for writing only.</para>
<para>After calling [<emph style="bold">chan split</emph> <emph style="italic">channel</emph>], <emph style="italic">channel</emph> is still opened, but now shares its internal descriptor(s)/handle(s) with the returned readable and writable channel. Then, simple &quot;reference counting&quot; at the descriptor level decides when a <emph style="bold">close</emph> translates into an actual close()/shutdown() at the OS level: a descriptor is shut down only when all its Tcl-level representatives have been closed.</para>
<para>As a consequence, the normal idiom for the graceful shutdown described above, is to close the bidirectional channel immediately after splitting it. Later, the sequence is initiated by closing the write side, then waiting for all data and EOF on the read side:</para>
<verbatim><vline encoding='base64'>Zm9yZWFjaCB7cmNoIHdjaH0gW2NoYW4gc3BsaXQgJGNoXSBicmVhaw==</vline><vline encoding='base64'>Y2xvc2UgJGNo</vline><vline encoding='base64'>Li4u</vline><vline encoding='base64'>IyBpbml0aWF0ZSBncmFjZWZ1bCBzaHV0ZG93bg==</vline><vline encoding='base64'>Y2xvc2UgJHdjaA==</vline><vline encoding='base64'>IyBnZXQgYmFjayBmaW5hbCBkYXRh</vline><vline encoding='base64'>c2V0IGxhc3QgW3JlYWQgJHJjaF0=</vline><vline encoding='base64'>Y2xvc2UgJHJjaA==</vline></verbatim>
<para>In the case of a pipe, the [close $rch] has the same semantics as [close $ch] would have had, that is, waiting for the child&apos;s exit status, and raising a Tcl exception if nonzero.</para>
</section>
<section title="Rationale">
<para>I had initially only asked for an extension of <emph style="bold">close</emph>, and Eric Hassold brought up the superbly elegant idea of <emph style="bold">chan split</emph>. Later on, Neil Madden came with the equally beautiful <emph style="bold">chan restrict</emph>, which initially won my preference. Only dirty concern for the amount of work needed vs. actual uses, did make me regard the less ambitious <emph style="bold">chan split</emph> as a more reasonable target (sorry for the change, Neil).</para>
<para>Also, it was suggested that the separation between the read and write side could be exploited not only by <emph style="bold">close</emph>, but also by <emph style="bold">fconfigure</emph>, e.g., to allow for a blocking-read/nonblocking-write, or different baudrates for both directions of a serial port, etc. I feel those are possible later extensions of <emph style="bold">chan split</emph>, however their implementation may be much harder than the <emph style="bold">close</emph> case, because (1) ioctls/fcntl() are not always suited for separation, and (2) multiplexing them is not possible with Threads.</para>
</section>
<section title="Reference Implementation">
<para>I have not yet written a reference implementation; I assume somebody with a more fluent practice of the channel system implementation will do so more efficiently. However, community feedback on <url ref="news:comp.lang.tcl"/> seems to witness some interest in the concept. As a last resort, I can give it a try, of course.</para>
</section>
<section title="Copyright">
<para>This document has been placed in the public domain.</para>
</section>
</body></TIP>

