Skip to content

Commit 56f27b1

Browse files
committed
Add support for GCC compiler in CMake build files
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
1 parent 547c557 commit 56f27b1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+627
-603
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939

4040
# Others
4141
.vs
42+
.vscode/
43+
*.code-workspace
4244

4345
# Specific directories
4446
build/

README.md

+12-12
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,17 @@ std::vector<int> make_random_vector() {
6666
return vec;
6767
}
6868

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

74-
std::vector<result<size_t>> chunk_count;
74+
std::vector<result<std::size_t>> chunk_count;
7575

7676
for (auto i = 0; i < concurrency_level; i++) {
7777
const auto chunk_begin = i * chunk_size;
7878
const auto chunk_end = chunk_begin + chunk_size;
79-
auto result = tpe->submit([&vector, chunk_begin, chunk_end]() -> size_t {
79+
auto result = tpe->submit([&vector, chunk_begin, chunk_end]() -> std::size_t {
8080
return std::count_if(vector.begin() + chunk_begin, vector.begin() + chunk_end, [](auto i) {
8181
return i % 2 == 0;
8282
});
@@ -85,7 +85,7 @@ result<size_t> count_even(std::shared_ptr<thread_pool_executor> tpe, const std::
8585
chunk_count.emplace_back(std::move(result));
8686
}
8787

88-
size_t total_count = 0;
88+
std::size_t total_count = 0;
8989

9090
for (auto& result : chunk_count) {
9191
total_count += co_await result;
@@ -187,13 +187,13 @@ class executor {
187187
Schedules a suspended coroutine to run in this executor.
188188
Throws concurrencpp::errors::executor_shutdown exception if shutdown was called before.
189189
*/
190-
virtual void enqueue(std::experimental::coroutine_handle<> task) = 0;
190+
virtual void enqueue(std::coroutine_handle<> task) = 0;
191191
192192
/*
193193
Schedules a range of suspended coroutines to run in this executor.
194194
Throws concurrencpp::errors::executor_shutdown exception if shutdown was called before.
195195
*/
196-
virtual void enqueue(std::span<std::experimental::coroutine_handle<>> tasks) = 0;
196+
virtual void enqueue(std::span<std::coroutine_handle<>> tasks) = 0;
197197
198198
/*
199199
Returns the maximum count of real OS threads this executor supports.
@@ -711,7 +711,7 @@ result<std::tuple<>> when_all();
711711
*/
712712
template <class sequence_type>
713713
struct when_any_result {
714-
std::size_t index;
714+
std::std::size_t index;
715715
sequence_type results;
716716
};
717717

@@ -951,7 +951,7 @@ using namespace std::chrono_literals;
951951
concurrencpp::null_result delayed_task(
952952
std::shared_ptr<concurrencpp::timer_queue> tq,
953953
std::shared_ptr<concurrencpp::thread_pool_executor> ex) {
954-
size_t counter = 1;
954+
std::size_t counter = 1;
955955
956956
while(true) {
957957
std::cout << "task was invoked " << counter << " times." << std::endl;
@@ -1082,7 +1082,7 @@ class logging_executor : public concurrencpp::derivable_executor<logging_executo
10821082
10831083
private:
10841084
mutable std::mutex _lock;
1085-
std::queue<std::experimental::coroutine_handle<>> _queue;
1085+
std::queue<std::coroutine_handle<>> _queue;
10861086
std::condition_variable _condition;
10871087
bool _shutdown_requested;
10881088
std::thread _thread;
@@ -1120,7 +1120,7 @@ public:
11201120
});
11211121
}
11221122
1123-
void enqueue(std::experimental::coroutine_handle<> task) override {
1123+
void enqueue(std::coroutine_handle<> task) override {
11241124
std::cout << _prefix << " A task is being enqueued!" << std::endl;
11251125
11261126
std::unique_lock<std::mutex> lock(_lock);
@@ -1132,7 +1132,7 @@ public:
11321132
_condition.notify_one();
11331133
}
11341134
1135-
void enqueue(std::span<std::experimental::coroutine_handle<>> tasks) override {
1135+
void enqueue(std::span<std::coroutine_handle<>> tasks) override {
11361136
std::cout << _prefix << tasks.size() << " tasks are being enqueued!" << std::endl;
11371137
11381138
std::unique_lock<std::mutex> lock(_lock);
@@ -1181,7 +1181,7 @@ int main() {
11811181
concurrencpp::runtime runtime;
11821182
auto logging_ex = runtime.make_executor<logging_executor>("Session #1234");
11831183
1184-
for (size_t i = 0; i < 10; i++) {
1184+
for (std::size_t i = 0; i < 10; i++) {
11851185
logging_ex->post([] {
11861186
std::cout << "hello world" << std::endl;
11871187
});

cmake/coroutineOptions.cmake

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ function(target_coroutine_options TARGET)
1414
target_compile_options(${TARGET} PUBLIC -stdlib=libc++ -fcoroutines-ts)
1515
target_link_options(${TARGET} PUBLIC -stdlib=libc++)
1616
set_target_properties(${TARGET} PROPERTIES CXX_EXTENSIONS NO)
17+
elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
18+
target_compile_options(${TARGET} PUBLIC -std=c++20 -fcoroutines -fpermissive)
19+
target_link_options(${TARGET} PUBLIC)
20+
#set_target_properties(${TARGET} PROPERTIES CXX_EXTENSIONS NO)
1721
else()
1822
message(FATAL_ERROR "Compiler not supported: ${CMAKE_CXX_COMPILER_ID}")
1923
endif()

example/CMakeLists.txt

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
cmake_minimum_required(VERSION 3.16)
22

3+
# GCC >= 10.2 is required. If it's not your default GCC version, use:
4+
# export CC=/usr/bin/gcc-10
5+
# export CXX=/usr/bin/g++-10
6+
# cmake ..
7+
# cmake --build .
8+
39
project(concurrencppExamples LANGUAGES CXX)
410

511
foreach(example IN ITEMS

example/async_file_processing/source/main.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
background_executor.
2121
*/
2222

23+
#include <cstring>
2324
#include <iostream>
2425
#include <vector>
2526
#include <fstream>

example/process_monitor/include/mock_process_monitor.h

+8-8
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@ namespace mock_process_monitor {
77
class monitor {
88

99
private:
10-
size_t m_last_cpu_usage;
11-
size_t m_last_memory_usage;
12-
size_t m_last_thread_count;
13-
size_t m_last_kernel_object_count;
10+
std::size_t m_last_cpu_usage;
11+
std::size_t m_last_memory_usage;
12+
std::size_t m_last_thread_count;
13+
std::size_t m_last_kernel_object_count;
1414

1515
public:
1616
monitor() noexcept;
1717

18-
size_t cpu_usage() noexcept;
19-
size_t memory_usage() noexcept;
20-
size_t thread_count() noexcept;
21-
size_t kernel_object_count() noexcept;
18+
std::size_t cpu_usage() noexcept;
19+
std::size_t memory_usage() noexcept;
20+
std::size_t thread_count() noexcept;
21+
std::size_t kernel_object_count() noexcept;
2222
};
2323
} // namespace mock_process_monitor
2424

example/process_monitor/source/mock_process_monitor.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
using mock_process_monitor::monitor;
77

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

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

23-
size_t monitor::cpu_usage() noexcept {
24-
size_t max_range, min_range;
23+
std::size_t monitor::cpu_usage() noexcept {
24+
std::size_t max_range, min_range;
2525

2626
if (m_last_cpu_usage >= 95) {
2727
max_range = 95;
@@ -39,8 +39,8 @@ size_t monitor::cpu_usage() noexcept {
3939
return m_last_cpu_usage;
4040
}
4141

42-
size_t monitor::memory_usage() noexcept {
43-
size_t max_range, min_range;
42+
std::size_t monitor::memory_usage() noexcept {
43+
std::size_t max_range, min_range;
4444

4545
if (m_last_memory_usage >= 95) {
4646
max_range = 95;
@@ -58,9 +58,9 @@ size_t monitor::memory_usage() noexcept {
5858
return m_last_memory_usage;
5959
}
6060

61-
size_t monitor::thread_count() noexcept {
61+
std::size_t monitor::thread_count() noexcept {
6262
const auto max_range = m_last_thread_count + 4;
63-
size_t min_range;
63+
std::size_t min_range;
6464

6565
if (m_last_thread_count <= 5) {
6666
min_range = 5;
@@ -72,7 +72,7 @@ size_t monitor::thread_count() noexcept {
7272
return m_last_thread_count;
7373
}
7474

75-
size_t monitor::kernel_object_count() noexcept {
75+
std::size_t monitor::kernel_object_count() noexcept {
7676
const auto max_range = m_last_thread_count + 20;
7777
const auto min_range = m_last_thread_count + 3;
7878

example/synchronous_web_socket/source/mock_web_socket.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace mock_web_socket {
1919
return randomized_num <= 5;
2020
}
2121

22-
size_t random_in_range(size_t min, size_t max) {
22+
std::size_t random_in_range(std::size_t min, std::size_t max) {
2323
const auto range = max - min + 1;
2424
return std::rand() % range + min;
2525
}

include/concurrencpp/executors/executor.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace concurrencpp {
2323
}
2424

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

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

6262
template<class executor_type, class callable_type>
6363
static void do_bulk_post(executor_type* executor_ptr, std::span<callable_type> callable_list) {
64-
std::vector<std::experimental::coroutine_handle<>> accumulator;
64+
std::vector<std::coroutine_handle<>> accumulator;
6565
accumulator.reserve(callable_list.size());
6666

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

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

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

9797
const std::string name;
9898

99-
virtual void enqueue(std::experimental::coroutine_handle<> task) = 0;
100-
virtual void enqueue(std::span<std::experimental::coroutine_handle<>> tasks) = 0;
99+
virtual void enqueue(std::coroutine_handle<> task) = 0;
100+
virtual void enqueue(std::span<std::coroutine_handle<>> tasks) = 0;
101101

102102
virtual int max_concurrency_level() const noexcept = 0;
103103

include/concurrencpp/executors/inline_executor.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ namespace concurrencpp {
2121
public:
2222
inline_executor() noexcept : executor(details::consts::k_inline_executor_name), m_abort(false) {}
2323

24-
void enqueue(std::experimental::coroutine_handle<> task) override {
24+
void enqueue(std::coroutine_handle<> task) override {
2525
throw_if_aborted();
2626
task();
2727
}
2828

29-
void enqueue(std::span<std::experimental::coroutine_handle<>> tasks) override {
29+
void enqueue(std::span<std::coroutine_handle<>> tasks) override {
3030
throw_if_aborted();
3131

3232
for (auto& task : tasks) {

include/concurrencpp/executors/manual_executor.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace concurrencpp {
1111

1212
private:
1313
mutable std::mutex m_lock;
14-
std::deque<std::experimental::coroutine_handle<>> m_tasks;
14+
std::deque<std::coroutine_handle<>> m_tasks;
1515
std::condition_variable m_condition;
1616
bool m_abort;
1717
std::atomic_bool m_atomic_abort;
@@ -21,23 +21,23 @@ namespace concurrencpp {
2121
public:
2222
manual_executor();
2323

24-
void enqueue(std::experimental::coroutine_handle<> task) override;
25-
void enqueue(std::span<std::experimental::coroutine_handle<>> tasks) override;
24+
void enqueue(std::coroutine_handle<> task) override;
25+
void enqueue(std::span<std::coroutine_handle<>> tasks) override;
2626

2727
int max_concurrency_level() const noexcept override;
2828

2929
void shutdown() noexcept override;
3030
bool shutdown_requested() const noexcept override;
3131

32-
size_t size() const noexcept;
32+
std::size_t size() const noexcept;
3333
bool empty() const noexcept;
3434

3535
bool loop_once();
3636
bool loop_once(std::chrono::milliseconds max_waiting_time);
3737

38-
size_t loop(size_t max_count);
38+
std::size_t loop(std::size_t max_count);
3939

40-
size_t clear() noexcept;
40+
std::size_t clear() noexcept;
4141

4242
void wait_for_task();
4343
bool wait_for_task(std::chrono::milliseconds max_waiting_time);

include/concurrencpp/executors/thread_executor.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include <span>
1111
#include <mutex>
1212
#include <condition_variable>
13-
#include <experimental/coroutine>
13+
#include <coroutine>
1414

1515
namespace concurrencpp::details {
1616
class thread_worker {
@@ -19,13 +19,13 @@ namespace concurrencpp::details {
1919
thread m_thread;
2020
thread_executor& m_parent_pool;
2121

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

2424
public:
2525
thread_worker(thread_executor& parent_pool) noexcept;
2626
~thread_worker() noexcept;
2727

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

@@ -42,16 +42,16 @@ namespace concurrencpp {
4242
bool m_abort;
4343
std::atomic_bool m_atomic_abort;
4444

45-
void enqueue_impl(std::experimental::coroutine_handle<> task);
45+
void enqueue_impl(std::coroutine_handle<> task);
4646

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

4949
public:
5050
thread_executor();
5151
~thread_executor() noexcept;
5252

53-
void enqueue(std::experimental::coroutine_handle<> task) override;
54-
void enqueue(std::span<std::experimental::coroutine_handle<>> tasks) override;
53+
void enqueue(std::coroutine_handle<> task) override;
54+
void enqueue(std::span<std::coroutine_handle<>> tasks) override;
5555

5656
int max_concurrency_level() const noexcept override;
5757

0 commit comments

Comments
 (0)