beetlebug▾
/

interp limit

Reads or sets the command limit or the time limit of a child.

interp limit PATH TYPE ?-OPTION VALUE ...?

Description

Bounds the work of the child of PATH, so that a parent can stop a child that runs with no end. TYPE is commands or time, or a unique prefix of one. With no option, gives a dictionary of the options of the limit. With one option, gives its value. With pairs of an option and a value, sets them and gives an empty string. Each value is checked before any change.

The command limit counts compute units, the count that info cmdcount gives in the child, so the limit stops the child at the same point in the interpreter and in the JIT, on each run. -value N stops the child when its count reaches N; an empty value removes the limit. The units of the child's own children count in the child. The units of an alias into the parent do not.

The time limit stops the child at a time: -seconds since 1970 and -milliseconds in that second. It needs the clock of the embedder (the capability clock); with no clock, a time limit cannot be set. The clock is read at each -granularity × 100 compute units.

At the limit, the -command script runs at the global level of the interpreter that set it, and can raise the limit. When the limit is still passed after it, the child stops with the error command count limit exceeded or time limit exceeded. catch in the child does not stop that error, and each later command of the child fails until the parent changes the limit. The parent can catch the error.

PATH must not be the current interpreter. The command of a child has the same form: $c limit commands -value 1000.

Arguments

ArgumentTypeDefaultDescription
PATHinterpreter pathrequiredA child of the current interpreter.
TYPEcommands or timerequiredThe limit to read or set.

Options

OptionTypeDefaultDescription
-commandscriptnoneThe script that runs at the limit, in the interpreter that set it.
-granularityinteger, 1 or morenoneFor the time limit, the reads of the clock: one at each N × 100 units.
-valueinteger, 0 or more, or emptynoneThe command limit: the count of compute units of the child.
-secondsinteger, 0 or more, or emptynoneThe time limit, in seconds since 1970.
-millisecondsinteger, 0 or morenoneThe milliseconds in the second of -seconds.

-command

The script that runs at the limit, in the interpreter that set it.

The script runs at the global level of the interpreter that set it, with no limit of the child, and can raise the limit. An error of the script is dropped. An empty script removes it.

# The callback raised the limit once, and the loop ended.
% set c [interp create]; set n 0
> interp limit $c commands -value 200 -command {incr n; interp limit $c commands -value 2000}
> interp eval $c {set k 0; while {$k < 300} {incr k}}
> set n
1

Any text is a script.

-granularity

For the time limit, the reads of the clock: one at each N × 100 units.

The default is 1 for the command limit and 10 for the time limit. The command limit keeps the value and stops at the exact count.

% set c [interp create]; interp limit $c time -granularity 5; interp limit $c time -granularity
5
% interp limit [interp create] commands -granularity 0
granularity must be at least 1
-errorcode TCL OPERATION INTERP BADVALUE

-value

The command limit: the count of compute units of the child.

The child stops when the count of info cmdcount in it reaches the value. An empty value removes the limit.

% set c [interp create]; interp limit $c commands -value 500; catch {interp eval $c {while 1 {}}} m; set m
command count limit exceeded
% interp limit [interp create] commands -value -1
command limit value must be at least 0
-errorcode TCL OPERATION INTERP BADVALUE

-seconds

The time limit, in seconds since 1970.

Needs the clock of the embedder: with no clock, a time limit gives the error the embedder gave no clock. An empty value removes the time limit.

# No time limit is set.
% set c [interp create]; interp limit $c time -seconds
% interp limit [interp create] time -seconds -1
seconds must be at least 0
-errorcode TCL OPERATION INTERP BADVALUE

-milliseconds

The milliseconds in the second of -seconds.

A value of 1000 or more adds whole seconds to -seconds. It needs a time limit, or -seconds in the same call.

% set c [interp create]; interp limit $c time -milliseconds
% interp limit [interp create] time -milliseconds 5
may only set -milliseconds if -seconds is also being set
-errorcode TCL OPERATION INTERP BADUSAGE

Returns

The options of the limit, the value of one option, or an empty string.

Examples

% set c [interp create]; interp limit $c commands
-command {} -granularity 1 -value {}

% interp limit $c commands -value 1000; catch {interp eval $c {while 1 {}}} m; set m
command count limit exceeded

% interp limit $c commands -value {}; interp eval $c {set a 1}
1

Errors

% interp limit {} commands -value 10
limits on current interpreter inaccessible
-errorcode NONE

% interp limit $c foo
bad limit type "foo": must be commands or time
-errorcode TCL LOOKUP INDEX {limit type}

Facts

Capability
interp. A new interpreter does not have it until the host turns it on.
Tcl release
Tcl 9.1 and 8.6.
Since
ztcl 1.0.0-beta.1

See also