coroutine - Coroutine based event and IO handling
Contents
Api
All the commands listed below are synchronous with respect to the coroutine invoking them, i.e. this
coroutine blocks until the result is available. The overall eventloop is not blocked however.
coroutine::utilafterdelay
This command delays the coroutine invoking it by delay milliseconds.
coroutine::utilawaitvarname...
This command is an extension form of the coroutine::utilvwait command (see below) which waits on
a write to one of many named namespace variables.
coroutine::utilcreatearg...
This command creates a new coroutine with an automatically assigned name and causes it to run the
code specified by the arguments.
coroutine::utilexit ?status?
This command exits the current coroutine, causing it to return status. If no status was specified
the default 0 is returned.
coroutine::utilgetschan ?varname?
This command reads a line from the channel chan and returns it either as its result, or, if a
varname was specified, writes it to the named variable and returns the number of characters read.
coroutine::utilgets_safetychanlimitvarname
This command reads a line from the channel chan up to size limit and stores the result in varname.
Of limit is reached before the set first newline, an error is thrown. The command returns the
number of characters read.
coroutine::utilglobalvarname...
This command imports the named global variables of the coroutine into the current scope. From the
technical point of view these variables reside in level #1 of the Tcl stack. I.e. these are not
the regular global variable in to the global namespace, and each coroutine can have their own set,
independent of all others.
coroutine::utilputs ?-nonewline? channelstring
This commands writes the string to the specified channel. Contrary to the builtin puts this
command waits until the channel is writable before actually writing to it.
coroutine::utilread-nonewlinechan ?n?
This command reads n characters from the channel chan and returns them as its result. If n is not
specified the command will read the channel until EOF is reached.
coroutine::utilsocket ?options...? hostport
This command connects to the specified host and port and returns when that is done. Contrary to
the builtin command it performs a non-blocking connect in the background. As such, while its
blocks the calling coroutine, the overall application is not blocked.
coroutine::utilupdate ?idletasks?
This command causes the coroutine invoking it to run pending events or idle handlers before
proceeding.
coroutine::utilvwaitvarname
This command causes the coroutine calling it to wait for a write to the named namespace variable
varname.
Bugs, Ideas, Feedback
This document, and the package it describes, will undoubtedly contain bugs and other problems. Please
report such in the category coroutine of the TcllibTrackers [http://core.tcl.tk/tcllib/reportlist].
Please also report any ideas for enhancements you may have for either package and/or documentation.
When proposing code changes, please provide unifieddiffs, i.e the output of diff-u.
Note further that attachments are strongly preferred over inlined patches. Attachments can be made by
going to the Edit form of the ticket immediately after its creation, and then using the left-most button
in the secondary navigation bar.
Category
Coroutine
Copyright
Copyright (c) 2010-2015 Andreas Kupries <andreas_kupries@users.sourceforge.net>
tcllib 1.4 coroutine(3tcl)
Description
The coroutine package provides coroutine-aware implementations of various event- and channel related
commands. It can be in multiple modes:
[1] Call the commands through their ensemble, in code which is explicitly written for use within
coroutines.
[2] Import the commands into a namespace, either directly, or through namespacepath. This allows the
use from within code which is not coroutine-aware per se and restricted to specific namespaces.
A more agressive form of making code coroutine-oblivious than point 2 above is available through the
package coroutine::auto, which intercepts the relevant builtin commands and changes their implementation
dependending on the context they are run in, i.e. inside or outside of a coroutine.
Keywords
after, channel, coroutine, events, exit, gets, global, green threads, read, threads, update, vwait
Name
coroutine - Coroutine based event and IO handling
Synopsis
package require Tcl8.69
package require coroutine1.4coroutine::utilafterdelaycoroutine::utilawaitvarname...
coroutine::utilcreatearg...
coroutine::utilexit ?status?
coroutine::utilgetschan ?varname?
coroutine::utilgets_safetychanlimitvarnamecoroutine::utilglobalvarname...
coroutine::utilputs ?-nonewline? channelstringcoroutine::utilread-nonewlinechan ?n?
coroutine::utilsocket ?options...? hostportcoroutine::utilupdate ?idletasks?
coroutine::utilvwaitvarname
________________________________________________________________________________________________________________
