Faasm and Threading

Serverless applications benefit most from the parallelism of many small functionsdistributed across hosts. Spawning one or two threads to do background work stillmakes sense, but using threading and shared memory to parallelise a larger applicationis not necessarily effective in a serverless context.

To support larger multi-threaded applications, Faasm can transparently distributethreads across multiple serverless functions, thus allowing them to take advantageof serverless parallelism.

This functionality is accessed in C/C++ through subsets of pthreads and OpenMP.Faasm intercepts normal threading calls and chains a new serverless function to executethe thread, potentially on another host. Shared data is managed withshared state and the process memory is migrated across hosts withproto-functions.

pthreads

Faasm supports the following pthread functions. Although all pthread_attr_XXX callsare supported, the attributes themselves may be ignored in a Faasm context.

FunctionDescription
int pthread_create(…)Spawn a new thread
int pthread_join(…)Await thread completion
void pthread_exit(…)Exit the current thread
void pthread_attr_XXXAll attr-related calls

OpenMP

Faasm OpenMP support has separate docs.

Threading modes

Faasm has two threading modes, local and chain, which are configured with theTHREAD_MODE environment variable. In local mode, threads are spawned locally,operating on the same Wasm module memory as per theWasm threading proposal and usingWAVM's underlying implementation.

In chain mode, Faasm spawns all new threads as chained function calls, whichmay or may not execute on the same host.

Migrating threads across hosts

Threads are migrated across hosts using a version of proto-functions,which duplicate a function's memory and execution state on another host. This providesthe function on the other host with a copy of the heap, stack and data from its parentfunction, thus letting it continue thread-like execution and read any shared data.

Writes to global variables and shared memory are not propagated across distributedthreads automatically, and must be handled explicitly with Faasm's shared state.

An example of a distributed threaded application can be found in the examples.