llvm / llvm/llvm-project

Parallel.h: Adopt std::atomic::wait when llvm-project requires C++20

Open
#189,792 1 comment 0 reactions 0 assignees View on GitHub
llvm:support
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

@aganea @nga888

Optimization opportunity after https://github.com/llvm/llvm-project/pull/189196
`llvm/include/llvm/Support/Parallel.h` can be optimized to eliminate mutex+condition_variable once llvm-project requires C++20

```cpp
class Latch {
std::atomic Count;

public:
explicit Latch(uint32_t Count = 0) : Count(Count) {}
~Latch() { assert(Count.load(std::memory_order_relaxed) == 0); }

void inc() { Count.fetch_add(1, std::memory_order_relaxed); }

void dec() {
if (Count.fetch_sub(1, std::memory_order_acq_rel) == 1)
Count.notify_all();
}

uint32_t getCount() const { return Count.load(std::memory_order_acquire); }

void sync() const {
uint32_t val;
while ((val = Count.load(std::memory_order_acquire)) != 0)
Count.wait(val, std::memory_order_relaxed);
}
};
```

We cannot use std::latch (https://en.cppreference.com/w/cpp/thread/latch.html) because of `inc()`.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.