ucontext implementation featuring glibc-compatible ABI
Go to file
matoro be80075e95
ci/woodpecker/push/woodpecker Pipeline was successful Details
meson: support building docs
Toggleable with -Ddocs=(true|false), defaults to false.

Requires bumping minimum meson version to 0.59.0, or 0.61.0 to also
include symlinks.
2023-05-18 18:12:55 -07:00
arch Fixed empty TYPE(__proc) causing linker issues on 32-bit arm 2022-03-23 15:18:39 -07:00
doc meson: support building docs 2023-05-18 18:12:55 -07:00
examples add cooperative threading example 2020-12-07 18:15:18 -07:00
include/libucontext libucontext.h: add C++ externs 2023-05-18 18:11:50 -07:00
.gitignore test POSIX ABI compatibility in libucontext itself, too 2021-01-08 02:37:47 -07:00
.mailmap update copyright statements, add mailmap 2020-03-27 09:23:49 +00:00
.woodpecker.yml CI: also build armv7 2021-12-01 09:36:20 -06:00
LICENSE NEWS: prepare for 1.2 2022-02-13 06:38:00 +00:00
Makefile Fix FREESTANDING install target 2023-05-18 18:10:01 -07:00
NEWS NEWS: mention or1k port 2022-02-13 09:08:46 +00:00
README.md Added note to readme about floating point WIP 2022-03-04 10:41:54 -08:00
VERSION libucontext 1.2 2022-02-13 09:17:12 +00:00
libucontext.pc.in Makefile: Make variables Automake-compatible 2023-05-18 18:08:07 -07:00
libucontext_posix.c libucontext_posix: fix build under glibc 2020-12-11 11:12:47 +00:00
meson.build meson: support building docs 2023-05-18 18:12:55 -07:00
meson_options.txt meson: support building docs 2023-05-18 18:12:55 -07:00
test_libucontext.c test program: use libucontext-prefixed symbols 2020-12-06 03:32:09 -06:00
test_libucontext_posix.c add libucontext_posix test program 2020-12-09 01:40:12 +00:00



libucontext is a library which provides the ucontext.h C API. Unlike other implementations, it faithfully follows the kernel process ABI when doing context swaps.

Notably, when combined with gcompat, it provides a fully compatible implementation of the ucontext functions that are ABI compatible with glibc.

Since version 0.13, for some architectures, you can deploy to bare metal using newlib via the FREESTANDING=yes make option. Systems which use a syscall cannot work this way. The table below shows which architecture ports have been adapted to build with FREESTANDING=yes.

Adding support for new architectures is easy, but you need to know assembly language for the target to do it.

supported features

Architecture Works on musl Syscall Supports FREESTANDING Common trampoline


libucontext uses a simple makefile build system. You should define ARCH= at build time, otherwise the build system will attempt to guess using uname -m.

$ make ARCH=x86_64
$ make ARCH=x86_64 check
$ make ARCH=x86_64 DESTDIR=out install

There are a few options:

  • ARCH: The architecture libucontext is being built for. Must be set to one of the architectures listed in the feature support table. If unset, the build system will attempt to guess based on what architecture the host is running. Setting this option explicitly is highly recommended.

  • FREESTANDING: If this is set to yes, the system ucontext.h headers will not be used. Instead, the headers in arch/${ARCH}/freestanding will be used for definitions where appropriate. Default is no.

  • EXPORT_UNPREFIXED: If this is set to yes, the POSIX 2004 names getcontext, setcontext, swapcontext and makecontext will be provided as weak symbols aliased against their libucontext_ namespaced equivalents. This is necessary for libucontext to provide these functions on musl systems, but you may wish to disable this when using FREESTANDING mode to avoid conflicts with the target's libc. Default is yes.

  • DESTDIR: If this variable is set, the installed files will be installed to the specified path instead of the system root.

If you have scdoc installed, you can build manpages and install them:

$ make docs
$ make DESTDIR=out install_docs

real-world use cases

libucontext is used on almost all musl distributions to provide the legacy ucontext.h API. Additionally, it is used by:

  • UTM -- friendly qemu distribution for macOS and iOS devices. UTM uses libucontext as qemu's coroutine backend.

  • Lwan -- a high-performance embeddable asynchronous web server. Lwan uses libucontext to provide green threads when building on non-x86 architectures.


libucontext, while largely functionally equivalent does have some differences over traditional POSIX ucontext functions:

  • Saving and restoring the signal mask is not implemented by default in order to avoid kernel syscall overhead. Use -lucontext_posix if you actually need this functionality, which provides a POSIX compliant implementation at the cost of performance.

  • Only basic GPR registers are saved and restored when context swapping. The glibc implementation uses hardware capability detection to save/restore other register groups, such as the FPU registers or vector processing (AltiVec/AVX/NEON) registers. Adding this capability detection would significantly increase the complexity of the project and thus is not implemented. Support for compiling in code to save/restore FPU registers or vector registers may be added in a later release as a build-time setting -- for now, we assume a soft-float ABI with no optional processor features. In practice, this does not really matter, code using these functions are unlikely to be impacted by this design assumption. This is a work in progress, as newer compilers will spill even non-floating-point state through floating point registers when allowed to do so.