Skip to content

Commit 7bfbde5

Browse files
committed
Prefer std::shared_mutex on Windows
1 parent c7697d8 commit 7bfbde5

File tree

4 files changed

+14
-8
lines changed

4 files changed

+14
-8
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CMakeCache.txt
55
CMakeFiles
66
Makefile
77
*.ninja
8+
\.cache
89
\.ninja_*
910
Testing
1011
build

src/nanothread.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ struct Worker {
6464

6565

6666
static Pool *pool_default_inst = nullptr;
67-
static std::mutex pool_default_lock;
67+
static Lock pool_default_lock;
6868
static uint32_t cached_core_count = 0;
6969

7070
uint32_t core_count() {
@@ -128,7 +128,7 @@ uint32_t pool_thread_id() {
128128
}
129129

130130
Pool *pool_default() {
131-
std::unique_lock<std::mutex> guard(pool_default_lock);
131+
std::unique_lock<Lock> guard(pool_default_lock);
132132

133133
if (!pool_default_inst)
134134
pool_default_inst = pool_create();
@@ -159,7 +159,7 @@ void pool_destroy(Pool *pool) {
159159

160160
uint32_t pool_size(Pool *pool) {
161161
if (!pool) {
162-
std::unique_lock<std::mutex> guard(pool_default_lock);
162+
std::unique_lock<Lock> guard(pool_default_lock);
163163
pool = pool_default_inst;
164164
}
165165

@@ -171,7 +171,7 @@ uint32_t pool_size(Pool *pool) {
171171

172172
void pool_set_size(Pool *pool, uint32_t size) {
173173
if (!pool) {
174-
std::unique_lock<std::mutex> guard(pool_default_lock);
174+
std::unique_lock<Lock> guard(pool_default_lock);
175175
pool = pool_default_inst;
176176

177177
if (!pool) {

src/queue.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ std::pair<Task *, uint32_t> TaskQueue::pop() {
397397
}
398398

399399
void TaskQueue::wakeup() {
400-
std::unique_lock<std::mutex> guard(sleep_mutex);
400+
std::unique_lock<Lock> guard(sleep_mutex);
401401
uint64_t value = sleep_state.load();
402402
NT_TRACE("wakeup(): sleep_state := (%u, 0)", (uint32_t) (sleep_state >> 32) + 1);
403403
sleep_state = (value + high_bit) & high_mask;
@@ -440,7 +440,7 @@ TaskQueue::pop_or_sleep(bool (*stopping_criterion)(void *), void *payload,
440440
attempts++;
441441

442442
if (may_sleep && attempts >= NANOTHREAD_MAX_ATTEMPTS) {
443-
std::unique_lock<std::mutex> guard(sleep_mutex);
443+
std::unique_lock<Lock> guard(sleep_mutex);
444444

445445
uint64_t value = ++sleep_state, phase = value & high_mask;
446446
NT_TRACE("pop_or_sleep(): falling asleep after %.2f milliseconds, "

src/queue.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,19 @@
1111

1212
#include <atomic>
1313
#include <vector>
14-
#include <mutex>
1514
#include <condition_variable>
1615
#include <cstring>
1716

1817
#if defined(_WIN32)
1918
# include <windows.h>
19+
# include <shared_mutex>
20+
using Lock = std::shared_mutex; // Prefer (more efficient) shared_mutex on Windows
21+
#else
22+
# include <mutex>
23+
using Lock = std::mutex;
2024
#endif
2125

26+
2227
struct Pool;
2328

2429
constexpr uint64_t high_bit = (uint64_t) 0x0000000100000000ull;
@@ -241,7 +246,7 @@ struct TaskQueue {
241246
std::atomic<uint64_t> sleep_state;
242247

243248
/// Mutex protecting the fields below
244-
std::mutex sleep_mutex;
249+
Lock sleep_mutex;
245250

246251
/// Condition variable used to manage workers that are asleep
247252
std::condition_variable sleep_cv;

0 commit comments

Comments
 (0)