jhrutgers / zth

Cross-platform cooperative multitasking (fiber) framework.
https://jhrutgers.github.io/zth/
Mozilla Public License 2.0
9 stars 2 forks source link
async cmake cpp cross-platform embedded fibers fsm multitasking zeromq

CI

Zth (libzth) - Zeta threads

This library provides user-space cooperative multitasking, also known as fibers. One can see fibers as threads, but with the exception that you explicitly have to indicate when the fiber is allowed to switch to another fiber. As a result, locking, synchronization, using shared data structures between fibers is way easier than when using threads. See also https://en.wikipedia.org/wiki/Fiber_(computer_science).

The main benefits of Zth are:

Working with fibers is very easy. The examples/1_helloworld example starts two fibers by using the async keyword:

#include <zth>
#include <cstdio>

void world()
{
    printf("World!!1\n");
}
zth_fiber(world)

void hello()
{
    async world();
    printf("Hello\n");
}
zth_fiber(hello)

int main_fiber(int argc, char** argv)
{
    async hello();
    return 0;
}

Notable other features include:

Check out the examples for more details, including the full explanation of the example above.

Supported platforms are:

Check out the dist directory for example targets.

How to build

To install all build dependencies, run dist/<platform>/bootstrap (as Administrator under Windows). Next, run dist/<platform>/build to build the library and all examples. This does effectively:

mkdir build
cd build
cmake ../../..
cmake --build .

By default, release builds are generated. To do debug builds, pass Debug as command line argument to dist/<platform>/build, or do something like:

cmake ../../.. -D CMAKE_BUILD_TYPE=Debug

After building, check out the doxygen/html directory for documentation.

How to integrate in your project

Include the Zth top-level CMakeLists.txt in your project, and link to libzth. Configure Zth options as required, like this:

set(ZTH_HAVE_LIBZMQ OFF CACHE BOOL "Disable ZMQ" FORCE)
set(ZTH_THREADS OFF CACHE BOOL "Disable threads" FORCE)
set(ZTH_BUILD_EXAMPLES OFF CACHE BOOL "Disable Zth examples" FORCE)
set(ZTH_TESTS OFF CACHE BOOL "Disable Zth tests" FORCE)

add_subdirectory(zth)

# Override search path to your own zth_config.h.
target_include_directories(libzth BEFORE PUBLIC ${CMAKE_SOURCE_DIR}/include)

This also works for cross-compiling. Refer to dist/qemu-arm-a15/README-ARM.md for some hints when compiling for bare-metal ARM.

Note that zth_config.h can be provided by the user to optimize the configuration for a specific target. However, its include path must be available while compiling libzth itself. After installing the library, the configuration cannot be changed.

How to run

Zth checks for environment variables, which are listed below. The environment is (usually) only checked once, so dynamically changing the variables after startup has no effect to Zth's behavior.

Related

A predecessor project was called Xi, as the Greek capital symbol suggests parallel threads. In this project, preemptive multitasking is implemented. In the same context, the Z(eta) symbol suggests that threads are not parallel, but they explicitly yield from one to another.

GNU Pth has been a great inspiration for this library.

License

This project is licensed under the terms of the Mozilla Public License, v. 2.0, as specified in LICENSE.