Coder Social home page Coder Social logo

jhrutgers / zth Goto Github PK

View Code? Open in Web Editor NEW
9.0 1.0 2.0 8.15 MB

Cross-platform cooperative multitasking (fiber) framework.

Home Page: https://jhrutgers.github.io/zth/

License: Mozilla Public License 2.0

CMake 4.44% C++ 94.86% C 0.70%
fibers multitasking cpp cmake async cross-platform fsm embedded zeromq

zth's Introduction

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:

  • Cross-platform (Linux/Windows/macOS/bare metal) library for cross-platform application development.
  • No dependencies, other than a C library (usually newlib for embedded targets), and clock_gettime(). When available, ZeroMQ is supported and made fiber-aware, which can be used for inter-thread or inter-process communication.
  • The configuration can be tailored towards your target, such as fiber stack size, time slices, debug/VCD output.
  • Usage of dynamic memory is minimized (only during fiber creation and cleanup), and it allocator-aware.
  • Support for ASan, LSan, UBSan, and Valgrind.

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:

  • Finite State Machine implementation, which allows defining state transitions in an eDSL, and easily transfers a fiber into a fiber-aware FSM, properly handling polling timed guards.
  • Extensible Poller framework, which allows poll()-like calls by fibers to be forwarded to a single poller server, which can be user-defined for custom pollable types.
  • Inter-fiber signaling, such as a mutex, semaphore, signal, and future.

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

Supported platforms are:

  • Ubuntu/macOS/Windows 32/64-bit
  • bare-metal 32-bit ARM (with newlib)
  • gcc 4 or newer
  • C++98 or newer, C99 or newer. However, compiling for later C++ standards adds features and improves performance.

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.

  • ZTH_CONFIG_ENABLE_DEBUG_PRINT
    When set to 1, debug prints are enabled. Disabled by default. For non-debug builds, all debug prints are removed from the binary, so they cannot be enabled in that case.

  • ZTH_CONFIG_DO_PERF_EVENT
    When set to 1, perf VCD file generation is enabeled. Disabled by default.

  • ZTH_CONFIG_DO_PERF_SYSCALL
    When set to 1, the perf VCD logs will contain all calls to Zth's special functions. Enabled by default.

  • ZTH_CONFIG_CHECK_TIMESLICE_OVERRUN When set to 1, check at every context switch if the timeslice was overrun significantly. Only the longest overrun is reported. Enabled by default in the debug build, not available in release builds.

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.

zth's People

Contributors

jhrutgers avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

zth's Issues

Name change request

Currently, the way to start a new fiber, is by using the keyword async. This might be somewhat confusing for people coming from C# or Python (like me).

I'd like to suggest renaming async to spawn, so that it is clear a fiber is spawned at that point in the code.

Another thing which is available in python and C# is the await keyword, this could be useful to await the returned futures. Maybe a macro for this would be a nice syntactic sugar.

Support for libuv

A cool addition might be support for libuv using a configure option.

Libuv library: https://github.com/libuv/libuv

Benefits of using libuv: expose all of the libuv API in a cross platform way, so you have sockets and filesystem IO asynchronously.

Allow building using Ninja and with ZTH_HAVE_LIBZMQ=OFF

When building with cmake -G Ninja .. I ran into some troubles, also building with ZTH_HAVE_LIBZMQ=OFF results in some build errors.

What would you like me to do? Submit a pull request for this? Add a CI job for this specific configuration?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.