\documentclass[]{article}
\usepackage{amsmath,graphicx,supertabular,hyperref,tabularx,ifthen}
\title{TIP \#233: Virtualization of Tcl's Sense of Time}
\date{November 30, 2004}
\author{Andreas Kupries, Andreas Kupries}
\urlstyle{sf}
\setlength{\parskip}{1ex}
\setlength{\parindent}{0pt}
\def\tipversion$#1${\texttt{\$#1\$}}
\def\tiplangle#1{\ensuremath{<}}
\def\tiprangle#1{\ensuremath{>}}
\def\tipbar#1{\ensuremath{|}}
\def\tipmail#1#2{\(\langle\){\small\expandafter\url{#1@#2}}\(\rangle\)}
\ifx\pdfoutput\undefined
\newcommand{\tipimage}[2]{\typeout{Make sure you download #1.eps}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.eps}}{\includegraphics[width=0.8\textwidth]{#1.eps}}}
\newcommand{\tipxref}[1]{}
\newcommand{\tipxrefend}{}
\else
\newcommand{\tipimage}[2]{\typeout{Make sure you create #1.pdf}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.pdf}}{\includegraphics[width=0.8\textwidth]{#1.pdf}}}
\pdfcatalog{/PageMode /UseOutlines}
\newcommand{\tipxref}[1]{\pdfannotlink attr {/C [0.5 0.5 1.0] /Border [0 0 1]} goto name {#1}}
\newcommand{\tipxrefend}{\pdfendlink}
\fi
\newenvironment{tipabstract}{\begin{abstract}}{\end{abstract}}
\begin{document}\maketitle
\begin{center}\begin{tabularx}{\linewidth}{|r@{: }X|}\hline
\textbf{TIP \#233}&\textbf{Virtualization of Tcl's Sense of Time}\\\hline
Author&

Andreas Kupries \tipmail{andreas_kupries}{users.sf.net}
\par
Andreas Kupries \tipmail{akupries}{shaw.ca}
\\
Created&Tuesday, \(\text{30}^{\text{th}}\) November 2004\\
Type&Project\\
Tcl Version&\(8.5\)\\
State&Final\\
Vote&Done\\
Version&\tipversion$Revision: 1.6 $\\
Post History&\\
\hline\end{tabularx}\end{center}
\thispagestyle{empty}\pagestyle{empty}
\begin{tipabstract}

This document describes a number of changes to internal and public APIs which allows external code to hook into the routines through which the Tcl core computes time-dependent information, and to override them. Through this the external code can manipulate Tcl's sense of time.
\end{tipabstract}
\tableofcontents\setcounter{page}{0}\clearpage\pagestyle{plain}

\section{Background and Motivation}

The purpose of this change is to allow code embedding the Tcl core, like simulators of all kind, to manipulate Tcl's sense of time. Currently everything in the core dependent on time, like event handling and all clock operations will always operate in real-time, as the core gets the basic information directly from the operating system. At least in simulators however it can make sense to have Tcl run in the virtual time, and not real-time. This is currently not possible, and this TIP proposes to change this, and how.

\section{Specification}

At the Tcl level nothing changes. All changes are at the C level, extending and changing existing APIs.

Two new functions, \textbf{Tcl\_SetTimeProc} and \textbf{Tcl\_QueryTimeProc}, are added to the public API of the core. Their signatures are:
\begingroup\small\begin{verbatim}
 void Tcl_SetTimeProc(Tcl_GetTimeProc *getProc,
                      Tcl_ScaleTimeProc *scaleProc,
                      ClientData clientData);

 void Tcl_QueryTimeProc(Tcl_GetTimeProc **getProc,
                        Tcl_ScaleTimeProc **scaleProc,
                        ClientData *clientData);

 typedef void (Tcl_GetTimeProc)   (Tcl_Time *timebuf, ClientData clientData);
 typedef void (Tcl_ScaleTimeProc) (Tcl_Time *timebuf, ClientData clientData);
\end{verbatim}
\endgroup

The first function registers two related handler functions with the core. The first handler function is a replacement for \textbf{Tcl\_GetTime}, or rather the OS access made by \textbf{Tcl\_GetTime}. The other handler function is used by the Tcl notifier to convert wait/block times from virtual into real time.

The second function returns the currently registered handler functions. If no external handlers were set then this will return the handlers accessing and processing the native time of the OS.

Any handler pair specified has to return data which is consistent between them. In other words, setting one handler of the pair to something assuming a 10-times slowdown, and the other handler of the pair to something assuming a two-times slowdown is wrong and not allowed.

The set handlers are allowed to run the delivered time backwards, however this should be avoided. We have to allow it as the native time can run backwards as the user can fiddle with the system time one way or other. Note that the insertion of the hooks will not change the behaviour of the Tcl core with regard to this situation, i.e. the existing behaviour is retained.

The implementations of \textbf{Tcl\_GetTime} and \textbf{TclpGetClicks} are changed to check for the presence of a handler first, and will ask the operating system for the current time if and only if no handler has been registered. Both use the handler \textit{Tcl\_GetTimeProc}.

The implementations of \textbf{Tcl\_Sleep}, \textbf{Tcl\_WaitForEvent} and \textbf{NotifierThreadProc} are changed to check for the presence of a handler as well, and will invoke it with the current wait/block time if it is present. The handler is not invoked if it is either not present, or if no timeout was specified, or if the timeout is zero. Both use the handler \textit{Tcl\_ScaleTimeProc}.

\section{Limitations}

While the proposed changes are able to change Tcl's view of the current time, and of time intervals, no effort was made to reduce the execution speed of the interpreter itself, i,.e. of the bytecode engine.

This means that from the point of view of the virtual time the execution of bytecode has become faster. This can change the relative timing of events. For example the window for a race condition is in virtual time effectively reduced in size by the scaling factor between virtual and real time, becoming less likely.

\section{Reference Implementation}

A reference implementation is provided at SourceForge [\url{http://sourceforge.net/support/tracker.php?aid=1073863}].

\section{Comments}

[ Add comments on the document here ]

\section{Copyright}

This document has been placed in the public domain.

\section{Colophon}

\textit{TIP AutoGenerator --- written by Donal K. Fellows}
\end{document}
