Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HOLD MERGE PLEASE] ThreadPool compile time template parameter; CMake + CI cross platform work #9

Closed
Closed
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 88 additions & 14 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,23 +1,97 @@
cmake_minimum_required(VERSION 3.0)
cmake_minimum_required(VERSION 3.3) # support visibility settings

list(APPEND CMAKE_MODULE_PATH
"${CMAKE_CURRENT_SOURCE_DIR}/cmake"
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules")
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake/Modules")

project(thread-pool-cpp VERSION 1.1.0)

project(thread-pool-cpp)
add_definitions(-std=c++14 -Wall -Werror -O3)

option(THREAD_POOL_CPP_BUILD_TESTS "Build tests." OFF)
option(THREAD_POOL_CPP_BUILD_BENCHMARKS "Build benchmarks." OFF)

if(THREAD_POOL_CPP_BUILD_TESTS)
include(CTest)
endif()

# Get all include files
# Variable used in: benchmark, tests
set(THREAD_POOL_CPP_INC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/include/")
include_directories("${THREAD_POOL_CPP_INC_DIR}")
file(GLOB_RECURSE INSTALL_FILES_LIST "${THREAD_POOL_CPP_INC_DIR}/*")

add_subdirectory(tests)
add_subdirectory(benchmark)
# If true C++11 thread_local support exists, we will use it:
include(thread_pool_has_thread_local_storage)
thread_pool_has_thread_local_storage(THREAD_POOL_HAS_THREAD_LOCAL_STORAGE)
message("THREAD_POOL_HAS_THREAD_LOCAL_STORAGE: ${THREAD_POOL_HAS_THREAD_LOCAL_STORAGE}")

if(NOT THREAD_POOL_HAS_THREAD_LOCAL_STORAGE)
# Else, we will check for backups
include(thread_pool_has_thread_storage)
thread_pool_has_thread_storage(THREAD_POOL_HAS_THREAD_STORAGE)
message("THREAD_POOL_HAS_THREAD_STORAGE: ${THREAD_POOL_HAS_THREAD_STORAGE}")
if(NOT THREAD_POOL_HAS_THREAD_STORAGE)
message(FATAL_ERROR "Compiler does not support: thread_local, __thread, or declspec(thread)")
endif()
endif()

# http://stackoverflow.com/a/11583676
add_library(thread-pool-cpp INTERFACE)

if(THREAD_POOL_CPP_BUILD_TESTS)
add_subdirectory(tests)
endif()

if(THREAD_POOL_CPP_BUILD_BENCHMARKS)
add_subdirectory(benchmark)
endif()

#### install

set(TARGET_NAME thread-pool-cpp)

set(config_install_dir "lib/cmake/${TARGET_NAME}")
set(include_install_dir "include")

set(generated_dir "${CMAKE_CURRENT_BINARY_DIR}/generated")

# Configuration
set(version_config "${generated_dir}/${TARGET_NAME}ConfigVersion.cmake")
set(project_config "${generated_dir}/${TARGET_NAME}Config.cmake")
set(targets_export_name "${TARGET_NAME}Targets")
set(namespace "${TARGET_NAME}::")

include(CMakePackageConfigHelpers)

write_basic_package_version_file(
"${version_config}" COMPATIBILITY SameMajorVersion
)

configure_package_config_file(
"cmake/Config.cmake.in"
"${project_config}"
INSTALL_DESTINATION "${config_install_dir}"
)

install(
TARGETS thread-pool-cpp
EXPORT "${targets_export_name}"
LIBRARY DESTINATION "lib"
ARCHIVE DESTINATION "lib"
RUNTIME DESTINATION "bin"
INCLUDES DESTINATION "${include_install_dir}"
)

install(
DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/thread_pool
DESTINATION "${include_install_dir}"
FILES_MATCHING PATTERN "*.h" PATTERN "*.hpp"
)

install(
FILES "${project_config}" "${version_config}"
DESTINATION "${config_install_dir}"
)

# Install as header-only library
set_source_files_properties(${INSTALL_FILES_LIST} PROPERTIES HEADER_FILE_ONLY 1)
add_library(HEADER_ONLY_TARGET STATIC ${INSTALL_FILES_LIST})
set_target_properties(HEADER_ONLY_TARGET PROPERTIES LINKER_LANGUAGE CXX)
install(DIRECTORY ${THREAD_POOL_CPP_INC_DIR} DESTINATION "include")
install(
EXPORT "${targets_export_name}"
NAMESPACE "${namespace}"
DESTINATION "${config_install_dir}"
)
7 changes: 0 additions & 7 deletions benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
#benchmark

include_directories("${THREAD_POOL_CPP_INC_DIR}")

find_package(Boost REQUIRED COMPONENTS system)

include_directories(${CMAKE_CURRENT_SOURCE_DIR})
include_directories(${Boost_INCLUDE_DIR})
add_executable(benchmark benchmark.cpp)
target_link_libraries(benchmark ${Boost_LIBRARIES} pthread)

82 changes: 0 additions & 82 deletions benchmark/asio_thread_pool.hpp

This file was deleted.

62 changes: 3 additions & 59 deletions benchmark/benchmark.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
//#define WITHOUT_ASIO 1

#include <thread_pool.hpp>

#ifndef WITHOUT_ASIO
#include <asio_thread_pool.hpp>
#endif
#include <thread_pool/thread_pool.hpp>

#include <iostream>
#include <chrono>
Expand Down Expand Up @@ -88,50 +82,25 @@ struct RepostJob
// Heavy heavy;

ThreadPoolStd* thread_pool;
#ifndef WITHOUT_ASIO
AsioThreadPool* asio_thread_pool;
#endif

volatile size_t counter;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All occurrences of size_t should be properly qualified as std::size_t

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. I can replace size_t -> std::size_t in this PR.

long long int begin_count;
std::promise<void>* waiter;

RepostJob(ThreadPoolStd* thread_pool, std::promise<void>* waiter)
: thread_pool(thread_pool)
#ifndef WITHOUT_ASIO
,
asio_thread_pool(0)
#endif
,
counter(0), waiter(waiter)
{
begin_count = std::chrono::high_resolution_clock::now()
.time_since_epoch()
.count();
}

#ifndef WITHOUT_ASIO
RepostJob(AsioThreadPool* asio_thread_pool, std::promise<void>* waiter)
: thread_pool(0), asio_thread_pool(asio_thread_pool), counter(0),
waiter(waiter)
, counter(0)
, waiter(waiter)
{
begin_count = std::chrono::high_resolution_clock::now()
.time_since_epoch()
.count();
}
#endif

void operator()()
{
if(counter++ < REPOST_COUNT)
{
#ifndef WITHOUT_ASIO
if(asio_thread_pool)
{
asio_thread_pool->post(*this);
return;
}
#endif
if(thread_pool)
{
thread_pool->post(*this);
Expand Down Expand Up @@ -171,30 +140,5 @@ int main(int, const char* [])
}
}

#ifndef WITHOUT_ASIO
{
std::cout << "***asio thread pool***" << std::endl;

size_t workers_count = std::thread::hardware_concurrency();
if(0 == workers_count)
{
workers_count = 1;
}

AsioThreadPool asio_thread_pool(workers_count);

std::promise<void> waiters[CONCURRENCY];
for(auto& waiter : waiters)
{
asio_thread_pool.post(RepostJob(&asio_thread_pool, &waiter));
}

for(auto& waiter : waiters)
{
waiter.get_future().wait();
}
}
#endif

return 0;
}
4 changes: 4 additions & 0 deletions cmake/Config.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@PACKAGE_INIT@

include("${CMAKE_CURRENT_LIST_DIR}/@[email protected]")
check_required_components("@PROJECT_NAME@")
86 changes: 0 additions & 86 deletions cmake/Findthread-pool-cpp.cmake

This file was deleted.

Loading