-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tf: Add TfSpinRWMutex, which performs better than tbb::spin_rw_mutex …
…with low contention (which is when spin locks are appropriate) and when there is high reader contention with low writer contention (which is how we mostly use reader/writer locks). It performs slightly worse when there is simultaneously reader and writer contention, but spin locks should not be used in those situations. Also change TfBigRWMutex to use TfSpinRWMutex internally, improving its throughput under high reader contention. (Internal change: 2256317) (Internal change: 2257556)
- Loading branch information
Showing
6 changed files
with
495 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,6 +70,7 @@ pxr_library(tf | |
setenv | ||
singleton | ||
smallVector | ||
spinRWMutex | ||
stackTrace | ||
stacked | ||
status | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// | ||
// Copyright 2022 Pixar | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "Apache License") | ||
// with the following modification; you may not use this file except in | ||
// compliance with the Apache License and the following modification to it: | ||
// Section 6. Trademarks. is deleted and replaced with: | ||
// | ||
// 6. Trademarks. This License does not grant permission to use the trade | ||
// names, trademarks, service marks, or product names of the Licensor | ||
// and its affiliates, except as required to comply with Section 4(c) of | ||
// the License and to reproduce the content of the NOTICE file. | ||
// | ||
// You may obtain a copy of the Apache License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the Apache License with the above modification is | ||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the Apache License for the specific | ||
// language governing permissions and limitations under the Apache License. | ||
// | ||
|
||
#include "pxr/pxr.h" | ||
|
||
#include "pxr/base/tf/spinRWMutex.h" | ||
#include "pxr/base/arch/defines.h" | ||
|
||
// Needed for ARCH_SPIN_PAUSE on Windows in builds with precompiled | ||
// headers disabled. | ||
#ifdef ARCH_COMPILER_MSVC | ||
#include <intrin.h> | ||
#endif | ||
|
||
#include <thread> | ||
|
||
PXR_NAMESPACE_OPEN_SCOPE | ||
|
||
static constexpr int SpinsBeforeBackoff = 32; | ||
|
||
template <class Fn> | ||
static void WaitWithBackoff(Fn &&fn) { | ||
// Hope for the best... | ||
if (ARCH_LIKELY(fn())) { | ||
return; | ||
} | ||
// Otherwise spin for a bit... | ||
for (int i = 0; i != SpinsBeforeBackoff; ++i) { | ||
ARCH_SPIN_PAUSE(); | ||
if (fn()) { | ||
return; | ||
} | ||
} | ||
// Keep checking but yield our thread... | ||
do { | ||
std::this_thread::yield(); | ||
} while (!fn()); | ||
} | ||
|
||
|
||
void | ||
TfSpinRWMutex::_WaitForWriter() const | ||
{ | ||
// Wait until we see a cleared WriterFlag. | ||
WaitWithBackoff([this]() { | ||
return !(_lockState.load() & WriterFlag); | ||
}); | ||
} | ||
|
||
void | ||
TfSpinRWMutex::_WaitForReaders() const | ||
{ | ||
// Wait until we see zero readers. | ||
WaitWithBackoff([this]() { | ||
return _lockState.load() == WriterFlag; | ||
}); | ||
} | ||
|
||
PXR_NAMESPACE_CLOSE_SCOPE |
Oops, something went wrong.