SpatiallyNestable: more locks, less contention

This commit is contained in:
Anthony J. Thibault 2016-02-08 20:09:32 -08:00
parent 9a0e206843
commit 6a82594a4f
2 changed files with 10 additions and 8 deletions

View file

@ -395,7 +395,7 @@ glm::vec3 SpatiallyNestable::getVelocity(bool& success) const {
glm::vec3 parentVelocity = getParentVelocity(success); glm::vec3 parentVelocity = getParentVelocity(success);
Transform parentTransform = getParentTransform(success); Transform parentTransform = getParentTransform(success);
glm::vec3 result; glm::vec3 result;
_transformLock.withReadLock([&] { _velocityLock.withReadLock([&] {
// TODO: take parent angularVelocity into account. // TODO: take parent angularVelocity into account.
result = parentVelocity + parentTransform.getRotation() * _velocity; result = parentVelocity + parentTransform.getRotation() * _velocity;
}); });
@ -410,7 +410,7 @@ glm::vec3 SpatiallyNestable::getVelocity() const {
void SpatiallyNestable::setVelocity(const glm::vec3& velocity, bool& success) { void SpatiallyNestable::setVelocity(const glm::vec3& velocity, bool& success) {
glm::vec3 parentVelocity = getParentVelocity(success); glm::vec3 parentVelocity = getParentVelocity(success);
Transform parentTransform = getParentTransform(success); Transform parentTransform = getParentTransform(success);
_transformLock.withWriteLock([&] { _velocityLock.withWriteLock([&] {
// TODO: take parent angularVelocity into account. // TODO: take parent angularVelocity into account.
_velocity = glm::inverse(parentTransform.getRotation()) * velocity - parentVelocity; _velocity = glm::inverse(parentTransform.getRotation()) * velocity - parentVelocity;
}); });
@ -434,7 +434,7 @@ glm::vec3 SpatiallyNestable::getAngularVelocity(bool& success) const {
glm::vec3 parentAngularVelocity = getParentAngularVelocity(success); glm::vec3 parentAngularVelocity = getParentAngularVelocity(success);
Transform parentTransform = getParentTransform(success); Transform parentTransform = getParentTransform(success);
glm::vec3 result; glm::vec3 result;
_transformLock.withReadLock([&] { _angularVelocityLock.withReadLock([&] {
result = parentAngularVelocity + parentTransform.getRotation() * _angularVelocity; result = parentAngularVelocity + parentTransform.getRotation() * _angularVelocity;
}); });
return result; return result;
@ -448,7 +448,7 @@ glm::vec3 SpatiallyNestable::getAngularVelocity() const {
void SpatiallyNestable::setAngularVelocity(const glm::vec3& angularVelocity, bool& success) { void SpatiallyNestable::setAngularVelocity(const glm::vec3& angularVelocity, bool& success) {
glm::vec3 parentAngularVelocity = getParentAngularVelocity(success); glm::vec3 parentAngularVelocity = getParentAngularVelocity(success);
Transform parentTransform = getParentTransform(success); Transform parentTransform = getParentTransform(success);
_transformLock.withWriteLock([&] { _angularVelocityLock.withWriteLock([&] {
_angularVelocity = glm::inverse(parentTransform.getRotation()) * angularVelocity - parentAngularVelocity; _angularVelocity = glm::inverse(parentTransform.getRotation()) * angularVelocity - parentAngularVelocity;
}); });
} }
@ -592,28 +592,28 @@ void SpatiallyNestable::setLocalOrientation(const glm::quat& orientation) {
glm::vec3 SpatiallyNestable::getLocalVelocity() const { glm::vec3 SpatiallyNestable::getLocalVelocity() const {
glm::vec3 result(glm::vec3::_null); glm::vec3 result(glm::vec3::_null);
_transformLock.withReadLock([&] { _velocityLock.withReadLock([&] {
result = _velocity; result = _velocity;
}); });
return result; return result;
} }
void SpatiallyNestable::setLocalVelocity(const glm::vec3& velocity) { void SpatiallyNestable::setLocalVelocity(const glm::vec3& velocity) {
_transformLock.withWriteLock([&] { _velocityLock.withWriteLock([&] {
_velocity = velocity; _velocity = velocity;
}); });
} }
glm::vec3 SpatiallyNestable::getLocalAngularVelocity() const { glm::vec3 SpatiallyNestable::getLocalAngularVelocity() const {
glm::vec3 result(glm::vec3::_null); glm::vec3 result(glm::vec3::_null);
_transformLock.withReadLock([&] { _angularVelocityLock.withReadLock([&] {
result = _angularVelocity; result = _angularVelocity;
}); });
return result; return result;
} }
void SpatiallyNestable::setLocalAngularVelocity(const glm::vec3& angularVelocity) { void SpatiallyNestable::setLocalAngularVelocity(const glm::vec3& angularVelocity) {
_transformLock.withWriteLock([&] { _angularVelocityLock.withWriteLock([&] {
_angularVelocity = angularVelocity; _angularVelocity = angularVelocity;
}); });
} }

View file

@ -165,6 +165,8 @@ protected:
private: private:
mutable ReadWriteLockable _transformLock; mutable ReadWriteLockable _transformLock;
mutable ReadWriteLockable _idLock; mutable ReadWriteLockable _idLock;
mutable ReadWriteLockable _velocityLock;
mutable ReadWriteLockable _angularVelocityLock;
Transform _transform; // this is to be combined with parent's world-transform to produce this' world-transform. Transform _transform; // this is to be combined with parent's world-transform to produce this' world-transform.
glm::vec3 _velocity; glm::vec3 _velocity;
glm::vec3 _angularVelocity; glm::vec3 _angularVelocity;