TIP 335: An API for Detecting Active Interpreters

Login
Author:		Joe Mistachkin <[email protected]>
State:		Final
Type:		Project
Vote:		Done
Created:	13-Oct-2008
Keywords:	numLevels,embedding,terminate,async,thread,safe,gc
Tcl-Version:	8.6
Post-History:	

Abstract

This TIP introduces the ability to quickly and safely decide whether a Tcl script is evaluating in an interpreter, allowing an external system to determine whether it is safe to delete that interpreter.

Rationale

For applications written in garbage-collected languages, such as C#, it is not always desirable to rely upon the Tcl_Preserve / Tcl_Release mechanism to protect an against deletion of an interpreter while it is in use. For details of how this is used, see http://wiki.tcl.tk/6580 http://eagle.to/ and Joe Mistachkin's talk at Tcl 2008.

Additionally, an application may want to proactively forbid attempts to delete an interpreter while it is in use. Unfortunately, there is currently no publicly exposed method to determine if a given Tcl interpreter is in use (i.e. one or more calls to Tcl_Eval are active). This TIP proposes to correct that deficiency.

Specification

This TIP introduces a single function to Tcl's public API:

int Tcl_InterpActive(Tcl_Interp *interp)

The Tcl_InterpActive function returns non-zero if the interpreter is in use, and zero if it is idle (i.e. not evaluating any script).

Reference Implementation

/*
 *----------------------------------------------------------------------
 *
 * Tcl_InterpActive --
 *
 *	Returns non-zero if the specified interpreter is in use.
 *
 * Results:
 *	See above.
 *
 * Side effects:
 *	None.
 *
 *----------------------------------------------------------------------
 */

int
Tcl_InterpActive(Tcl_Interp *interp)
{
    return (((Interp *) interp)->numLevels > 0);
}

Copyright

This document has been placed in the public domain.