Skip to content

Commit 929e15e

Browse files
committed
Refactor: Add safe hardware concurrency detection.
1 parent e0e7a61 commit 929e15e

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
cmake_minimum_required(VERSION 3.25)
2-
project(xtxn_fast_mpmc_queue VERSION 0.0.4 LANGUAGES CXX)
2+
project(xtxn_fast_mpmc_queue VERSION 0.1.0 LANGUAGES CXX)
33

44
set(CMAKE_VERBOSE_MAKEFILE ON)
55
set(CMAKE_CXX_STANDARD 20)

src/config.hpp

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
#include <thread>
1010

1111
namespace test::config {
12+
inline unsigned baseline_concurrency() {
13+
return std::max(std::thread::hardware_concurrency(), 2u);
14+
}
15+
1216
struct prelim {
1317
#if defined(ENABLE_MEMORY_PROFILING)
1418
const int prelim_test_iters { 2 };
@@ -23,19 +27,19 @@ namespace test::config {
2327
};
2428

2529
struct mpsc : public prelim {
26-
const unsigned cores;
30+
const unsigned concurrency;
2731
const config_set set_a;
2832
const config_set set_b;
2933
const config_set set_c;
3034
const config_set set_d;
3135

3236
mpsc()
3337
: prelim {},
34-
cores { std::thread::hardware_concurrency() },
38+
concurrency { baseline_concurrency() },
3539
set_a { 1, 1 },
36-
set_b { std::max(1u, cores - 1), 1 },
37-
set_c { cores, 1 },
38-
set_d { cores << 1, 1 } {}
40+
set_b { std::max(1u, concurrency - 1), 1 },
41+
set_c { concurrency, 1 },
42+
set_d { concurrency << 1, 1 } {}
3943

4044
mpsc(const mpsc &) = delete;
4145
mpsc(mpsc &&) = delete;
@@ -46,19 +50,19 @@ namespace test::config {
4650
};
4751

4852
struct mpmc : public prelim {
49-
const unsigned cores;
53+
const unsigned concurrency;
5054
const config_set set_a;
5155
const config_set set_b;
5256
const config_set set_c;
5357
const config_set set_d;
5458

5559
mpmc()
5660
: prelim {},
57-
cores { std::thread::hardware_concurrency() },
58-
set_a { proportion(cores, cores >> 1) },
59-
set_b { proportion(cores, static_cast<unsigned>(static_cast<double>(cores) / 3.0 * 2.0)) },
60-
set_c { same(cores) },
61-
set_d { same(cores << 1) } {}
61+
concurrency { baseline_concurrency() },
62+
set_a { proportion(concurrency, concurrency >> 1) },
63+
set_b { proportion(concurrency, static_cast<unsigned>(static_cast<double>(concurrency) / 3.0 * 2.0)) },
64+
set_c { same(concurrency) },
65+
set_d { same(concurrency << 1) } {}
6266

6367
mpmc(const mpmc &) = delete;
6468
mpmc(mpmc &&) = delete;

0 commit comments

Comments
 (0)