commit | ec288e222255d34f878ed1ab5efb387112680c73 | [log] [tgz] |
---|---|---|
author | Ben Clayton <bclayton@google.com> | Sun Feb 09 19:20:01 2020 +0000 |
committer | Ben Clayton <bclayton@google.com> | Sun Feb 09 19:20:01 2020 +0000 |
tree | f5aae4ddaacfb037cf30898259318f3aae689d43 | |
parent | 6dd9ff1eb3ef1b677ba37d2d011a61b237d913d2 [diff] |
Squashed 'third_party/marl/' changes from 14e4d862a..246091e81 246091e81 Implement support for MIPS64 2b3e3fe6d Core: Rework fiber scheduling to fix broken behavior. a10646182 Add tool to diff benchmarks. 2c9624c1b Kokoro: Print test output to logs. 36208b483 Use SAL annotations more aggressively in the scheduler. 492edc979 Add benchmarks using google benchmark 32102554d Add new 'hello task' example, add to README.md 9b860935b CMake: Make the marl target's include directory public 8cdb1b1f0 CMakeLists.txt: Make it easier to specify third_party vars. e25626a8e CMakeLists: Don't repeatedly include MARL_OS_LIBS git-subtree-dir: third_party/marl git-subtree-split: 246091e814658337b22f212c97d0659ea3673c9f
Marl is a hybrid thread / fiber task scheduler written in C++ 11.
Marl is a C++ 11 library that provides a fluent interface for running tasks across a number of threads.
Marl uses a combination of fibers and threads to allow efficient execution of tasks that can block, while keeping a fixed number of hardware threads.
Marl supports Windows, macOS, Linux, Fuchsia and Android (arm, aarch64, mips64, ppc64 (ELFv2), x86 and x64).
Marl has no dependencies on other libraries (with an exception on googletest for building the optional unit tests).
Example:
#include "marl/defer.h" #include "marl/event.h" #include "marl/scheduler.h" #include <cstdio> int main() { // Create a marl scheduler using the 4 hardware threads. // Bind this scheduler to the main thread so we can call marl::schedule() marl::Scheduler scheduler; scheduler.bind(); scheduler.setWorkerThreadCount(4); defer(scheduler.unbind()); // Automatically unbind before returning. // Create an event that automatically resets itself. marl::Event sayHellow(marl::Event::Mode::Auto); marl::Event saidHellow(marl::Event::Mode::Auto); // Schedule some tasks to run asynchronously. for (int i = 0; i < 10; i++) { // Each task will run on one of the 4 worker threads. marl::schedule([=] { // All marl primitives are capture-by-value. printf("Task %d waiting to say hello!\n", i); // Blocking in a task? // The scheduler will find something else for this thread to do. sayHellow.wait(); printf("Hello from task %d!\n", i); saidHellow.signal(); }); } // Unblock the tasks one by one. for (int i = 0; i < 10; i++) { sayHellow.signal(); saidHellow.wait(); } // All tasks are guaranteed to completed before the scheduler is destructed. }
Marl contains many unit tests and examples that can be built using CMake.
Unit tests require fetching the googletest
external project, which can be done by typing the following in your terminal:
cd <path-to-marl> git submodule update --init
To build the unit tests and examples, type the following in your terminal:
cd <path-to-marl> mkdir build cd build cmake .. -DMARL_BUILD_EXAMPLES=1 -DMARL_BUILD_TESTS=1 make
The resulting binaries will be found in <path-to-marl>/build
Marl can be built using Visual Studio 2019's CMake integration.
You can build and link Marl using add_subdirectory()
in your project's CMakeLists.txt
file:
set(MARL_DIR <path-to-marl>) # example <path-to-marl>: "${CMAKE_CURRENT_SOURCE_DIR}/third_party/marl" add_subdirectory(${MARL_DIR})
This will define the marl
library target, which you can pass to target_link_libraries()
:
target_link_libraries(<target> marl) # replace <target> with the name of your project's target
You may also wish to specify your own paths to the third party libraries used by marl
. You can do this by setting any of the following variables before the call to add_subdirectory()
:
set(MARL_THIRD_PARTY_DIR <third-party-root-directory>) # defaults to ${MARL_DIR}/third_party set(MARL_GOOGLETEST_DIR <path-to-googletest>) # defaults to ${MARL_THIRD_PARTY_DIR}/googletest add_subdirectory(${MARL_DIR})
Note: This is not an officially supported Google product