Skip to content

Commit 2918a09

Browse files
authored
Refactor threading run (#1961)
* ThreadManager::WaitForAllThreads removed * WaitForAllThreads was only called either in single threaded environments or just before all threads are joined anyway. As this doesn't add a useful synchronization point, it's removed. * Formatting issue * Thread Sanitizer satisfied * More formatting issues
1 parent cb4239f commit 2918a09

2 files changed

Lines changed: 3 additions & 24 deletions

File tree

src/benchmark_runner.cc

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,6 @@ BenchmarkRunner::IterationResults BenchmarkRunner::DoNIterations() {
302302
/*profiler_manager=*/nullptr);
303303

304304
// The main thread has finished. Now let's wait for the other threads.
305-
manager->WaitForAllThreads();
306305
for (std::thread& thread : pool) {
307306
thread.join();
308307
}
@@ -434,7 +433,6 @@ MemoryManager::Result BenchmarkRunner::RunMemoryManager(
434433
RunInThread(&b, memory_iterations, 0, manager.get(),
435434
perf_counters_measurement_ptr,
436435
/*profiler_manager=*/nullptr);
437-
manager->WaitForAllThreads();
438436
manager.reset();
439437
b.Teardown();
440438
MemoryManager::Result memory_result;
@@ -450,7 +448,6 @@ void BenchmarkRunner::RunProfilerManager(IterationCount profile_iterations) {
450448
RunInThread(&b, profile_iterations, 0, manager.get(),
451449
/*perf_counters_measurement_ptr=*/nullptr,
452450
/*profiler_manager=*/profiler_manager);
453-
manager->WaitForAllThreads();
454451
manager.reset();
455452
b.Teardown();
456453
}

src/thread_manager.h

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,15 @@ namespace internal {
1111

1212
class ThreadManager {
1313
public:
14-
explicit ThreadManager(int num_threads)
15-
: alive_threads_(num_threads), start_stop_barrier_(num_threads) {}
14+
explicit ThreadManager(int num_threads) : start_stop_barrier_(num_threads) {}
1615

1716
Mutex& GetBenchmarkMutex() const RETURN_CAPABILITY(benchmark_mutex_) {
1817
return benchmark_mutex_;
1918
}
2019

21-
bool StartStopBarrier() EXCLUDES(end_cond_mutex_) {
22-
return start_stop_barrier_.wait();
23-
}
24-
25-
void NotifyThreadComplete() EXCLUDES(end_cond_mutex_) {
26-
start_stop_barrier_.removeThread();
27-
if (--alive_threads_ == 0) {
28-
MutexLock lock(end_cond_mutex_);
29-
end_condition_.notify_all();
30-
}
31-
}
20+
bool StartStopBarrier() { return start_stop_barrier_.wait(); }
3221

33-
void WaitForAllThreads() EXCLUDES(end_cond_mutex_) {
34-
MutexLock lock(end_cond_mutex_);
35-
end_condition_.wait(lock.native_handle(),
36-
[this]() { return alive_threads_ == 0; });
37-
}
22+
void NotifyThreadComplete() { start_stop_barrier_.removeThread(); }
3823

3924
struct Result {
4025
IterationCount iterations = 0;
@@ -51,10 +36,7 @@ class ThreadManager {
5136

5237
private:
5338
mutable Mutex benchmark_mutex_;
54-
std::atomic<int> alive_threads_;
5539
Barrier start_stop_barrier_;
56-
Mutex end_cond_mutex_;
57-
Condition end_condition_;
5840
};
5941

6042
} // namespace internal

0 commit comments

Comments
 (0)