Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix thread safety issues in UsdSkel_SkelDefinition. #2369

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 29 additions & 18 deletions pxr/usd/usdSkel/skelDefinition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,19 +244,21 @@ UsdSkel_SkelDefinition::_ComputeJointSkelRestTransforms()
if (TF_VERIFY(GetJointLocalRestTransforms(&jointLocalRestXforms))) {

std::lock_guard<std::mutex> lock(_mutex);

VtArray<Matrix4>& skelXforms = _jointSkelRestXforms.Get<Matrix4>();
skelXforms.resize(_topology.size());

const bool success =
UsdSkelConcatJointTransforms(_topology, jointLocalRestXforms,
skelXforms);
if (!(_flags & ComputeFlag)) {
VtArray<Matrix4>& skelXforms = _jointSkelRestXforms.Get<Matrix4>();
skelXforms.resize(_topology.size());

// XXX: Topology was validated when the definition was constructed,
/// so this should not have failed.
TF_VERIFY(success);
const bool success =
UsdSkelConcatJointTransforms(_topology, jointLocalRestXforms,
skelXforms);

_flags = _flags|ComputeFlag;
// XXX: Topology was validated when the definition was constructed,
/// so this should not have failed.
TF_VERIFY(success);

_flags |= ComputeFlag;
}

return true;
}
Expand Down Expand Up @@ -360,11 +362,18 @@ UsdSkel_SkelDefinition::_ComputeJointWorldInverseBindTransforms()

std::lock_guard<std::mutex> lock(_mutex);

_InvertTransforms<Matrix4>(
jointWorldBindXforms,
&_jointWorldInverseBindXforms.Get<Matrix4>());
// Check the flag again after acquiring the lock to avoid re-computing.
// It is not safe to call mutating member functions that can cause a
// copy-on-write detach while other threads may be making copies of the
// array.
if (!(_flags & ComputeFlag)) {
_InvertTransforms<Matrix4>(
jointWorldBindXforms,
&_jointWorldInverseBindXforms.Get<Matrix4>());

_flags |= ComputeFlag;
}

_flags = _flags|ComputeFlag;
return true;
}
return false;
Expand Down Expand Up @@ -430,11 +439,13 @@ UsdSkel_SkelDefinition::_ComputeJointLocalInverseRestTransforms()

std::lock_guard<std::mutex> lock(_mutex);

_InvertTransforms<Matrix4>(
jointLocalRestXforms,
&_jointLocalInverseRestXforms.Get<Matrix4>());
if (!(_flags & ComputeFlag)) {
_InvertTransforms<Matrix4>(
jointLocalRestXforms,
&_jointLocalInverseRestXforms.Get<Matrix4>());

_flags = _flags|ComputeFlag;
_flags |= ComputeFlag;
}

return true;
}
Expand Down