Threads
The capability thread adds the commands of the Tcl Thread package: thread::*, tsv::*, tpool::*, and the mutexes and conditions of thread::mutex, thread::rwmutex, and thread::cond. The embedder turns it on and sets its limits. This page has the model, the limits, and the differences from the Thread package. References lists the manual pages that the commands follow.
Example
set ids {}
for {set i 0} {$i < 4} {incr i} {
lappend ids [thread::create -joinable [format {
set sum 0
for {set k 0} {$k < 1000000} {incr k} { incr sum %d }
tsv::incr totals all $sum
} $i]]
}
foreach id $ids { thread::join $id }
puts [tsv::get totals all]
$ ztcl --allow thread threads.tcl
6000000
Speed
Each benchmark of bench/ is as fast as tclsh 8.6.16 with Thread 2.8.11, or faster. Wall time in milliseconds, best of 5 runs, on a 4-core Intel Core i7-6700K. Performance has the method.
| Benchmark | ztcl | tclsh 8.6.16 |
|---|---|---|
thread_map.tcl: 1,000,000 integers in 8 parts |
99 | 305 |
thread_pipe.tcl: 4 stages, 100,000 messages |
605 | 706 |
thread_tsv.tcl: 8 threads, 100,000 tsv::incr each, 8 elements |
1214 | 3276 |
thread_tpool.tcl: 8 workers, 100,000 jobs |
466 | 639 |
The model
- Each thread has one interpreter, and the threads share no interpreter data. Each thread has its own heap, collector, JIT state, event loop, channels, and child interpreters.
- No value crosses threads by pointer. A message, a result, and each value of
tsvandtpoolcross as a deep copy. - The host interpreter is the main thread of a group. Each thread that a script makes joins the group of its creator. The group has the registry of the threads, the shared variables, the pools, the mutexes, and the conditions. Two hosts have two groups, and a script of one group cannot name a thread of the other.
- A thread id has the form
tid0xand a hex counter, for exampletid0x2. The id has no address.
The capability and the limits
Interp.allow with Capability.thread turns on the commands. The runner flag is --allow thread. The embedder sets Interp.thread_limits before the call:
| Field | Default | What it limits |
|---|---|---|
max_threads |
8 | The threads that run at one time, the workers of the pools included. The main thread does not count. |
compute_limit |
the compute limit of the host | The compute units of one thread. |
mem_limit |
256 MiB | The bytes of the heap of one thread. |
total_compute |
no limit | The compute units of all threads together. |
total_mem |
1 GiB | The bytes of all threads together, with the shared variables, the queues, and the JIT data of the threads. |
max_queue |
10,000 | The messages that wait in the queue of one thread. |
- A new thread gets its compute limit from
total_computeat its start, and the compute units that it did not use go back at its end. So the sum of the compute units of the threads is at mosttotal_compute. - The allocator of a thread adds each byte to an atomic count of the group. When the count passes
total_mem, the allocation fails, and the thread stops with an error. - A thread that passes a limit stops alone. The other threads and the host keep running. The error goes to the joiner, to the procedure of
thread::errorproc, or to the output. - A new thread has the capabilities of its creator, the JIT options of its creator, and the collector settings of its creator (
gc_onand the stress mode). A child interpreter of a thread has nothreadcapability. - The allocator of the embedder must be safe for threads. The output callback runs under a lock of the group, so the callback needs no lock of its own.
With the capability on, a script is not deterministic: the order of the events of two threads depends on the system.
Messages
A message is a script, a result, or a job. The sender copies each value into a neutral form in the memory of the group. The form keeps the kind of a value: an integer, a double, a string, or a list of such values. So a list of numbers stays a list of numbers, and the receiver does no parse. The receiver copies the neutral form into its own heap on its own thread. Only the owner thread of a heap allocates from it.
A list deeper than 200 levels crosses as its string form. The copy then needs no deep recursion.
ztcl has no frozen, read-only form of a value that two threads share, so a large message costs one copy at each end.
The thread commands
| Command | ztcl behavior |
|---|---|
thread::create ?-joinable? ?-preserved? ?-isolation LEVEL? ?-caps LIST? ?-compute UNITS? ?-meter NAME? ?script? |
Starts a thread and waits until its interpreter exists. With no script, the thread runs thread::wait. Without thread::wait, the thread ends at the end of its script. -compute caps the compute units of the thread below the limit of the group. -meter counts the thread in a meter of ztcl::meter, with the capability meter. -caps gives the thread only the listed capabilities, each one a capability of its creator. A thread with no thread in its list has no thread::* and tsv::* commands, also at the level process: it gets messages and answers them, and it cannot send a script to another thread. |
thread::send ?-async? ?-head? id script ?varname? |
Queues the script at the target, or at the head with -head. Without -async, the sender waits for the result. While it waits, it runs the scripts that other threads send to it, so two threads that send to each other do not deadlock. With varname, the result goes to the variable, and the command gives the return code. With -async and varname, the variable gets the result later, in the event loop of the sender. A send to the own thread runs the script at once. |
thread::broadcast script |
Sends the script with -async to each other thread. |
thread::wait |
Runs the event loop of the thread until a release ends it. |
thread::preserve ?id?, thread::release ?-wait? ?id? |
Change the reference count. At zero or below, thread::wait returns and the thread ends. -wait waits for the end. |
thread::join id |
Waits for the end of a joinable thread and gives its exit status. |
thread::id, thread::names, thread::exists id |
The own id, the ids of the running threads, and a test for one id. |
thread::exit ?status? |
Ends the thread. catch cannot catch it. The main thread gives an error. |
thread::cancel ?-unwind? id ?result? |
Stops the script of a thread at its next safe point. A cancel of a thread that runs no script has no effect, as in the Thread package. -unwind gives an error that catch cannot catch. The main thread cannot be the target. |
thread::unwind |
The same as thread::release for the own thread. |
thread::errorproc ?proc? |
Sets the procedure that gets the errors of all threads: proc id errorInfo. It runs in the thread that set it. |
thread::configure id ?option? ?value? |
-eventmark N makes thread::send -async wait while the queue of the target has N messages. -unwindonerror bool ends the thread after a send with an error. |
thread::eval ?-lock mutex? arg ?arg ...? |
Runs the script while it holds the mutex, or a mutex of the group. |
thread::transfer id channel, thread::detach channel, thread::attach channel |
Move a file or socket channel between threads. A server socket and a command pipeline stay with their thread. The move removes the fileevent handlers of the channel. The channel commands exist on Linux, macOS, and Windows. |
The shared variables
tsv keeps arrays of the group. Each array has its own lock, so two threads that use two arrays do not wait for each other. The table of the arrays has a lock of its own, which a command holds only to find or make an array.
- A value of an element is in the neutral form. A list keeps its elements as a list, so
tsv::lappend,tsv::lpushat the end, andtsv::lpopat the end take amortized constant time with no parse. - The lock of an array is recursive for its thread.
tsv::lock array scriptholds it while the script runs, and eachtsvcommand of the script on that array runs in the same lock. The lock goes free when the script ends, also with an error. tsv::object array elementgives a command that runs eachtsvcommand on that element.- The commands are
set,get,unset,exists,pop,move,names,incr,append,lappend,linsert,lreplace,llength,lindex,lrange,lsearch,lset,lpop,lpush,array set|get|names|size|reset,lock,object,handlers, and the keyed listskeylset,keylget,keylkeys, andkeyldel.array bindand the persistent storage handlers are absent.
The thread pools
tpool::create makes a pool of worker threads. A worker is a thread of the group with its own interpreter. It runs -initcmd one time and then keeps its interpreter between jobs, so a proc of -initcmd compiles one time for each worker.
tpool::postqueues a job and starts a worker when no worker is idle and the pool has fewer than-maxworkers.-nowaitqueues the job and starts no worker when one exists.-detachedkeeps no result.tpool::waitwaits until one job of the list is done and gives the done jobs.tpool::cancelremoves jobs that did not start.tpool::getgives the result or the error of a done job and forgets the job.- A worker with no job for
-idletimeseconds ends, down to-minworkersworkers. It runs-exitcmdbefore its end. tpool::suspendstops the start of new jobs, andtpool::resumestarts them again.tpool::preserveandtpool::releasecount the references. At zero, the pool ends its workers.- The workers count in
max_threads. A post that needs a worker when no thread is free waits for a free worker of the pool. With no worker at all, it gives an error.
The mutexes and the conditions
thread::mutex, thread::rwmutex, and thread::cond give handles of the group (mid0, rwmid0, cid0). A handle is a name in a table of the group, so a bad name is an error and never a bad pointer.
- An exclusive mutex gives an error when its thread locks it again.
-recursiveallows that. - A lock and a condition wait in slices of 50 ms. After each slice, the thread checks its stop flag. So a cancel, a teardown, or the end of the group stops a thread that waits.
- A mutex that a thread holds at its end goes free. Another thread then gets it.
destroyof a mutex that a thread holds, or of a condition that a thread waits on, is an error.
The event loop
The queue of a thread is a source of the event loop. vwait, update, and thread::wait run the scripts of the queue. A thread without the event capability has thread::wait and runs only its queue.
- A wait with no timer and no channel blocks on the condition of the queue. In the main thread,
vwaitblocks only when another thread exists. Otherwise it gives the error of Tcl:would wait forever. - A wait with channels polls the descriptors in slices of 10 ms and checks the queue between the slices.
Stops, cancels, and teardown
A thread checks a stop flag at its safe points. Another thread sets the flag and then writes zero to mem_limit of the target interpreter. Each safe point of the interpreter and of the JIT code already compares the memory count with mem_limit, so the next safe point calls Interp.memPoint. There the target restores its limit and reads the flag. The fast path of the interpreter and of the JIT has no new check.
- The flag is atomic. The write of
mem_limitis an aligned word that only the target restores. The other thread sets the flag and stores the zero under the lock of the worker. The target reads the flag and restores the limit under the same lock. So a cancel is never lost, and no zero store comes after a restore.Interp.overLimitcompares a thread interpreter with the limit of its worker, so a memory check between the two steps gives no false error. - A thread that runs in a child interpreter sees the cancel when the call into the child returns. The compute limit of the thread bounds that call.
- A thread deletes its own interpreter, the JIT code included, on its own thread after its script ends. So no JIT code of a thread runs when its interpreter goes.
- When the host ends (
deinitorreset), the group sets the stop flag of each thread, wakes each wait, and joins each thread. A thread with no compute limit in a loop with no safe point can delay that. Set a compute limit for untrusted scripts.
Isolated threads
Isolated threads compares the levels and the sandbox of each system, with a review list for the code of a parent. thread::create -isolation process runs the thread in a child process (ztcl#209). The child is the ztcl runner with one interpreter, and it talks to the parent over the protocol of docs/dev/isolation.md: sealed frames on a socket pair. A crash, a memory error, or a bad C package in the child ends only the child.
set t [thread::create -isolation process {proc sq x {expr {$x * $x}}; thread::wait}]
puts [thread::send $t {sq 12}] ;# 144, from another process
thread::send,-asyncwith a variable, a send of the child to another thread, the errors with their codes,thread::cancel,thread::release,thread::exit,thread::join,thread::names, andthread::errorprocwork as for a thread of the levelinterp. The values keep their kinds.- The child gets the capabilities, the compute limit, and the memory limit of the thread, and the JIT when the creator has it on and the sandbox allows it. The clock and the event loop of the child are those of the runner, when the creator has them.
- The files of the child are the root folder of the creator. A creator in host mode (the default of the
ztclprogram) with thefileor channels capability getsZTCL ISOLATION HOSTFILESfromthread::create -isolation process: "give the thread a root folder, or pass -files host". Give the creator a root folder (ztcl --root DIR), or pass-files hostto give the child the files of the host, with the other parts of the isolation in place: the child process, its limits, and its network and process limits.-files hostfrom a creator with a root folder is an error, and so is-files hostwith no-isolation process.-files rootis the default. - A child that ends with no exit, for example a kill from outside, ends the thread: a send that waits gives
target thread died,thread::joingives 1, andthread::errorprocgets the error. - At the end of the group, a child that does not stop within 2 seconds gets SIGKILL.
- The runner starts the child (
Interp.iso_runner, which the runner sets to itself). An embedder that is not the runner setsiso_runnerto a ztcl runner, or getsZTCL ISOLATION RUNNER. - On macOS the child enters a sandbox (
sandbox_init) before its first script. Its files are the root folder of the creator, all files of the user with-files host, or none without thefilecapability. Its network needs thesocketcapability. It cannot start a program or reach a Mach service. The parent ends a child whose physical footprint passes its memory limit by more than 128 MiB, the margin of the runner itself. - On Windows the child starts in an AppContainer and a Job Object. Its files are the root folder of the creator, or none without the
filecapability. Its network needs thesocketcapability. The job allows it one process, soexecfails, and ends it past its memory limit plus 64 MiB for the runner. - On Windows with
-files host, the child runs with no AppContainer, because an AppContainer reaches no folder of the user. It keeps the Job Object (one process, the memory limit, a kill at the end of the group) and gets the token of the user with no privilege and with the group of the administrators for deny only. It gives up the limit of the network of the system: with thesocketcapability it reaches each address that the user can, and without it the interpreter still has no socket. - On Linux the parent starts the child in new user, PID, mount, IPC, UTS, and cgroup namespaces, and in a new network namespace without the
socketcapability. The mount namespace has a new root with only a new/proc, the root folder of the creator at its real path, the zone data withclock, and/etc/hostsand/etc/resolv.confwithsocket. The child is in a cgroup v2 group of its own under the delegated cgroup of the user (ztcl-iso/), withmemory.maxat its memory limit plus 64 MiB,pids.max32, andcpu.maxat one CPU. Before its first script it adds Landlock rules for the same files and a seccomp filter: it cannot start a program, mount a file system, or open a socket withoutsocket. A kernel or a system with no unprivileged user namespaces, no mounts in them (the AppArmor limit of Ubuntu), no delegated cgroup with the cpu, memory, and pids controllers, or no Landlock givesZTCL ISOLATION LEVELwith the name of the missing feature. - In this version the level runs on Linux, macOS, and Windows. A thread that the child creates is not yet available in the child: it gives an error there.
- On Linux, macOS, and Windows,
thread::transfermoves a file or a socket between the processes: from the parent to an isolated thread, back, and from one isolated thread to another through the parent. The descriptor and its buffered input move, and the channel keeps its name: the channels of a child have names in a range of their own. A channel in memory, a terminal, a server socket, and a command pipeline stay. A channel whosethread::attachfails in the other process stays detached there and closes at the end of the group. On Windows a file, a pipe, or a console moves withDuplicateHandle, and a socket to a child moves withWSADuplicateSocketW. The parent pulls a handle out of a child, which cannot open its parent. tsv, the mutexes, the conditions, andthread::evalwork in the child on the arrays and the locks of the group. The parent runs each of these commands for the child (the broker), so each one costs a round trip between the processes.tsv::lockandthread::evalrun their script in the child and hold the lock in the parent. A lock that the child holds at its end goes free. The levelswasm,vm, andvm-confidentialgiveZTCL ISOLATION LEVEL.
A send from the child to the parent runs in the parent with the power of the parent, as with any thread. The sandbox limits the child, and a script the child sends to the parent goes past it.
Compiled code
A proc goes to a thread as its text in a script. Each thread compiles its own bytecode and its own JIT code.
The bytecode compiler inlines core commands such as set and incr. A command of the same name in another namespace stops the inline forms in the whole interpreter, because a call in that namespace finds that command first. The commands of tsv (tsv::set, tsv::incr, and the others) do not stop them. The cost of that rule: compiled code in the namespace tsv, for example a proc tsv::helper that calls set, runs the core set and not tsv::set. Tcl runs tsv::set there. Code outside the namespace tsv gets the same command as in Tcl. Without this rule, the commands of tsv stop the inline forms in each interpreter with the capability.
Platforms
The commands use std.Thread, so they build for Linux, macOS, and Windows. The channel commands of thread::transfer, thread::detach, and thread::attach need the channel capability, which exists on Linux, macOS, and Windows. The default WebAssembly build (ztcl.wasm) has no threads. zig build wasm-threads makes two WebAssembly builds with threads. See "WebAssembly" below. ThreadSanitizer runs on Linux: tools/tsan.sh.
WebAssembly
Each thread is a Web Worker, or a worker_threads worker in Node.js, with its own instance of the module. web/ztcl-threads.mjs is the host. WebAssembly has the API.
- On a cross-origin isolated page and in Node.js, the host uses
ztcl-threads.wasm. All instances use one shared memory, so the group, the queues,tsv, the mutexes, the conditions, the stop flags, and the totals are in aSharedArrayBuffer. The code of the native platforms runs there.src/webthread.ziggives it the parts ofstd.Threadandstd.Io: a thread start through the host, a futex onmemory.atomic.wait32, a lock, and an allocator with a lock. The main interpreter also runs in a worker, because a browser does not allowAtomics.waiton its main thread. The output of the threads goes to the main worker, which sends it to the host every 50 ms. - Without cross-origin isolation, the host uses
ztcl-async.wasm(src/webasync.zig). Each instance has its own memory. A message is a script, and the reply is the result in a byte form that keeps the kind of each value. Only the commands that do not wait work. Each command that waits gives an error that names the missing isolation. Only the limits of one thread apply, and a stop isWorker.terminate(). - GitHub Pages cannot set the headers COOP and COEP. A service worker can add them on a static site. Embedding has the steps.
Differences from the Thread package
- No value is shared by pointer, and
tsvvalues keep their list and number forms. - Each thread has limits, and a group has totals.
- The
ttracepackage,tsv::array bind, and the-eventmarkwait ofthread::broadcastare absent. thread::cancelreaches a thread in a child interpreter only when the child returns.