Skip to content

a lightweight userspace thread library with non-blocking IO for C++

License

Notifications You must be signed in to change notification settings

xush6528/uthread

 
 

Repository files navigation

README

uthread is a lightweight fiber/user threading library I wrote for fun as a learning experience. It's far from "complete" but includes the following core features...

  • Cooperative scheduling with fast context switching (~30M thread context switches/second on a single core)
  • Synchronization primitives like mutexes, condition variables, etc. for use between user threads
  • Asynchronous IO notifications for timers, sockets, etc. courtesy of libevent

Some missing features which may come in the future...

  • Guard pages for stack overflow protection

Design

uthread is designed around an N:1 (user-thread:kernel-thread) mapping model. An executor is responsible for multiplexing a set of user threads on top of a kernel thread. See executor.hpp for more detailed info. The Executor::sleep(...) and Executor::ready(...) functions are especially important for being the basis of the provided synchronization primitives and IO notification system.

Once you have a high level understanding of the executor take a look at the Mutex as an example of using the Executor::sleep(...) and Executor::ready(...) functions. The IO system works in a similar fassion and relies on libevent as a backend. You can read the libevent online book to learn more.

Examples

You can run example <X> via GLOG_logtostderr=1 ./<X> after building.

Building

A Vagrant VM is provided with a complete development environment. You should first bootup the VM from the vagrant directory...

vagrant up && vagrant ssh

Once the VM is up you can perform a build...

cd /uthread                           && \
./cmake.sh -DCMAKE_BUILD_TYPE=Release && \
cd build                              && \
make -j8                              && \
make tests                            && \
make bench

Dependencies

Reading

The following are some good links regarding memory models, compiler/hardware instruction reordering, etc that are important to understand in concurrent environments. uthread multiplexes user threads on top of a single kernel thread so most of this is not immediately relevant but useful to understanding what kind of compiler optimizations might occur around context switches. Despite thinking I understand this stuff, I wouldn't be surprised if uthreads has some bugs with regards to these ideas :)

About

a lightweight userspace thread library with non-blocking IO for C++

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C++ 61.7%
  • CMake 27.6%
  • Shell 5.9%
  • Assembly 2.1%
  • Python 1.9%
  • Ruby 0.8%