beetlebug▾
/

thread create

Starts a new thread that runs a script, and gives its id.

thread::create ?-joinable? ?-preserved? ?-isolation LEVEL? ?-guest GUEST? ?-files host|root? ?-write PATHS? ?-caps LIST? ?-compute UNITS? ?-memory BYTES? ?-stack BYTES? ?-meter NAME? ?--? ?SCRIPT?

Description

The command thread::create starts a thread with a new interpreter, waits until the interpreter exists, and gives the id of the thread. The thread runs SCRIPT. At the end of SCRIPT the thread ends, unless SCRIPT calls thread::wait, which serves the messages of other threads until a release. With no SCRIPT, the thread runs thread::wait, so it waits for messages.

A new thread has the capabilities of its creator, the JIT options of its creator, and the settings of its collector. Its compute limit and memory limit are the limits of one thread of the group. An error of SCRIPT goes to the proc of thread::errorproc, or to the output.

The options come before SCRIPT. The parse of the options stops at the first word that is not an option, so a word such as -bogus is the script. -- ends the options. The examples of this page run with --allow thread --allow meter.

Arguments

ArgumentTypeDefaultDescription
SCRIPTscriptnoneThe script of the thread, or none for a thread that waits for messages.

Options

OptionTypeDefaultDescription
-joinableflagnoneMakes a thread that thread::join can wait for.
-preservedflagnoneStarts the thread with a reference count of 1.
-isolationone of interp, process, vminterpChooses where the interpreter of the thread runs.
-guestone of linux, unikernellinuxChooses the guest of a thread of the level vm.
-filesone of host, rootrootGives a thread of the level process the files of the host or the root folder.
-writelist of absolute pathsnoneLists the only paths that a process thread and its programs can change.
-capslist of capability namesnoneGives the thread only the listed capabilities.
-computeinteger, 0 or morenoneCaps the compute units of the thread.
-memoryinteger, 1 or morenoneCaps the heap bytes of the thread.
-stackinteger, from 262144 to 67108864 (8388608 on WebAssembly)noneSets the native stack of the thread.
-metermeter namenoneCounts the thread in a meter of ztcl::meter.

-joinable

Makes a thread that thread::join can wait for.

Another thread can wait for the end of the thread with thread::join, which gives its exit status. A thread that is not joinable cannot be joined.

% set t [thread::create -joinable {thread::exit 3}]
> thread::join $t
3
# The id names no joinable thread.
% thread::join tid0x99
cannot join thread tid0x99
-errorcode NONE

-preserved

Starts the thread with a reference count of 1.

The thread starts with one reference, as after one thread::preserve, so one thread::release ends its thread::wait. With no option, the count starts at 0.

% set t [thread::create -preserved]
> set r [thread::preserve $t]
> thread::release $t
> thread::release $t
> set r
2

A flag has no value to check.

-isolation LEVEL

Chooses where the interpreter of the thread runs.

interp runs the thread in this process. process runs it in a child process of the ztcl runner, in the sandbox of the system: a crash or a bad C package in the child ends only the child. vm runs it in a guest of ztcl::vm and needs the capability vm. The levels wasm and vm-confidential give ZTCL ISOLATION LEVEL.

A thread of the level process works with thread::send, the errors, thread::cancel, thread::release, thread::exit, thread::join, tsv, and the mutexes, as a thread of the level interp does. Each tsv command and each lock of the child is a round trip to the parent. A thread that the child creates is not available in this version. Isolated threads has the sandbox of each system.

The level process needs the sandbox features of the system: on Linux, user namespaces, a delegated cgroup, and Landlock. A system without one gives ZTCL ISOLATION LEVEL with the name of the missing feature, so the examples of this page do not use the level.

ValueDescription
interpA thread in this process: the default.
processA thread in a child process, in the sandbox of the system.
vmA thread in a guest of ztcl::vm, with the capability vm.
% set t [thread::create -isolation interp {thread::wait}]
> set r [thread::send $t {expr {2 + 2}}]
> thread::release $t
> set r
4
% thread::create -isolation bogus {}
bad isolation level "bogus": must be interp, process, or vm
-errorcode NONE

% thread::create -isolation vm {}
the level vm needs the vm capability
-errorcode ZTCL ISOLATION LEVEL

% thread::create -isolation wasm {}
the isolation level wasm is not available yet
-errorcode ZTCL ISOLATION LEVEL

-guest GUEST

Chooses the guest of a thread of the level vm.

linux is a Linux guest with the runner as its first process. unikernel is the ztcl unikernel, which has no files, sockets, or processes. The option needs -isolation vm.

ValueDescription
linuxA Linux guest: the default.
unikernelThe ztcl unikernel.
% catch {thread::create -guest unikernel {}} m
> set m
-guest needs -isolation vm
% thread::create -guest bogus {}
bad guest "bogus": must be linux or unikernel
-errorcode NONE

% thread::create -guest unikernel {}
-guest needs -isolation vm
-errorcode NONE

-files MODE

Gives a thread of the level process the files of the host or the root folder.

With root, the child process sees the root folder of its creator. With host, it sees the files of the host, and the other parts of the sandbox stay: the child process, its limits, and its network and process limits. -files host needs -isolation process and a creator in host mode. A creator in host mode with the capability file or channels gets ZTCL ISOLATION HOSTFILES from -isolation process with no -files host.

ValueDescription
rootThe root folder of the creator: the default.
hostThe files of the host.
% set t [thread::create -joinable -files root {expr 1}]
> thread::join $t
0
% thread::create -files bogus {}
bad files mode "bogus": must be host or root
-errorcode NONE

% thread::create -files host {}
-files host needs -isolation process
-errorcode NONE

-write PATHS

Lists the only paths that a process thread and its programs can change.

For a thread of the level process with -files host and the capability exec: the thread and its programs can change only the files below the listed paths, on Linux and macOS. Each path must be absolute. The option needs -files host.

% catch {thread::create -write /tmp {}} m
> set m
-write needs -files host
% thread::create -write /tmp {}
-write needs -files host
-errorcode NONE

% thread::create -isolation process -write relative {}
bad write path "relative": must be an absolute path
-errorcode NONE

-caps LIST

Gives the thread only the listed capabilities.

The thread gets only the capabilities of LIST, and each one must be a capability of the creator. A thread with no thread in LIST has no thread::* and tsv::* commands. With no SCRIPT, such a thread still serves the messages of other threads, and it cannot send a script to another thread.

% set t [thread::create -caps {}]
> set r [thread::send $t {info commands thread::*}]
> thread::release $t
> set r

% set t [thread::create -caps thread]
> set r [thread::send $t {llength [info commands tsv::*]}]
> thread::release $t
> set r
27
% thread::create -caps bogus {}
unknown capability "bogus"
-errorcode THREAD CAPS

% thread::create -caps file {}
the creator has no capability "file"
-errorcode THREAD CAPS

-compute UNITS

Caps the compute units of the thread.

The compute limit of the thread is the smaller of UNITS and the compute limit of one thread of the group. A script of the thread that passes it stops with compute limit reached and the code THREAD STOPPED, and the thread ends. See the compute limit.

% set t [thread::create -compute 100000]
> set r [thread::send $t {expr {6 * 7}}]
> thread::release $t
> set r
42
% set t [thread::create -compute 1000]
> thread::send $t {while 1 {}}
compute limit reached
-errorcode THREAD STOPPED

% thread::create -compute -5 {}
bad compute limit "-5": must be 0 or more
-errorcode NONE

% thread::create -compute x {}
expected integer but got "x"
-errorcode TCL VALUE NUMBER

-memory BYTES

Caps the heap bytes of the thread.

The memory limit of the thread is the smallest of BYTES, the memory limit of one thread of the group (--thread-mem), and the memory limit of its creator, so a thread never gets more than the script that made it. It works at each isolation level. A script of the thread that passes it stops with the thread used too much memory: its limit is -memory of thread::create, or --thread-mem, and the thread ends. See the memory limit.

% set t [thread::create -memory 4000000]
> set r [thread::send $t {string length [string repeat x 1000000]}]
> thread::release $t
> set r
1000000
% set t [thread::create -memory 4000000]
> thread::send $t {set s x; while 1 {append s $s}}
the thread used too much memory: its limit is -memory of thread::create, or --thread-mem
-errorcode THREAD STOPPED

% thread::create -memory 0 {}
bad memory limit "0": must be 1 or more
-errorcode NONE

% thread::create -memory x {}
expected integer but got "x"
-errorcode TCL VALUE NUMBER

-stack BYTES

Sets the native stack of the thread.

The stack of the thread, for the level interp. The default is 8 MiB (8388608). The system commits a page of the stack at its first use, so a stack costs memory only for the depth that the scripts reach: a script of the tests of ztcl uses about 12 KB. The nested scripts of the thread can use half of the stack, at most 5 MiB; deeper, a script gets too many nested evaluations (infinite loop?), and the thread goes on. A smaller stack leaves more address space for many threads, on a 32-bit system and on the web.

% set t [thread::create -stack 262144]
> set r [thread::send $t {expr {6 * 7}}]
> thread::release $t
> set r
42
% set t [thread::create -stack 262144]
> thread::send $t {proc r {n} {r [expr {$n + 1}]}; r 0}
too many nested evaluations (infinite loop?)
-errorcode NONE

% thread::create -stack 1000 {}
bad stack size "1000": must be from 262144 to 67108864
-errorcode NONE

% thread::create -stack 262144 -isolation process {}
-stack needs -isolation interp
-errorcode NONE

-meter NAME

Counts the thread in a meter of ztcl::meter.

The thread counts its compute units, time, memory, and input and output in the meter NAME of ztcl::meter, and a NAME with no meter makes one. The option needs the capability meter. Without it, -meter is not an option: it is the script, and the next word is an extra argument.

% set t [thread::create -joinable -meter m {expr 1}]
> thread::join $t
> dict get [ztcl::meter get m] threads
interp 1 process 0

With the capability meter, each NAME is valid, and a NAME with no meter makes one.

Returns

The id of the new thread, such as tid0x2.

Examples

# The first thread after the main thread. It ends at the end of its script.
% thread::create {expr 1}
tid0x2

# The release ends the wait of the thread, so the thread ends.
% set t [thread::create {set ::n 1; thread::wait}]
> set r [thread::send $t {incr ::n}]
> thread::release $t
> set r
2

% set t [thread::create -- {thread::wait}]
> set r [thread::exists $t]
> thread::release $t
> set r
1

Errors

% thread::create a b
wrong # args: should be "thread::create ?-joinable? ?script?"
-errorcode TCL WRONGARGS

Facts

Capability
thread. 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