Skip to content

Commit

Permalink
Add support for GCC compiler in CMake build files
Browse files Browse the repository at this point in the history
Replace <experimental/coroutine> with <coroutine>
Replace std::experimental:: with std::
Replace size_t with std::size_t
Add full namespace to randomizer to avoid name clash
Add comments for using custom GCC 10.2 in CMake
Add preprocessor conditional exclusion for template specializations incompatible with GCC
  • Loading branch information
canepat committed Dec 4, 2020
1 parent 547c557 commit 56f27b1
Show file tree
Hide file tree
Showing 58 changed files with 627 additions and 603 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@

# Others
.vs
.vscode/
*.code-workspace

# Specific directories
build/
Expand Down
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,17 @@ std::vector<int> make_random_vector() {
return vec;
}

result<size_t> count_even(std::shared_ptr<thread_pool_executor> tpe, const std::vector<int>& vector) {
result<std::size_t> count_even(std::shared_ptr<thread_pool_executor> tpe, const std::vector<int>& vector) {
const auto vecor_size = vector.size();
const auto concurrency_level = tpe->max_concurrency_level();
const auto chunk_size = vecor_size / concurrency_level;

std::vector<result<size_t>> chunk_count;
std::vector<result<std::size_t>> chunk_count;

for (auto i = 0; i < concurrency_level; i++) {
const auto chunk_begin = i * chunk_size;
const auto chunk_end = chunk_begin + chunk_size;
auto result = tpe->submit([&vector, chunk_begin, chunk_end]() -> size_t {
auto result = tpe->submit([&vector, chunk_begin, chunk_end]() -> std::size_t {
return std::count_if(vector.begin() + chunk_begin, vector.begin() + chunk_end, [](auto i) {
return i % 2 == 0;
});
Expand All @@ -85,7 +85,7 @@ result<size_t> count_even(std::shared_ptr<thread_pool_executor> tpe, const std::
chunk_count.emplace_back(std::move(result));
}

size_t total_count = 0;
std::size_t total_count = 0;

for (auto& result : chunk_count) {
total_count += co_await result;
Expand Down Expand Up @@ -187,13 +187,13 @@ class executor {
Schedules a suspended coroutine to run in this executor.
Throws concurrencpp::errors::executor_shutdown exception if shutdown was called before.
*/
virtual void enqueue(std::experimental::coroutine_handle<> task) = 0;
virtual void enqueue(std::coroutine_handle<> task) = 0;
/*
Schedules a range of suspended coroutines to run in this executor.
Throws concurrencpp::errors::executor_shutdown exception if shutdown was called before.
*/
virtual void enqueue(std::span<std::experimental::coroutine_handle<>> tasks) = 0;
virtual void enqueue(std::span<std::coroutine_handle<>> tasks) = 0;
/*
Returns the maximum count of real OS threads this executor supports.
Expand Down Expand Up @@ -711,7 +711,7 @@ result<std::tuple<>> when_all();
*/
template <class sequence_type>
struct when_any_result {
std::size_t index;
std::std::size_t index;
sequence_type results;
};

Expand Down Expand Up @@ -951,7 +951,7 @@ using namespace std::chrono_literals;
concurrencpp::null_result delayed_task(
std::shared_ptr<concurrencpp::timer_queue> tq,
std::shared_ptr<concurrencpp::thread_pool_executor> ex) {
size_t counter = 1;
std::size_t counter = 1;
while(true) {
std::cout << "task was invoked " << counter << " times." << std::endl;
Expand Down Expand Up @@ -1082,7 +1082,7 @@ class logging_executor : public concurrencpp::derivable_executor<logging_executo
private:
mutable std::mutex _lock;
std::queue<std::experimental::coroutine_handle<>> _queue;
std::queue<std::coroutine_handle<>> _queue;
std::condition_variable _condition;
bool _shutdown_requested;
std::thread _thread;
Expand Down Expand Up @@ -1120,7 +1120,7 @@ public:
});
}
void enqueue(std::experimental::coroutine_handle<> task) override {
void enqueue(std::coroutine_handle<> task) override {
std::cout << _prefix << " A task is being enqueued!" << std::endl;
std::unique_lock<std::mutex> lock(_lock);
Expand All @@ -1132,7 +1132,7 @@ public:
_condition.notify_one();
}
void enqueue(std::span<std::experimental::coroutine_handle<>> tasks) override {
void enqueue(std::span<std::coroutine_handle<>> tasks) override {
std::cout << _prefix << tasks.size() << " tasks are being enqueued!" << std::endl;
std::unique_lock<std::mutex> lock(_lock);
Expand Down Expand Up @@ -1181,7 +1181,7 @@ int main() {
concurrencpp::runtime runtime;
auto logging_ex = runtime.make_executor<logging_executor>("Session #1234");
for (size_t i = 0; i < 10; i++) {
for (std::size_t i = 0; i < 10; i++) {
logging_ex->post([] {
std::cout << "hello world" << std::endl;
});
Expand Down
4 changes: 4 additions & 0 deletions cmake/coroutineOptions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ function(target_coroutine_options TARGET)
target_compile_options(${TARGET} PUBLIC -stdlib=libc++ -fcoroutines-ts)
target_link_options(${TARGET} PUBLIC -stdlib=libc++)
set_target_properties(${TARGET} PROPERTIES CXX_EXTENSIONS NO)
elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
target_compile_options(${TARGET} PUBLIC -std=c++20 -fcoroutines -fpermissive)
target_link_options(${TARGET} PUBLIC)
#set_target_properties(${TARGET} PROPERTIES CXX_EXTENSIONS NO)
else()
message(FATAL_ERROR "Compiler not supported: ${CMAKE_CXX_COMPILER_ID}")
endif()
Expand Down
6 changes: 6 additions & 0 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
cmake_minimum_required(VERSION 3.16)

# GCC >= 10.2 is required. If it's not your default GCC version, use:
# export CC=/usr/bin/gcc-10
# export CXX=/usr/bin/g++-10
# cmake ..
# cmake --build .

project(concurrencppExamples LANGUAGES CXX)

foreach(example IN ITEMS
Expand Down
1 change: 1 addition & 0 deletions example/async_file_processing/source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
background_executor.
*/

#include <cstring>
#include <iostream>
#include <vector>
#include <fstream>
Expand Down
16 changes: 8 additions & 8 deletions example/process_monitor/include/mock_process_monitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ namespace mock_process_monitor {
class monitor {

private:
size_t m_last_cpu_usage;
size_t m_last_memory_usage;
size_t m_last_thread_count;
size_t m_last_kernel_object_count;
std::size_t m_last_cpu_usage;
std::size_t m_last_memory_usage;
std::size_t m_last_thread_count;
std::size_t m_last_kernel_object_count;

public:
monitor() noexcept;

size_t cpu_usage() noexcept;
size_t memory_usage() noexcept;
size_t thread_count() noexcept;
size_t kernel_object_count() noexcept;
std::size_t cpu_usage() noexcept;
std::size_t memory_usage() noexcept;
std::size_t thread_count() noexcept;
std::size_t kernel_object_count() noexcept;
};
} // namespace mock_process_monitor

Expand Down
16 changes: 8 additions & 8 deletions example/process_monitor/source/mock_process_monitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using mock_process_monitor::monitor;

namespace mock_process_monitor {
size_t random_in_range(size_t min, size_t max) {
std::size_t random_in_range(std::size_t min, std::size_t max) {
static const int dummy = [] {
std::srand(static_cast<unsigned int>(std::time(nullptr)));
return 0;
Expand All @@ -20,8 +20,8 @@ namespace mock_process_monitor {

monitor::monitor() noexcept : m_last_cpu_usage(5), m_last_memory_usage(15), m_last_thread_count(4), m_last_kernel_object_count(21) {}

size_t monitor::cpu_usage() noexcept {
size_t max_range, min_range;
std::size_t monitor::cpu_usage() noexcept {
std::size_t max_range, min_range;

if (m_last_cpu_usage >= 95) {
max_range = 95;
Expand All @@ -39,8 +39,8 @@ size_t monitor::cpu_usage() noexcept {
return m_last_cpu_usage;
}

size_t monitor::memory_usage() noexcept {
size_t max_range, min_range;
std::size_t monitor::memory_usage() noexcept {
std::size_t max_range, min_range;

if (m_last_memory_usage >= 95) {
max_range = 95;
Expand All @@ -58,9 +58,9 @@ size_t monitor::memory_usage() noexcept {
return m_last_memory_usage;
}

size_t monitor::thread_count() noexcept {
std::size_t monitor::thread_count() noexcept {
const auto max_range = m_last_thread_count + 4;
size_t min_range;
std::size_t min_range;

if (m_last_thread_count <= 5) {
min_range = 5;
Expand All @@ -72,7 +72,7 @@ size_t monitor::thread_count() noexcept {
return m_last_thread_count;
}

size_t monitor::kernel_object_count() noexcept {
std::size_t monitor::kernel_object_count() noexcept {
const auto max_range = m_last_thread_count + 20;
const auto min_range = m_last_thread_count + 3;

Expand Down
2 changes: 1 addition & 1 deletion example/synchronous_web_socket/source/mock_web_socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace mock_web_socket {
return randomized_num <= 5;
}

size_t random_in_range(size_t min, size_t max) {
std::size_t random_in_range(std::size_t min, std::size_t max) {
const auto range = max - min + 1;
return std::rand() % range + min;
}
Expand Down
12 changes: 6 additions & 6 deletions include/concurrencpp/executors/executor.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace concurrencpp {
}

template<class callable_type>
static null_result bulk_post_bridge(details::executor_bulk_tag, std::vector<std::experimental::coroutine_handle<>>* accumulator, callable_type callable) {
static null_result bulk_post_bridge(details::executor_bulk_tag, std::vector<std::coroutine_handle<>>* accumulator, callable_type callable) {
callable();
co_return;
}
Expand All @@ -35,7 +35,7 @@ namespace concurrencpp {

template<class callable_type, class return_type = typename std::invoke_result_t<callable_type>>
static result<return_type> bulk_submit_bridge(details::executor_bulk_tag,
std::vector<std::experimental::coroutine_handle<>>* accumulator,
std::vector<std::coroutine_handle<>>* accumulator,
callable_type callable) {
co_return callable();
}
Expand All @@ -61,7 +61,7 @@ namespace concurrencpp {

template<class executor_type, class callable_type>
static void do_bulk_post(executor_type* executor_ptr, std::span<callable_type> callable_list) {
std::vector<std::experimental::coroutine_handle<>> accumulator;
std::vector<std::coroutine_handle<>> accumulator;
accumulator.reserve(callable_list.size());

for (auto& callable : callable_list) {
Expand All @@ -74,7 +74,7 @@ namespace concurrencpp {

template<class executor_type, class callable_type, class return_type = std::invoke_result_t<callable_type>>
static std::vector<concurrencpp::result<return_type>> do_bulk_submit(executor_type* executor_ptr, std::span<callable_type> callable_list) {
std::vector<std::experimental::coroutine_handle<>> accumulator;
std::vector<std::coroutine_handle<>> accumulator;
accumulator.reserve(callable_list.size());

std::vector<concurrencpp::result<return_type>> results;
Expand All @@ -96,8 +96,8 @@ namespace concurrencpp {

const std::string name;

virtual void enqueue(std::experimental::coroutine_handle<> task) = 0;
virtual void enqueue(std::span<std::experimental::coroutine_handle<>> tasks) = 0;
virtual void enqueue(std::coroutine_handle<> task) = 0;
virtual void enqueue(std::span<std::coroutine_handle<>> tasks) = 0;

virtual int max_concurrency_level() const noexcept = 0;

Expand Down
4 changes: 2 additions & 2 deletions include/concurrencpp/executors/inline_executor.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ namespace concurrencpp {
public:
inline_executor() noexcept : executor(details::consts::k_inline_executor_name), m_abort(false) {}

void enqueue(std::experimental::coroutine_handle<> task) override {
void enqueue(std::coroutine_handle<> task) override {
throw_if_aborted();
task();
}

void enqueue(std::span<std::experimental::coroutine_handle<>> tasks) override {
void enqueue(std::span<std::coroutine_handle<>> tasks) override {
throw_if_aborted();

for (auto& task : tasks) {
Expand Down
12 changes: 6 additions & 6 deletions include/concurrencpp/executors/manual_executor.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace concurrencpp {

private:
mutable std::mutex m_lock;
std::deque<std::experimental::coroutine_handle<>> m_tasks;
std::deque<std::coroutine_handle<>> m_tasks;
std::condition_variable m_condition;
bool m_abort;
std::atomic_bool m_atomic_abort;
Expand All @@ -21,23 +21,23 @@ namespace concurrencpp {
public:
manual_executor();

void enqueue(std::experimental::coroutine_handle<> task) override;
void enqueue(std::span<std::experimental::coroutine_handle<>> tasks) override;
void enqueue(std::coroutine_handle<> task) override;
void enqueue(std::span<std::coroutine_handle<>> tasks) override;

int max_concurrency_level() const noexcept override;

void shutdown() noexcept override;
bool shutdown_requested() const noexcept override;

size_t size() const noexcept;
std::size_t size() const noexcept;
bool empty() const noexcept;

bool loop_once();
bool loop_once(std::chrono::milliseconds max_waiting_time);

size_t loop(size_t max_count);
std::size_t loop(std::size_t max_count);

size_t clear() noexcept;
std::size_t clear() noexcept;

void wait_for_task();
bool wait_for_task(std::chrono::milliseconds max_waiting_time);
Expand Down
12 changes: 6 additions & 6 deletions include/concurrencpp/executors/thread_executor.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <span>
#include <mutex>
#include <condition_variable>
#include <experimental/coroutine>
#include <coroutine>

namespace concurrencpp::details {
class thread_worker {
Expand All @@ -19,13 +19,13 @@ namespace concurrencpp::details {
thread m_thread;
thread_executor& m_parent_pool;

void execute_and_retire(std::experimental::coroutine_handle<> task, typename std::list<thread_worker>::iterator self_it);
void execute_and_retire(std::coroutine_handle<> task, typename std::list<thread_worker>::iterator self_it);

public:
thread_worker(thread_executor& parent_pool) noexcept;
~thread_worker() noexcept;

void start(const std::string worker_name, std::experimental::coroutine_handle<> task, std::list<thread_worker>::iterator self_it);
void start(const std::string worker_name, std::coroutine_handle<> task, std::list<thread_worker>::iterator self_it);
};
} // namespace concurrencpp::details

Expand All @@ -42,16 +42,16 @@ namespace concurrencpp {
bool m_abort;
std::atomic_bool m_atomic_abort;

void enqueue_impl(std::experimental::coroutine_handle<> task);
void enqueue_impl(std::coroutine_handle<> task);

void retire_worker(std::list<details::thread_worker>::iterator it);

public:
thread_executor();
~thread_executor() noexcept;

void enqueue(std::experimental::coroutine_handle<> task) override;
void enqueue(std::span<std::experimental::coroutine_handle<>> tasks) override;
void enqueue(std::coroutine_handle<> task) override;
void enqueue(std::span<std::coroutine_handle<>> tasks) override;

int max_concurrency_level() const noexcept override;

Expand Down
Loading

0 comments on commit 56f27b1

Please sign in to comment.