Skip to content

Commit

Permalink
Merge pull request #40 from Exerg/develop
Browse files Browse the repository at this point in the history
Release 1.0
  • Loading branch information
CBenoit authored Jan 10, 2019
2 parents 39af41e + ab61631 commit 30686ad
Show file tree
Hide file tree
Showing 28 changed files with 3,222 additions and 1,865 deletions.
2 changes: 2 additions & 0 deletions .conanfile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[requires]
gtest/1.8.1@bincrafters/stable
83 changes: 69 additions & 14 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,83 @@ cmake_minimum_required(VERSION 3.10) # For CXX_STANDARD 17 property on Visual St
project(WaitFreeHashMap)
enable_language(CXX)

include(cmake/common.cmake)
include(cmake/get_external_dependency.cmake)
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)

include(ctulu)
include(conan)
include(setup_output_dirs)

set(CMAKE_DEBUG_POSTFIX _d)

# By default build in Release mode
if( NOT CMAKE_BUILD_TYPE )
if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release")
endif()

get_filename_component(root_dir ${CMAKE_SOURCE_DIR} ABSOLUTE)
get_filename_component(root_dir ${CMAKE_CURRENT_SOURCE_DIR} ABSOLUTE)
get_filename_component(include_dir ${root_dir}/include ABSOLUTE)
make_target(WaitFreeHashMapLibrary "WaitFreeHashMap" INCLUDES ${include_dir} OPTIONS cxx interface)
get_filename_component(tests_dir ${root_dir}/tests ABSOLUTE)
get_filename_component(example_dir ${root_dir}/examples ABSOLUTE)

# set parameter INTERFACE since the library is header only.
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
message(STATUS "Activating warning on library")
ctulu_create_target(WaitFreeCollections "WaitFreeCollections" INTERFACE_INCLUDES ${include_dir} CXX 17 INTERFACE)
else()
ctulu_create_target(WaitFreeCollections "WaitFreeCollections" INTERFACE_INCLUDES SYSTEM ${include_dir} CXX 17 INTERFACE)
endif()

option(WFC_BUILD_EXAMPLES "Build project examples" OFF)
option(WFC_BUILD_TESTS "Build project tests" OFF)
option(WFC_BUILD_CLANG_FORMAT_TARGET "Build clang-format target" OFF)
option(WFC_BUILD_ALL "Activate all previous options" OFF)

if (WFC_BUILD_ALL)
set(WFC_BUILD_EXAMPLES ON)
set(WFC_BUILD_TESTS ON)
set(WFC_BUILD_CLANG_FORMAT_TARGET ON)
endif()

if (WFC_BUILD_EXAMPLES)
find_package(Threads REQUIRED)

ctulu_create_target(UnorderedMapExample1 "WaitFreeCollections" FILES ${example_dir}/unordered_map_example.cpp CXX 17 EXECUTABLE W_LEVEL 2)
ctulu_target_warning_from_file(UnorderedMapExample1 ${root_dir}/cmake/warnings.txt)
target_link_libraries(UnorderedMapExample1 Threads::Threads WaitFreeCollections)
endif()

find_package(Threads REQUIRED)
get_external_dependency(googletest external_libs/google_test.cmake)
if (WFC_BUILD_CLANG_FORMAT_TARGET)
set(DIRS "${include_dir}")
if (BUILD_TESTS)
list(APPEND DIRS ${test_dir})
endif()
if (BUILD_EXAMPLES)
list(APPEND DIRS ${example_dir})
endif()

make_target(Sample1 "WaitFreeHashMap" example/main.cpp OPTIONS cxx executable)
set_property(TARGET Sample1 PROPERTY CXX_STANDARD 17)
target_link_libraries(Sample1 ${CMAKE_THREAD_LIBS_INIT} WaitFreeHashMapLibrary gtest_main)
ctulu_generate_clang_format("Clang-format" DIRS ${DIRS})
endif()

if (CMAKE_BUILD_TYPE MATCHES "Debug")
set(WFC_BUILD_TESTS ON)
endif()

if (WFC_BUILD_TESTS)
message(STATUS "Enabling testing")
enable_testing()

find_package(Threads REQUIRED)

conan_check(REQUIRED)
conan_cmake_run(CONANFILE .conanfile.txt BASIC_SETUP CMAKE_TARGETS BUILD missing)

ctulu_create_target(UnorderedMapTests "WaitFreeCollections" DIRS ${tests_dir}/unordered_map/ TEST CXX 17 W_LEVEL 2)
ctulu_target_warning_from_file(UnorderedMapTests ${root_dir}/cmake/warnings.txt)
target_link_libraries(UnorderedMapTests WaitFreeCollections CONAN_PKG::gtest Threads::Threads)

ctulu_create_target(UtilityTests "WaitFreeCollections" DIRS ${tests_dir}/utility/ TEST CXX 17 W_LEVEL 2)
ctulu_target_warning_from_file(UtilityTests ${root_dir}/cmake/warnings.txt)
target_link_libraries(UtilityTests WaitFreeCollections CONAN_PKG::gtest)
endif()

make_target(unit_tests "WaitFreeHashMap" test/single_thread_test.cpp OPTIONS cxx executable)
set_property(TARGET unit_tests PROPERTY CXX_STANDARD 17)
target_link_libraries(unit_tests WaitFreeHashMapLibrary gtest_main)
add_test(NAME example_test COMMAND unit_tests)
#set(CMAKE_VERBOSE_MAKEFILE 1)
178 changes: 173 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,183 @@
# WaitFreeHashMap
# WaitFreeCollections

<table>
<tr>
<td><strong>master branch</strong></td>
<td>
<a href="https://ci.appveyor.com/project/CBenoit/waitfreecollections/branch/master"><img src="https://ci.appveyor.com/api/projects/status/sucn29if5de65t01/branch/master?svg=true"></a>
</td>
</tr>
<tr>
<td><strong>develop branch</strong></td>
<td>
<a href="https://ci.appveyor.com/project/CBenoit/waitfreecollections/branch/develop"><img src="https://ci.appveyor.com/api/projects/status/sucn29if5de65t01/branch/develop?svg=true"></a>
</td>
</tr>
<tr>
<td><strong>License</strong></td>
<td>
<a href="https://opensource.org/licenses/MIT"><img src="https://img.shields.io/badge/License-MIT-yellow.svg"></a>
</td>
</tr>
</table>

A header-only library providing wait-free collections such as hash map and double ended queue (deque).

## Wait-Free Hash Map

An implementation of a wait-free hash map as described in the article

An implementation of an hashmap described in the article:
> P. Laborde, S. Feldman et D. Dechev, « A Wait-Free Hash Map », <br>
> International Journal of Parallel Programming, t. 45, n o 3, p. 421-448, 2017, issn : 1573-7640. <br>
> doi : https://doi.org/10.1007/s10766-015-0376-3
# Copyright
See [unordered_map example](./examples/unordered_map_example.cpp) in `examples` folder.

License: MIT
## Double ended queue (Deque)

WIP

# Requirements
## Requirements

- Compiler which supports C++17
- CMake ≥ 3.10

## Minimal unordered map example

```cpp
#include <iostream>
#include <thread>

#include <wfc/unordered_map.hpp>

constexpr int nbr_threads = 16;

int main()
{
wfc::unordered_map<std::size_t, std::size_t> m(4, nbr_threads, nbr_threads);

std::array<std::thread, nbr_threads> threads;
for (std::size_t i = 0; i < nbr_threads; ++i)
{
threads[i] = std::thread([&m, i]() {
m.insert(i, i);
});
}

for (auto& t: threads)
{
t.join();
}

m.visit([](std::pair<const std::size_t, std::size_t> p) {
std::cout << '[' << p.first << '-' << p.second << "]\n";
});

return 0;
}
```

This should output:

```
[0-0]
[1-1]
[2-2]
[3-3]
[4-4]
[5-5]
[6-6]
[7-7]
[8-8]
[9-9]
[10-10]
[11-11]
[12-12]
[13-13]
[14-14]
[15-15]
```

## How to import the library using CMake

To include the library, you may copy / paste the content of this repository in a subfolder (such as `externals`) or use a git submodule.

Then, in you CMakeLists.txt file, you just have to do something similar to:

```cmake
cmake_minimum_required(VERSION 3.10)
project(MyProject)
enable_language(CXX)
set(CMAKE_CXX_STANDARD 17)
add_subdirectory(externals/WaitFreeCollections)
find_package(Threads REQUIRED) # you'll probably need that one too
add_executable(MyTarget src/main.cpp)
target_link_libraries(MyTarget Threads::Threads WaitFreeCollections)
```

At this point, the project hierarchy looks like:

```
|- CMakeLists.txt
|- src/
|- main.cpp
|- externals/
|- WaitFreeCollections/
|- ...
```

You may try to use the minimal example above as your `main.cpp`.

Also, since this is a header-only library, you may copy / paste the content of the include folder in your
project to achieve similar results (maybe a little bit less clean though).

## Build targets

You don't need to actually build the library beforehand to use it in your project due to the "header-only" nature of it.
The build targets in this repository are for tests, exemples and code formatting.
These are good to know should you contribute to this project or play with the exemples.

First, you need to create a sub-directory to run `cmake` from it. Then you can build using `make`.

```
$ mkdir build
$ cmake .. -DWFC_BUILD_ALL=1
```

By default, everything is build in Release mode. You can pass `-DCMAKE_BUILD_TYPE=Debug` to `cmake` to ask Debug builds.
This will also activate useful features for developing purposes such as the holy warnings.
The `-DWFC_BUILD_ALL=1` parameter will tell CMake to include all our targets in the build process.

CMake will generate several targets that you can build separately using the `--target` parameter:

- `UnorderedMapExample1`
- `UtilityTests`
- `UnorderedMapTests`
- `Clang-format`

For instance, to build the `UnorderedMapTests` run

```
$ cmake --build . --target UnorderedMapTests
```

You can also build everything by not giving any specific target.
Produced executables are inside the `bin` folder.

Note that the `Clang-format` target does not produce anything.
It just run the clang formatter.
Also this target needs to be called explicitly.

For more details, see the [CMakeLists.txt](CMakeLists.txt) file.

## Copyright

License: MIT

Main contributors:

- [Jérôme Boulmier](https://github.com/Lomadriel)
- [Benoît Cortier](https://github.com/CBenoit)
35 changes: 35 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
image: ubuntu

install:
# install conan
- sudo pip install conan --upgrade

before_build:
# update gcc and g++ alternatives
- sudo update-alternatives --set gcc /usr/bin/gcc-8

- gcc --version
- g++ --version
- cmake --version

- mkdir ${APPVEYOR_BUILD_FOLDER}/debug_build
- cd ${APPVEYOR_BUILD_FOLDER}/debug_build
- cmake .. -DWFC_BUILD_ALL=1 -DCMAKE_BUILD_TYPE=Debug

- mkdir ${APPVEYOR_BUILD_FOLDER}/release_build
- cd ${APPVEYOR_BUILD_FOLDER}/release_build
- cmake .. -DWFC_BUILD_ALL=1 -DCMAKE_BUILD_TYPE=Release

build_script:
- cd ${APPVEYOR_BUILD_FOLDER}/debug_build
- make

- cd ${APPVEYOR_BUILD_FOLDER}/release_build
- make

test_script:
- ${APPVEYOR_BUILD_FOLDER}/debug_build/bin/UnorderedMapTests
- ${APPVEYOR_BUILD_FOLDER}/debug_build/bin/UtilityTests
- ${APPVEYOR_BUILD_FOLDER}/release_build/bin/UnorderedMapTests
- ${APPVEYOR_BUILD_FOLDER}/release_build/bin/UtilityTests

Loading

0 comments on commit 30686ad

Please sign in to comment.