Squashed 'third_party/marl/' changes from 246091e81..a047dd0bb

a047dd0bb Include benchmarks on project's README.md
4c702da52 Scheduler: Fix lock state on Fiber::wait timeout.
b4e305525 Docs: Add documentation for marl::Scheduler.
5f18ac0e0 ConditionVariable: Delete copy and move constructors
f78eb441f Scheduler: document requirement to unbind() before destruction
9f9f6d32e Defer benchmark - avoid benchmark::DoNotOptimize()
3b610e902 Fix compiler warnings with MARL_FIBERS_USE_UCONTEXT
0dbab1184 Scheduler: Delete copy and move constructors / assignment ops.
cbef55d58 Kokoro: Build benchmarks
e923c3d96 Rework the 'hello task' example to be more idiomatic

git-subtree-dir: third_party/marl
git-subtree-split: a047dd0bbbd6a65ee4d03d0ceb4fedfa56da02a5
18 files changed
tree: aa0a3416fe99d84ddd3d49d9486d12c560f15a30
  1. .clang-format
  2. .gitignore
  3. .gitmodules
  4. AUTHORS
  5. BUILD.bazel
  6. CMakeLists.txt
  7. CONTRIBUTING.md
  8. LICENSE
  9. README.md
  10. WORKSPACE
  11. docs/
  12. examples/
  13. include/
  14. kokoro/
  15. src/
  16. third_party/
  17. tools/
README.md

Marl

Marl is a hybrid thread / fiber task scheduler written in C++ 11.

About

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 "marl/waitgroup.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.

  constexpr int numTasks = 10;

  // Create an event that is manually reset.
  marl::Event sayHellow(marl::Event::Mode::Manual);

  // Create a WaitGroup with an initial count of numTasks.
  marl::WaitGroup saidHellow(numTasks);

  // Schedule some tasks to run asynchronously.
  for (int i = 0; i < numTasks; i++) {
    // Each task will run on one of the 4 worker threads.
    marl::schedule([=] {  // All marl primitives are capture-by-value.
      // Decrement the WaitGroup counter when the task has finished.
      defer(saidHellow.done());

      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);
    });
  }

  sayHellow.signal();  // Unblock all the tasks.

  saidHellow.wait();  // Wait for all tasks to complete.

  printf("All tasks said hello.\n");

  // All tasks are guaranteed to complete before the scheduler is destructed.
}

Building

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

Linux and macOS

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

Windows

Marl can be built using Visual Studio 2019's CMake integration.

Using Marl in your CMake project

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})

Benchmarks

Graphs of several microbenchmarks can be found here.


Note: This is not an officially supported Google product