WebAssembly/Threads

From Lazarus wiki
Revision as of 00:32, 28 May 2022 by Nickysn (talk | contribs) (→‎Atomic instructions: - there's now a new compiler option to enable WASM threads: -CTwasmthreads, it should be used to compile the RTL, instead of -dFPC_WASM_THREADS)
Jump to navigationJump to search

Thread support

This page contains some collected informations on the features needed for thread support in WebAssembly (in the browser).

Thread support consists of 2 parts:

  • Atomic instructions.
  • Actually starting a thread.


Atomic instructions

The proposed specs 

When the Free Pascal RTL is compiled with -CTwasmthreads, the following RTL functions will use the new atomic instructions and thus should be thread safe in a multithreaded environment:

InterlockedDecrement
InterlockedIncrement
InterlockedExchange
InterlockedCompareExchange
InterlockedExchangeAdd

Note that these require proper alignment (4 bytes) of the target, otherwise they trap (i.e. terminate the program with a stack trace).

Thread support

Webassembly relies on the hosting environment to actually start threads.

Some extra info: