Skip to content

Commit

Permalink
Micro-optimize locking in fences
Browse files Browse the repository at this point in the history
When a fence has been missed, we can avoid locking *most* of the time
via the double-checked locking pattern. We still lock before a second
check in case the scheduler caused us to miss the fence. If the
scheduler did cause us to miss the fence, we can drop the lock prior to
executing the callback function, as a second micro-optimization.

Signed-off-by: Richard Yao <[email protected]>
  • Loading branch information
ryao authored and doitsujin committed Jan 15, 2024
1 parent 854e06d commit 1456060
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/util/sync/sync_signal.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,22 @@ namespace dxvk::sync {

template<typename Fn>
void setCallback(uint64_t value, Fn&& proc) {
if (value <= this->value()) {
proc();
return;
}

std::unique_lock<dxvk::mutex> lock(m_mutex);

// Verify value is still in the future upon lock.
if (value > this->value())
m_callbacks.emplace_back(std::piecewise_construct,
std::make_tuple(value),
std::make_tuple(proc));
else
else {
lock.unlock();
proc();
}
}

private:
Expand Down

0 comments on commit 1456060

Please sign in to comment.